简体   繁体   English

在给出周数,工作日和年份的Java中获取日期

[英]Get Date in Java given Week number, week day and year

Given Week of the year, the week day and the year, how can we get the Date in Java? 鉴于一年中的每周,每周和每年,我们如何在Java中获取日期?

With Jodatime, I tried the following: 有了Jodatime,我尝试了以下方法:

DateTime dt = new DateTime();
dt.withYear(year);
dt.withWeekOfWeekyear(weekOfYear);
dt.withDayOfWeek(weekDay);
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyMMdd");
System.out.println(dateTimeFormatter.print(dt));

But it gets the current Date! 但它得到了当前的日期!

JodaTime returns a changed copy, so do: JodaTime返回一个已更改的副本,所以:

DateTime dt = new DateTime()
    .withWeekyear(year)
    .withWeekOfWeekyear(weekOfYear)
    .withDayOfWeek(weekDay);
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyMMdd");
System.out.println(dateTimeFormatter.print(dt));

And this should work as expected. 这应该按预期工作。

The accepted answer has bug. 接受的答案有bug。 .withYear(year) should be withWeekyear(year) . .withYear(year)应该是withWeekyear(year) @Neet please update it. @Neet请更新它。

You need to reassign the date afterwards! 之后您需要重新分配日期! the dt.with*() methods simply make a copy of the date. dt.with *()方法只是复制日期。

try 尝试

DateTime dt = new DateTime();
dt = dt.withYear(year);
dt = dt.withWeekOfWeekyear(weekOfYear);
dt = dt.withDayOfWeek(weekDay);
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyMMdd");
System.out.println(dateTimeFormatter.print(dt));

We can also use this native java code using Calendar class: 我们也可以使用Calendar类来使用这个本机java代码:

    SimpleDateFormat sdf = new SimpleDateFormat("MM dd yyyy");
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.WEEK_OF_YEAR, 23);
    cal.set(Calendar.DAY_OF_WEEK, 3);
    cal.set(Calendar.YEAR,2013);
    System.out.println(sdf.format(cal.getTime()));

Here is a simple example of how to do it without JodaTime: 这是一个简单的例子,说明如何在没有JodaTime的情况下完成它:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Snippet {
    public static void main(String args[]) {
        String year = "2013";
        String week_of_year = "46";
        String day_of_week = "4";
        String yearweekday = year + week_of_year + day_of_week;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyywwu");
        Date date = null;
        try {
            date = sdf.parse(yearweekday);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(date);
    }
}

Good luck! 祝好运!

tl;dr TL;博士

YearWeek.of( 2017 , 1 )
        .atDay( DayOfWeek.TUESDAY ) 
        .format( DateTimeFormatter.ofPattern( "uuMMdd" ) ) 

“Week” ambiguous “周”暧昧

The word 'week' is ambiguous. “周”这个词含糊不清。 Do you mean week number 1 contains January 1? 你的意思是第1周包含1月1日? Or week number 1 contains the first of a particular day of year such as Sunday or Monday? 或者第1周包含一个特定日期的第一天,例如星期日或星期一?

Or do you mean a standard ISO 8601 week ? 或者你的意思是标准的ISO 8601周 To quote from YearWeek doc : 引用YearWeek doc

ISO-8601 defines the week as always starting with Monday. ISO-8601将星期定义为始终从星期一开始。 The first week is the week which contains the first Thursday of the calendar year. 第一周是包含该日历年的第一个星期四的一周。 As such, the week-based-year used in this class does not align with the calendar year. 因此,本课程中使用的以周为基础的年份与日历年不一致。

java.time java.time

The modern approach uses the java.time classes. 现代方法使用java.time类。

ThreeTen-Extra ThreeTen-EXTRA

The ThreeTen-Extra project extends java.time with additional functionality. ThreeTen-Extra项目扩展了具有附加功能的java.time。 This includes a handy YearWeek class, just what we need for this Question. 这包括一个方便的YearWeek课程,正是我们对本课题的需求。

Specify your week-based year number and your week number. 指定基于周的年份编号和周编号。

YearWeek yw = YearWeek.of( 2017 , 1 ) ; 

The LocalDate class represents a date-only value without time-of-day and without time zone. LocalDate类表示没有时间且没有时区的仅日期值。

You can ask the YearWeek object to determine the date of a day contained within its week. 您可以要求YearWeek对象确定其周内包含的一天的日期。 Specify a DayOfWeek enum object. 指定DayOfWeek枚举对象。 Note that a DayOfWeek is an object rather than a mere integer or string, providing for type-safety and valid values. 请注意, DayOfWeek是一个对象,而不仅仅是整数或字符串,提供类型安全性和有效值。

LocalDate ld = yw.atDay( DayOfWeek.TUESDAY ) ; 

About java.time 关于java.time

The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat . 这些类取代了麻烦的旧遗留日期时间类,如java.util.DateCalendarSimpleDateFormat

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial . 要了解更多信息,请参阅Oracle教程 And search Stack Overflow for many examples and explanations. 并搜索Stack Overflow以获取许多示例和解释。 Specification is JSR 310 . 规范是JSR 310

Where to obtain the java.time classes? 从哪里获取java.time类?

The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time. 该项目是未来可能添加到java.time的试验场。 You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more . 您可以在这里找到一些有用的类,比如IntervalYearWeekYearQuarter ,和更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM