tl;dr
LocalDate.of( 2014 , Month.NOVEMBER , 22 )
.atStartOfDay( ZoneId.of ( "Asia/Kolkata" ) )
.toInstant()
.toEpochMilli()
Details
The Answer by Jon Skeet is correct and wise.
Here is some example code using java.time classes.
LocalDate
The LocalDate
class represents a date-only value without time-of-day and without time zone.
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
You can specify a LocalDate
.
LocalDate ld = LocalDate.of( 2014 , Month.NOVEMBER , 22 );
ld.toString(): 2014-11-22
ZonedDateTime
You desire a count-of-milliseconds-since-epoch. I do not recommend using a count-from-epoch for handling date-time. But if you insist, here we go.
A count-from-epoch of milliseconds means we need a date and a time-of-day. We have a date. I assume you want the first moment of the day as the time-of-day. Do not assume this time is 00:00:00
. Anomalies such as Daylight Saving Time (DST) may mean the day starts at some other time such as 01:00:00
. Let java.time determine that first moment by generating a ZonedDateTime
object from a LocalDate
.
LocalDate ld = LocalDate.of ( 2014 , Month.NOVEMBER , 22 );
ZoneId z = ZoneId.of ( "Asia/Kolkata" );
ZonedDateTime zdt = ld.atStartOfDay ( z );
zdt.toString(): 2014-11-22T00:00+05:30[Asia/Kolkata]
Instant
You can extract an Instant
if desired. The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction). A developer should think of UTC as the One True Time, and not think about their own parochial time zone while working.
Instant instant = zdt.toInstant ();
instant.toString(): 2014-11-21T18:30:00Z
From the instant we can ask for a count of milliseconds since epoch. Be aware this involves data-loss as any nanoseconds will be truncated to milliseconds.
long epochMillis = instant.toEpochMilli();
java.util.Date
I suggest avoiding the legacy date-time classes. But if you must, you can convert. Look to new methods added to the old classes like java.util.Date
.
java.util.Date utilDate = java.util.Date.from( instant );
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
, & java.text.SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
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?
- Java SE 8 and SE 9 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 SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
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.