简体   繁体   English

使用JodaTime获取本地化的周数

[英]Get localized week number with JodaTime

I am trying to get the current week number with JodaTime. 我想用JodaTime获得当前的周数。

In France, weeks are defined like this : 在法国,周定义如下:

  • A week begins on Monday (whereas weeks begin on Sunday in the US). 一周从星期一开始(而星期日在美国开始)。
  • The first week of the year is the week which contains the 4th of January (whereas IMO, it is the week which contains 1st of January in the USA. Is that right? I verfied it here ). 一年中的第一周是包含1月4日的一周(而IMO,这是包含1月1日在美国的一周。是吗?我在这里做了验证 )。

Example : The 1st of January 2012 is a Sunday. 示例:2012年1月1日是星期日。 Hence, 因此,

  • According to French Calendar, it belongs to week 52 of 2011 . 根据法国日历,它属于2011年52周。
  • According to US calendar, it belongs to week 1 of 2012 根据美国日历,它属于2012年1

With JodaTime, I found that I can get the week number with the following method DateTime#getWeekOfWeekyear() . 使用JodaTime,我发现我可以使用以下方法获取周数: DateTime#getWeekOfWeekyear()

I thought that by specifying the right TimeZone, I would get a localized result : 我认为通过指定正确的TimeZone,我会得到一个本地化的结果:

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());

The output is : 输出是:

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

I had expected : 我原以为:

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

Is there something wrong in my code ? 我的代码中有什么问题吗?

Sad answer to your question for localized week number: JodaTime does not support it, only the ISO-8601 definition of week numbers. 对于本地化周数的问题的悲伤答案:JodaTime不支持它,只支持ISO-8601周数的定义。 I know for many users this is like a show stopper. 我知道很多用户这就像一个节目塞子。 Here the old java.util.*-stuff is clearly better. 这里旧的java.util。* - 东西显然更好。

The only way to realize this feature in JodaTime would be to introduce a specialized date-time-field, but it is surely not easy to implement as the project lead himself admitted. 在JodaTime中实现此功能的唯一方法是引入一个专门的日期时间字段,但实际上并不容易实现,因为项目负责人自己承认。 This was already noticed in year 2007 , but nothing has happened since. 在2007年已经被注意到了 ,但此后一无所获。

By the way, time zones have nothing to do with the question of localized week numbers so you cannot cure this old Joda problem with any kind of time zone configuration. 顺便说一下,时区与本地周数的问题无关,所以你不能用任何类型的时区配置来解决这个旧的Joda问题。

I know this question is 3 years old, but with the java.time classes built into Java 8 and later, this problem can now be resolved with an elegant short code, without Joda-Time: 我知道这个问题已经有3年了,但是对于Java 8及更高版本中内置的java.time类,现在可以使用优雅的短代码来解决这个问题,而不需要使用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());
    }
}

Note: in this code, US weeks definition is hardcoded. 注意:在此代码中,美国周定义是硬编码的。 The WeekFields object can also be defined from a Locale by calling WeekFields.of( Locale ) . 也可以通过调用WeekFields.of(Locale)Locale定义WeekFields对象。

As per Joda Time Description of this method - 根据Joda时间此方法的描述 -

Joda Time 乔达时间

Description is - 描述是 -

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

There is nothing wrong in the code. 代码没有错。

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

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