Convert a date format in epoch

user804979 picture user804979 · Jul 14, 2011 · Viewed 133.4k times · Source

I have a string with a date format such as

Jun 13 2003 23:11:52.454 UTC

containing millisec... which I want to convert in epoch. Is there an utility in Java I can use to do this conversion?

Answer

Bohemian picture Bohemian · Jul 14, 2011

This code shows how to use a java.text.SimpleDateFormat to parse a 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 epoch time in milliseconds.