tl;dr
ZonedDateTime
.parse(
"Oct 24 2019 12:00:00.000 AM UTC" ,
DateTimeFormatter.ofPattern( "MMM d uuuu hh:mm:ss.SSS a z" ).withLocale( Locale.US )
)
.toInstant()
.toEpochMilli()
1571875200000
java.time
Two problems:
- As commented by Heuberger, you are using incorrect formatting codes.
- You are using terrible date-time classes supplanted years ago by the modern java.time classes.
Your inputs, renamed for clarity.
String inputStart = "Oct 24 2019 12:00:00.000 AM UTC";
String inputStop = "Oct 24 2019 11:59:59.000 PM UTC";
Define a formatting pattern to match your inputs.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MMM d uuuu hh:mm:ss.SSS a z" ).withLocale( Locale.US );
Parse inputs.
ZonedDateTime zdtStart = ZonedDateTime.parse( inputStart , f );
ZonedDateTime zdtStop = ZonedDateTime.parse( inputStop , f );
Calculate elapsed time. We should get one second less than 24 hours.
Duration d = Duration.between( zdtStart , zdtStop );
Apparently you want the count of milliseconds since the epoch reference of first moment of 1970 in UTC, 1970-01-01T00:00Z. First extract the building-block class, Instant
, from each ZonedDateTime
. An Instant
represents a moment in UTC. This class lets you interrogate for the count since epoch. Note that java.time classes resolve to nanoseconds. So asking for milliseconds can result in data loss, ignoring any microseconds or nanoseconds.
Instant start = zdtStart.toInstant() ;
Instant stop = zdtStop.toInstant() ;
long millisStart = start.toEpochMilli() ;
long milliStop = stop.toEpochMilli() ;
Dump to console.
System.out.println( "zdtStart = " + zdtStart );
System.out.println( "zdtStop = " + zdtStop );
System.out.println( "d = " + d );
System.out.println( "start = " + start );
System.out.println( "stop = " + stop );
System.out.println( "millisStart = " + millisStart );
System.out.println( "milliStop = " + milliStop );
zdtStart = 2019-10-24T00:00Z[UTC]
zdtStop = 2019-10-24T23:59:59Z[UTC]
d = PT23H59M59S
start = 2019-10-24T00:00:00Z
stop = 2019-10-24T23:59:59Z
millisStart = 1571875200000
milliStop = 1571961599000

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
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
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.