tl;dr
ZonedDateTime.of( 2014 , 2 , 20 , 13 , 18 , 48 , 0 , ZoneId.of( "Pacific/Auckland" ) )
2014-02-20T13:18:48+05:30[Asia/Kolkata]
java.time
Use modern java.time classes that have sane numbering. Avoid the troublesome poorly-designed old date-time classes that are now legacy, supplanted by the java.time classes.
No crazy numbering in java.time:
2014
means the year 2014. No funky math with 1900.
2
means February, 1-12 for January-December.
1
means Monday, 1-7 for Monday-Sunday per ISO 8601 standard.
So, if you want early afternoon on March 20, 2014 in India time zone:
ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
ZonedDateTime zdt = ZonedDateTime.of( 2014 , 2 , 20 , 13 , 18 , 48 , 0 , z ) ;
zdt.toString(): 2014-02-20T13:18:48+05:30[Asia/Kolkata]
Or build in pieces.
LocalDate ld = LocalDate.of( 2014 , 2 , 20 ) ;
LocalTime lt = LocalTime.of( 13 , 18 , 48 ) ;
ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;
Or, better, use the Month
enum objects pre-defined, one for each month of the year. Tip: Use these Month
objects throughout your codebase rather than a mere integer number to make your code more self-documenting, ensure valid values, and provide type-safety.
LocalDate ld = LocalDate.of( 2014 , Month.FEBRUARY , 20 ) ;
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
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.
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.