Java 使用JodaTime获取本地化周数

Java 使用JodaTime获取本地化周数,java,jodatime,Java,Jodatime,我正在尝试使用JodaTime获取当前周数 在法国,周的定义如下: 一周从周一开始(而在美国,一周从周日开始) 一年中的第一周是包含1月4日的一周(而在国际海事组织,在美国是包含1月1日的一周。对吗?我证实了这一点) 2012年1月1日是星期日。 因此, 根据法国历法,它属于2011年的第52周 根据美国日历,它属于2012年的第1周 使用JodaTime,我发现我可以通过以下方法获得周数DateTime\getWeekOfWeekyear() 我认为,通过指定正确的时区,我将获得本地化

我正在尝试使用JodaTime获取当前周数

在法国,周的定义如下:

  • 一周从周一开始(而在美国,一周从周日开始)
  • 一年中的第一周是包含1月4日的一周(而在国际海事组织,在美国是包含1月1日的一周。对吗?我证实了这一点)
2012年1月1日是星期日。 因此,

  • 根据法国历法,它属于2011年的第52周
  • 根据美国日历,它属于2012年的第1周
使用JodaTime,我发现我可以通过以下方法获得周数
DateTime\getWeekOfWeekyear()

我认为,通过指定正确的时区,我将获得本地化结果:

DateTime dtFr = new DateTime(2012, 1, 1, 11,11, DateTimeZone.forTimeZone(TimeZone.getTimeZone("Europe/Paris")));
DateTime dtUS = new DateTime(2012, 1, 1, 11,11, DateTimeZone.forTimeZone(TimeZone.getTimeZone("US/Arizona")));
LOGGER.info("weekYear (FR) : " + dtFr.weekyear().get());
LOGGER.info("weekOfWeekYear (FR) : " + dtFr.getWeekOfWeekyear());
LOGGER.info("weekYear (US) : " + dtUS.weekyear().get());
LOGGER.info("weekOfWeekYear (US) : " + dtUS.getWeekOfWeekyear());
输出为:

2014-03-05 11:28:08,708 - INFO - c.g.s.u.JodaTest - weekYear (FR) : 2011
2014-03-05 11:28:08,709 - INFO - c.g.s.u.JodaTest - weekOfWeekYear (FR) : 52
2014-03-05 11:28:08,709 - INFO - c.g.s.u.JodaTest - weekYear (US) : 2011
2014-03-05 11:28:08,709 - INFO - c.g.s.u.JodaTest - weekOfWeekYear (US) : 52
我原以为:

  • weekYear(美国):2012年
  • weekOfWeekYear(美国):1

我的代码有问题吗?

根据Joda Time对该方法的描述-

描述是-

public int getWeekOfWeekyear()
Get the week of weekyear field value.
This field is associated with the "weekyear" via getWeekyear(). In the standard ISO8601 week algorithm, the first week of the year is that in which at least 4 days are in the year. As a result of this definition, day 1 of the first week may be in the previous year.

Specified by:
getWeekOfWeekyear in interface ReadableDateTime
Returns:
the week of a week based year

代码中没有错误

对于您关于本地化周数的问题,您的回答很悲伤:JodaTime不支持它,只支持ISO-8601对周数的定义。我知道对于很多用户来说,这就像是一个表演的阻碍。这里旧的java.util.*-stuff显然更好

在JodaTime中实现此功能的唯一方法是引入一个专门的日期时间字段,但正如项目负责人自己所承认的,这肯定不容易实现。这已经发生了,但此后什么也没有发生


顺便说一句,时区与本地化周数的问题无关,因此您无法用任何类型的时区配置来解决这个旧的Joda问题。

我知道这个问题已经存在3年了,但是随着Java 8及更高版本中内置的类,现在可以用一个优雅的短代码解决这个问题,而无需Joda time:

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.WeekFields;

public class USWeek {

    public static final WeekFields US_WEEK_FIELDS = WeekFields.of(DayOfWeek.SUNDAY, 4);
    // equivalent to: public static final WeekFields US_WEEK_FIELDS = WeekFields.SUNDAY_START;
    // equivalent to: public static final WeekFields US_WEEK_FIELDS = WeekFields.of(Locale.US);

    public static int getWeek(LocalDate date) {
        return date.get(US_WEEK_FIELDS.weekOfWeekBasedYear());
    }

    public static int getYear(LocalDate date) {
        return date.get(US_WEEK_FIELDS.weekBasedYear());
    }
}

注:在本代码中,美国周定义为硬编码。也可以通过调用从中定义对象。

对于
一年中至少有4天的
部分,确定。但如果我用2009年取代2011年:2009年1月1日是星期四。因为在美国,每周从周日开始,2009年这一周只有3天。所以根据美国日历,应该是2008年的第52周,但我的代码显示这两个日历都是2009年的第一周。