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