Convert date format to era

I have a string with a date format like

Jun 13 2003 23:11:52.454 UTC 

containing milliseconds ... which I want to convert to an era. Is there a utility in Java that I can use for this conversion?

+46
java date datetime epoch
Jul 14 '11 at 1:13
source share
4 answers

This code shows how to use java.text.SimpleDateFormat to parse java.util.Date from a string:

 String str = "Jun 13 2003 23:11:52.454 UTC"; SimpleDateFormat df = new SimpleDateFormat("MMM dd yyyy HH:mm:ss.SSS zzz"); Date date = df.parse(str); long epoch = date.getTime(); System.out.println(epoch); // 1055545912454 

Date.getTime() returns the time of an era in milliseconds.

+79
Jul 14 '11 at 1:25
source share

You can also use the new Java 8 API

 import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; public class StackoverflowTest{ public static void main(String args[]){ String strDate = "Jun 13 2003 23:11:52.454 UTC"; DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMM dd yyyy HH:mm:ss.SSS zzz"); ZonedDateTime zdt = ZonedDateTime.parse(strDate,dtf); System.out.println(zdt.toInstant().toEpochMilli()); // 1055545912454 } } 

The DateTimeFormatter class replaces the old SimpleDateFormat . Then you can create a ZonedDateTime from which you can extract the desired era time.

The main advantage is that you are now thread safe.

Thanks to Vasily Burk for his comments and suggestions. Read his answer for full details.

+19
Jun 14 '15 at 13:43 on
source share

TL; DR

 ZonedDateTime.parse( "Jun 13 2003 23:11:52.454 UTC" , DateTimeFormatter.ofPattern ( "MMM d uuuu HH:mm:ss.SSS z" ) ) .toInstant() .toEpochMilli() 

1055545912454

java.time

This answer extends to Lockni's answer .

DateTimeFormatter

First, define a formatting pattern that matches your input string by creating a DateTimeFormatter object.

 String input = "Jun 13 2003 23:11:52.454 UTC"; DateTimeFormatter f = DateTimeFormatter.ofPattern ( "MMM d uuuu HH:mm:ss.SSS z" ); 

ZonedDateTime

Parse the string as ZonedDateTime . You can view this class as: ( Instant + ZoneId ).

 ZonedDateTime zdt = ZonedDateTime.parse ( "Jun 13 2003 23:11:52.454 UTC" , f ); 

zdt.toString (): 2003-06-13T23: 11: 52.454Z [UTC]

Count-out era

I do not recommend tracking date-time values ​​as count-from- epoch . This makes debugging difficult, as people cannot recognize a significant date-time from a number, so invalid / unexpected values ​​can slip through. In addition, such calculations are ambiguous, in granularity (whole seconds, milli, micro, nano, etc.) and in the era (at least two dozen different computer systems).

But if you insist, you can get a millisecond score from the era of the first moment of 1970 in UTC ( 1970-01-01T00:00:00 ) through Instant . Remember that this means data loss when you truncate any nanoseconds to milliseconds.

 Instant instant = zdt.toInstant (); 

instant.toString (): 2003-06-13T23: 11: 52.454Z

 long millisSinceEpoch = instant.toEpochMilli() ; 

1055545912454

About java.time

The java.time framework is built into Java 8 and later. These classes supersede the nasty old time classes such as java.util.Date , .Calendar and java.text.SimpleDateFormat .

The Joda-Time project, now in maintenance mode , advises switching to java.time.

To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations.

Most of the functionality of java.time is ported to Java 6 and 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use ... ).

The ThreeTen-Extra project extends java.time with additional classes. This project is a proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and more .

+4
Oct. 06 '16 at 20:21
source share

Create a generic String to Date format conversion method

  public static void main(String[] args) throws Exception { long test=ConvertStringToDate("May 26 10:41:23","MMM dd hh:mm:ss"); long test2=ConvertStringToDate("Tue, Jun 06 2017, 12:30 AM","EEE, MMM dd yyyy, hh:mm a"); long test3=ConvertStringToDate("Jun 13 2003 23:11:52.454 UTC","MMM dd yyyy HH:mm:ss.SSS zzz"); } private static long ConvertStringToDate(String dateString,String format) { try { return new SimpleDateFormat(format).parse(dateString).getTime(); } catch (ParseException e) { } return 0; } 
0
Jun 07 '17 at 6:55
source share



All Articles