如何从Java中的ISO8601周数计算日期 [英] How to calculate Date from ISO8601 week number in Java

查看:240
本文介绍了如何从Java中的ISO8601周数计算日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法做到这一点:如何获取一周的日期(我知道周数)? ISO 8601 周编号,不使用Java中的任何库或日历?

Is there any way of doing this: How to get dates of a week (I know week number)? for ISO 8601 week number without using any library or calender in Java?

推荐答案

更新:此处介绍的概念仍然适用,但代码已过时。 Joda-Time 项目,现在位于维护模式,建议迁移到 java.time 类。请参阅 Szulc回答中的java.time代码。

UPDATE: The concepts presented here still apply, but the code is outmoded. The Joda-Time project, now in maintenance mode, advises migration to the java.time classes. See the java.time code in the Answer by Szulc.

DateTime dateTimeStart = new DateTime( "2003-W01-1", DateTimeZone.UTC ); // Joda-Time 2.4.
DateTime dateTimeStop = dateTimeStart.plusWeeks( 1 );

详情请继续阅读。

与Java捆绑在一起的旧java.util.Date和java.util.Calendar类是众所周知的麻烦,应该避免。 Sun及其合作伙伴在Java库中添加了许多简洁的东西,但并非所有这些都很好。日期时间类可能是最糟糕的。

The old java.util.Date and java.util.Calendar classes bundled with Java are notoriously troublesome and should be avoided. Sun and its partners put a lot of neat stuff in the Java libraries, but not all of it is good. The date-time classes are perhaps the worst of it.

此外,这些类对 ISO 8601周。有关详细信息,请参阅此答案

Furthermore those classes have weak support for ISO 8601 weeks. See this answer for details.

您可以使用这些类编写自己的代码,但我不建议这样做。计算ISO周的规则很简单:

You could write your own code using those classes, but I don't recommend this. The rules for calculation of ISO weeks are simple enough:


  • 第1周是日历年的第一个星期四。

  • 星期一是一周的第一天。

在他们的位置,常见的替代品是名为 Joda-Time 的图书馆。该库包含对ISO周的出色支持。

In their place, the common replacement is a library called Joda-Time. This library includes excellent support for ISO weeks.

简单地添加到您的项目中,只需添加一个 .jar 文件。

Simple to add to your project, just add a single .jar file.

请参阅我的其他答案这一个,例如获取日期的代码 - 来自ISO周数的时间。

See this other answer of mine or this one for example code for getting a date-time from an ISO week number.

Java 8有一个新的日期 - 时间框架,受Joda-Time启发,可在 java.time 包中找到。

Java 8 has a new date-time framework, inspired by Joda-Time, found in the java.time package.

Java用于将库混合在一起。这样做是面向对象编程和后期绑定的主要目的之一。对你的问题的评论指的是老板非理性或无知地禁止添加图书馆这种非常常见的情况。虽然这种禁令有正当理由,但它们很少见。

Java is built to mix libraries together. Doing so is one of the main purposes to object-oriented programming and late-binding. The comments on your question refer to the all-too-common situation where bosses irrationally or ignorantly forbid adding libraries. While there are valid reasons for such a ban, they are rare.

禁止在Java中添加库和罐子就像禁止在装有车辆的车辆上挂钩顺便说一句。

Forbidding adding libraries and jars in Java is like forbidding the hooking up of trailers on a fleet of vehicles equipped with a hitch.

旧的日期时间课程确实很糟糕,我们很多人都把Joda-Time添加到大多数新项目作为习惯。

The old date-time classes really are bad enough that many of us add Joda-Time to most any new project as a habit.

在日期工作中,定义时间跨度的常用方法是半开接近。这意味着开头是包容性的,而结尾是独占的。因此,标准周从星期一的第一个时刻开始,到下一个星期一的第一个时刻结束。搜索StackOverflow.com以获取更多讨论和示例。

In date-time work, a common way to define a span of time is the "Half-Open" approach. This means the beginning is inclusive while the ending is exclusive. So a standard week begins on the first moment of a Monday, and ends on the first moment of the following Monday. Search StackOverflow.com for more discussion and examples.

ISO 8601标准定义了代表标准周甚至是那一周内的一天。

The ISO 8601 standard defines ways to represent a standard week and even a day within that week.

取年份,连字符, W 分隔符,以及周数表示整周: YYYY-Www 。添加连字符和星期几编号以确定该周内的一天: YYYY-Www-D

Take the year, a hyphen, a W delimiter, and the week number represents the whole week: YYYY-Www. Add a hyphen and day-of-week number to pinpoint a day within that week: YYYY-Www-D.

Joda-Time理解这种格式,如下面的代码示例所示。

Joda-Time understands this format as seen in the code example below.

这是一些Joda-Time 2.4代码。搜索StackOverflow.com以获取这些概念的讨论和示例。这个问题和这个答案几乎与许多其他答案重复。

Here is some Joda-Time 2.4 code. Search StackOverflow.com for discussion and examples of these concepts. This Question and this Answer pretty much duplicate many others.

int year = 2003;
int week = 1; // Domain: 1 to 53.

// Build a String in ISO 8601 Week format: YYYY-Www-D
// Hard-coding a `1` for Monday, the standard first-day-of-week.
String input = ( String.format( "%04d", year ) + "-W" + String.format( "%02d", week ) + "-1" );

// Specify the time zone by which to define the beginning of a day.
DateTimeZone timeZone = DateTimeZone.UTC; // Or: DateTimeZone.forID( "America/Montreal" );

// Calculate beginning and ending, using Half-Open (inclusive, exclusive) approach.
DateTime dateTimeStart = new DateTime( input, timeZone );
DateTime dateTimeStop = dateTimeStart.plusWeeks( 1 );

// Use Joda-Time's tidy Interval class to represent the entire week. Use getters to access start and stop.
Interval weekInterval = new Interval( dateTimeStart, dateTimeStop );

// Is today in that week? Joda-Time has handy methods: contains, isBefore, isAfter, overlap.
boolean isTodayInThatWeek = weekInterval.contains( DateTime.now() );

转储到控制台。

System.out.println( "input: " + input );
System.out.println( "dateTimeStart: " + dateTimeStart );
System.out.println( "dateTimeStop: " + dateTimeStop );
System.out.println( "interval: " + interval );
System.out.println( "isTodayInThatWeek: " + isTodayInThatWeek );

运行时。

input: 2003-W01-1
dateTimeStart: 2002-12-30T00:00:00.000Z
dateTimeStop: 2003-01-06T00:00:00.000Z
interval: 2002-12-30T00:00:00.000Z/2003-01-06T00:00:00.000Z
isTodayInThatWeek: false

这篇关于如何从Java中的ISO8601周数计算日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆