Week of year calculation differences between Java and Python
I have two parts of my implementation, one in Python and one in Java. Now the problem is I realized their calculations of week number of the year don't match. For example:
Java code:
private static int getWeekOfYear(int y, int m, int d) {
Calendar cal = Calendar.getInstance();
cal.setMinimalDaysInFirstWeek(4);
cal.set(y, m, d);
retur开发者_JAVA技巧n cal.get(Calendar.WEEK_OF_YEAR);
}
System.out.println(getWeekOfYear(2010, 7, 1));
Java result: 31
Python code:
print datetime(2010, 7, 1, 0, 0).isocalendar()[1]
Python result: 26
Now how can I get these to be the same? I'd like Java to follow exactly python's calculation by the way.
Months in Java are 0-based, you need this:
cal.set(y, m - 1, d);
EDIT:
As noted by jarnbjo, Calendar
should be configured to produce ISO 8601 week numbers as follows:
cal.setMinimalDaysInFirstWeek(4);
cal.setFirstDayOfWeek(Calendar.MONDAY);
The issue with the odd month numbering in Java is not the entire solution. Java uses localized rules to calculate the week number (different countries have different rules regarding which weekday is the first and which week is the first of the year). Python's isocalendar function uses the week numbering rules according to ISO 8601, which for these purposes defines that a week starts on Monday and that January 4th is always in week number 1.
tl;dr
LocalDate.of( 2010 , Month.JULY , 1 )
.get (
IsoFields.WEEK_OF_WEEK_BASED_YEAR
)
26
Details
As other noted, your month argument was incorrect because of the crazy counting from zero done in that Calendar
class.
Also noted was that Calendar
class definition of "week" varies by Locale
rather than using the ISO 8601 standard week definition used in your Python code.
java.time
Another issue: You are using terribly troublesome old date-time classes bundled with the earliest version of Java. They are now legacy, supplanted by the java.time classes.
For a date-only value, without time-of-day and without time zone, use LocalDate
. Use the handy Month
enum for readability.
LocalDate ld = LocalDate.of( 2010 , Month.JULY , 1 ) ;
Or use month number, with sane numbering 1-12 for January-December.
LocalDate ld = LocalDate.of( 2010 , 7 , 1 ) ;
Calculate the standard ISO 8601 week number. Use the LocalDate::get
method to access a relevant value. Pass the enum element IsoFields.WEEK_OF_WEEK_BASED_YEAR
to indicate your desired value.
int w = ld.get( IsoFields.WEEK_OF_WEEK_BASED_YEAR ) ;
YearWeek
Tip: If you are doing much work with this week-of-year, you may find helpful the YearWeek
class found in the ThreeTen-Extra project.
YearWeek yw = YearWeek.from( ld ) ;
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
- Java SE 8, Java SE 9, Java SE 10, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
Check the javadoc for the set
method. You'll see the following:
Parameters:
year - the value used to set the YEAR calendar field.
month - the value used to set the MONTH calendar field. Month value is 0-based. e.g., 0 for January.
date - the value used to set the DAY_OF_MONTH calendar field.
So your code should become the following:
private static int getWeekOfYear(int y, int m, int d) {
Calendar cal = Calendar.getInstance();
cal.setMinimalDaysInFirstWeek(4);
cal.set(y, m - 1, d); // Note the change here
return cal.get(Calendar.WEEK_OF_YEAR);
}
In Java 7
is not July, it's August.
精彩评论