Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String to Date in epoch time Format in Java

i have a string in a format i don't really recognize: string received: "36872 Dec 12 15:35" while the windows representation for this date is: "12/12/2012 5:35 PM"

so first, I'm guessing something was corrupted in the date. second, I'm looking to parse this string to a epoch time format

something like this: "1355371390142" (which is actually "2012-12-13 06:03:10")

like image 582
Elad Avatar asked Oct 22 '22 22:10

Elad


1 Answers

You can format Dates in Java like this:

String str = "12/12/2012 5:35 PM"; //Your String containing a date
DateFormat dF = new SimpleDateFormat("dd//MM/yyyy hh:mm a"); // The mask
// 'a' value in the Mask represents AM / PM - h means hours in AM/PM mode
Date date = dF.parse(str); // parsing the String into a Date using the mask

//Date.getTime() method gives you the Long with milliseconds since Epoch.
System.out.println("Epoch representation of this date is: " + date.getTime()); 

Refer to this for mask options: http://docs.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html

like image 72
Andrea Ligios Avatar answered Oct 29 '22 14:10

Andrea Ligios