在纪元时间中的字符串格式在Java中

时间:2012-12-13 12:53:20

标签: java date time format epoch

我有一个我不认识的格式的字符串: 收到的字符串:“36872 12月12日15:35” 而此日期的窗口代表是:“12/12/2012 5:35 PM”

所以首先,我猜测日期中有些东西被破坏了。 第二,我希望将这个字符串解析成一个纪元时间格式

类似这样:“1355371390142”(实际上是“2012-12-13 06:03:10”)

1 个答案:

答案 0 :(得分:3)

您可以像这样格式化Java中的Date

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()); 

有关遮罩选项,请参阅此选项:http://docs.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html