21

让我自己解释一下。通过知道日期的周数和年份:

Date curr = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(curr);
int nweek = cal.WEEK_OF_YEAR;
int year = cal.YEAR;

但是现在我不知道如何获取该周第一天的日期。我一直在寻找日历、日期、日期格式,但没有任何有用的东西......

有什么建议吗?(在 Java 中工作)

4

8 回答 8

40

这些字段不返回值。这些是标识对象中可以获取/设置/添加的字段的常量。Calendar要实现您想要的,您首先需要获取 a Calendar,清除它并设置已知值。它会自动将日期设置为该周的第一天。

// We know week number and year.
int week = 3;
int year = 2010;

// Get calendar, clear it and set week number and year.
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.WEEK_OF_YEAR, week);
calendar.set(Calendar.YEAR, year);

// Now get the first day of week.
Date date = calendar.getTime();

请学习阅读javadocs以了解如何使用类/方法/字段,不要尝试在您的 IDE 中随意戳 ;)

也就是说,java.util.Dateandjava.util.Calendar史诗般的失败。如果可以,请考虑切换到Joda Time

于 2010-01-21T12:40:46.750 回答
4

试试这个:

public static Calendar setWeekStart(Calendar calendar) {
  while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
    calendar.add(Calendar.DATE, -1);
  }
  setDayStart(calendar); // method which sets H:M:S:ms to 0
  return calendar;
}
于 2012-02-28T16:47:59.797 回答
2

我在java中没有做太多日期的东西,但一个解决方案可能是:

cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) - cal.get(Calendar.DAY_OF_WEEK));

逻辑:

获取星期几并从当前日期中减去它(可能需要-1,取决于您需要星期一是一周的第一天还是星期天)

于 2010-01-21T12:41:50.100 回答
2

java.time

java.util日期时间 API 及其格式化 API已SimpleDateFormat过时且容易出错。建议完全停止使用它们并切换到现代 Date-Time API *

另外,下面引用的是来自Joda-Time主页的通知:

请注意,从 Java SE 8 开始,用户被要求迁移到 java.time (JSR-310) - JDK 的核心部分,它取代了这个项目。

使用java.time现代日期时间 API 的解决方案:

import java.time.LocalDate;
import java.time.temporal.WeekFields;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        // Test
        int weekNumber = 34;
        int year = 2021;
        
        System.out.println(getFirstDayOfWeek(year, weekNumber, Locale.UK));
        System.out.println(getFirstDayOfWeek(year, weekNumber, Locale.US));
    }

    static LocalDate getFirstDayOfWeek(int year, int weekNumber, Locale locale) {
        return LocalDate
                .of(year, 2, 1)
                .with(WeekFields.of(locale).getFirstDayOfWeek())
                .with(WeekFields.of(locale).weekOfWeekBasedYear(), weekNumber);
    }
}

输出:

2021-08-23
2021-08-15

ONLINE DEMO

请注意,一周的第一天取决于 - 例如在英国是Locale星期一,而在美国是星期日。根据ISO 8601标准,现在是星期一。为了比较,请查看美国日历英国日历

Trail: Date Time了解有关现代日期时间 API *的更多信息。


* 出于任何原因,如果您必须坚持使用 Java 6 或 Java 7,则可以使用ThreeTen-Backport,它将大部分java.time功能向后移植到 Java 6 和 7。如果您正在为 Android 项目和 Android API 工作level 仍然不符合 Java-8,请检查Java 8+ APIs available through desugaringHow to use ThreeTenABP in Android Project

于 2021-08-28T10:54:18.510 回答
1

还有一种方式...

    GregorianCalendar cal = new GregorianCalendar();
    cal.clearTime();
    Integer correction = 1-cal.get(GregorianCalendar.DAY_OF_WEEK)
    cal.add(Calendar.DATE, correction);

cal 现在是一周的第一天

1-cal.get(GregorianCalendar.DAY_OF_WEEK)

周日(我的语言环境中一周的第一天)评估为 1-1,周一评估为 1-2,因此这将为您提供将时钟倒回到周日所需的更正

于 2012-09-12T14:35:24.647 回答
0

这是一些快速而肮脏的代码来执行此操作。此代码使用当前日期创建一个日历对象,计算星期几,然后减去星期几,这样您就在第一个(星期日)。尽管我使用的是 DAY_OF_YEAR,但它可以使用多年(1/2/10 它将返回 12/27/09,这是正确的)。

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DOW {

    public static void main(String[] args) {
        DOW dow = new DOW();
        dow.doIt();
        System.exit(0);
    }

    private void doIt() {
        Date curr = new Date(); 
        Calendar cal = Calendar.getInstance(); 
        cal.setTime(curr); 
        int currentDOW = cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DAY_OF_YEAR, (currentDOW * -1)+1);

        Format formatter = new SimpleDateFormat("MM/dd/yy");
        System.out.println("First day of week="+formatter.format(cal.getTime()));
    }
}
于 2010-01-21T13:06:59.200 回答
0

小心这些,calendar.get(Calendar.WEEK_OF_YEAR)如果是 12 月底并且已经是明年结束的一周,则返回 1。

使用

//            cal2.set(Calendar.WEEK_OF_YEAR, cal.get(Calendar.WEEK_OF_YEAR));
//            cal2.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

将 cal2 日期设置为例如 2009 年底 29/12/2010 !

我用这个:

cal2.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR)); //to round at the start of day
cal2.set(Calendar.YEAR, cal.get(Calendar.YEAR));
cal2.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); //to round at the start of week

我还确保我的日历中的几周,无论它们在哪个地区,都是从星期一开始的:

cal.setFirstDayOfWeek(Calendar.MONDAY);
cal2.setFirstDayOfWeek(Calendar.MONDAY);
于 2010-12-29T11:53:18.880 回答
0

尝试公历算法:

public int getFirstDay(int m, int year)
    {
        int k=1;
        int c, y, w, M=0;
        if(m>2 && m<=12) M=m-2;
        else if(m>0 && M<=2)
        {
            M=m+10;
            year-=1;
        }
        c=year/100;
        y=year%100;
        w=(int)((k+(Math.floor(2.6*M - 0.2))-2*c+y+(Math.floor(y/4))+(Math.floor(c/4)))%7);//a fantastic formula           
        if(w<0) w+=7;
        return w;//thus the day of the week is obtained!
    }
于 2013-07-30T16:31:56.150 回答