2012-12-13 22 views
4

我有一個字符串格式我真的不認識: 字符串收到:「36872 12月12日15:35」 而這個日期的窗口表示是:「12/12/2012 5:35 PM「字符串日期在紀元時間格式在Java

所以首先,我猜測日期中有些東西已經損壞。 第二,我期待這個字符串解析到一個劃時代的時間格式

是這樣的:「1355371390142」(實際上是「二零一二年十二月十三日6時03分10秒」)

+1

來想想,日期格式(YYYY-MM-DD HH:MM:SS)將是多麼的作爲其他... – Elad

回答

3

你可以格式Date S IN的Java這樣的:

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

+0

當前java日期time epoch format可以通過long epoch = System.currentTimeMillis()獲得;通過System.out.println(「」+ new Date(1416306836));將其轉換回java日期時,將獲得結果爲1416306363 。獲得星期六1月17日16:55:06 SGT 1970,不是當前日期 它的工作原理 – Apache