简体   繁体   English

android日历中的周数是错误的

[英]Week number is wrong in android calendar

Week number is wrong in android calendar android日历中的周数是错误的

In some of device getting as week 53, but in some other devices getting as week 1 for end of the year在某些设备中作为第 53 周,但在其他一些设备中作为年末的第 1 周

Calendar calender = Calendar.getInstance();日历日历 = Calendar.getInstance(); int week = calender.get(Calendar.WEEK_OF_YEAR); int 周 = calender.get(Calendar.WEEK_OF_YEAR);

First Week第一周

Calendar defines a locale-specific seven day week using two parameters: the first day of the week and the minimal days in first week (from 1 to 7). Calendar 使用两个参数定义了特定语言环境的每周 7 天:一周的第一天和第一周的最少天数(从 1 到 7)。 These numbers are taken from the locale resource data when a Calendar is constructed.这些数字是在构造 Calendar 时从语言环境资源数据中获取的。 They may also be specified explicitly through the methods for setting their values.它们也可以通过设置它们的值的方法明确指定。

When setting or getting the WEEK_OF_MONTH or WEEK_OF_YEAR fields, Calendar must determine the first week of the month or year as a reference point.设置或获取 WEEK_OF_MONTH 或 WEEK_OF_YEAR 字段时,日历必须确定月份或年份的第一周作为参考点。 The first week of a month or year is defined as the earliest seven day period beginning on getFirstDayOfWeek() and containing at least getMinimalDaysInFirstWeek() days of that month or year .一个月或一年的第一周定义为从 getFirstDayOfWeek() 开始并至少包含该月或年的 getMinimalDaysInFirstWeek() 天数的最早 7 天 Weeks numbered ..., -1, 0 precede the first week;周数 ..., -1, 0 在第一周之前; weeks numbered 2, 3,... follow it.周数为 2、3、... 跟着它。 Note that the normalized numbering returned by get() may be different.请注意,get() 返回的规范化编号可能不同。 For example, a specific Calendar subclass may designate the week before week 1 of a year as week n of the previous year.例如,特定的 Calendar 子类可以将一年的第 1 周之前的一周指定为前一年的第 n 周。

From Java documentation ( https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html )来自 Java 文档( https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html

tl;dr tl;博士

LocalDate.of( 2018 , Month.DECEMBER , 31 )
         .get( IsoFields.WEEK_OF_WEEK_BASED_YEAR ) 

1 1

Locale语言环境

Not sure of the direct answer to your Question, but likely to be differing Locale values in play.不确定您的问题的直接答案,但可能是不同的Locale值在起作用。 In Calendar the definition of a week varies by the locale.Calendar中,一周的定义因地区而异。

But this is moot .但这是没有实际意义的 You should be using the java.time classes that replaced Calendar class.您应该使用取代Calendar类的java.time类。

java.time时间

Calendar is part of the troublesome old date-time classes that are now legacy, supplanted by the java.time classes. Calendar是现在遗留下来的麻烦的旧日期时间类的一部分,被java.time类取代。 For earlier Android, see the last bullets below.对于早期的 Android,请参阅下面的最后一个要点。

You must define what you mean by week number.您必须定义周数的含义。 There are different ways to define a week and week number.有多种方法可以定义周和周数。

By default, the java.time classes use the standard ISO 8601 definition : Week # 1 has the first Thursday of the calendar year, and starts on Monday (as you asked for).默认情况下, java.time 类使用标准的ISO 8601 定义:第 1 周是日历年的第一个星期四,从星期一开始(如您所要求的那样)。 So years have either 52 or 53 weeks.所以年有 52 或 53 周。 The first and last few days of the calendar year may land in the prior/following week-based year.日历年的第一天和最后几天可能会出现在前一周/后一周的年份中。

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

LocalDate ld = LocalDate.of( 2012 , Month.AUGUST , 7 ) ;

Interrogate for the standard week number.询问标准周数。 You can ask for either of these TemporalField objects: IsoFields.WEEK_BASED_YEAR & IsoFields.WEEK_OF_WEEK_BASED_YEAR您可以请求以下任一TemporalField对象: IsoFields.WEEK_BASED_YEAR & IsoFields.WEEK_OF_WEEK_BASED_YEAR

int weekOfWeekBasedYear = ld.get( IsoFields.WEEK_OF_WEEK_BASED_YEAR ) ;
int yearOfWeekBasedYear = ld.get( IsoFields.WEEK_BASED_YEAR ) ;

Dump to console using standard ISO 8601 format of YYYY-Www-D .使用YYYY-Www-D标准ISO 8601 格式转储到控制台。

String isoOutput = yearOfWeekBasedYear + "-W" + String.format("%02d", weekOfWeekBasedYear) + "-" + dayOfWeekNumber  ;
System.out.println( ld + " is ISO 8601 week: " + isoOutput ) ;

See this code run live at IdeOne.com .查看此代码在 IdeOne.com 上实时运行

2012-08-07 is ISO 8601 week: 2012-W32-2 2012-08-07 是 ISO 8601 周:2012-W32-2

By the way, if Android is ever able to run the ThreeTen-Extra library, you'll find its YearWeek class useful.顺便说一句,如果 Android 能够运行ThreeTen-Extra库,您会发现它的YearWeek类很有用。


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.DateCalendar ,和SimpleDateFormat

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 类?

Here you can view the reference by oracle在这里可以查看oracle的参考

https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html

And I have placed a quick solution to find the week count of current day.我已经放置了一个快速解决方案来查找当天的周数。 You can alter and optimize as your way.您可以按照自己的方式进行更改和优化。 Also set your GMT value according to your need.还可以根据需要设置 GMT 值。

public static int getWeeksOfMonth() {

    DATESTATUS = false;
    VALUESTATUS = false;
    int weekCount;
    WEEK_OF_MONTH= -1;

    // get the supported ids for GMT+04:00 (Pacific Standard Time)
    String[] ids = getAvailableIDs(4 * 60 * 60 * 1000);
    // if no ids were returned, something is wrong. get out.
    if (ids.length == 0)
        return WEEK_OF_MONTH;

    // create a Pacific Standard Time time zone
    SimpleTimeZone pdt = new SimpleTimeZone(4 * 60 * 60 * 1000, ids[0]);

    // create a GregorianCalendar with the Pacific Daylight time zone
    // and the current date and time
    Calendar calendar = new GregorianCalendar(pdt);
    Date trialTime = new Date();
    calendar.setTime(trialTime);

    weekCount = calendar.get(Calendar.WEEK_OF_YEAR);

    return recursiveWeekCountCheck(calendar, weekCount);
}

private static int recursiveWeekCountCheck(Calendar calendar, int weekCount) {
    if (calendar.get(Calendar.MONTH) == Calendar.DECEMBER && weekCount == 1) {
        DATESTATUS = true;
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        weekCount = calendar.get(Calendar.WEEK_OF_YEAR);
        recursiveWeekCountCheck(calendar, weekCount);
    }
    if (!VALUESTATUS){
        VALUESTATUS = true;
        if (DATESTATUS) {
            weekCount++;
            WEEK_OF_MONTH = weekCount;
        } else {
            WEEK_OF_MONTH = weekCount;
        }
    }
    return WEEK_OF_MONTH;
}

At the end just call the method getWeeksOfMonth();最后只需调用方法 getWeeksOfMonth();

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

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