2013-04-03 66 views
-1
/Date(13863838400000-0400)/ 

我得到如上的日期格式,我将如何显示在MM-DD-YY中。以下是我的尝试。如何解析日期字符串到日期?

public static void main(String[] args) throws Exception { 
    String target = "/Date(13863838400000-0400)/"; 
    DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy", Locale.ENGLISH); 
    Date result = df.parse(target); 
    System.out.println(result); 
} 

我没有得到解析日期...

+7

你会如何*期望*的是'SimpleDateFormat'解析你给它的文本?它看起来不像您提供的格式。 –

+0

DateFormat模式必须表示您想要解析的格式,而不是预期的结果 –

+1

我认为输入日期是时间(以毫秒为单位),然后是时区 –

回答

1

在给定的附加信息

String target = "/Date(13863838400000-0400)/"; 
long millis = Long.parseLong(target.substring(target.indexOf("(") + 1, 
     target.indexOf("-"))); 
DateFormat df = new SimpleDateFormat("MM-dd-yyyy", Locale.ENGLISH); 
System.out.println(df.format(new Date(millis))); 
+0

这不考虑-0400。 – Joe2013

1

看起来日期以毫秒(LONG)提供。因此,如使用下面

 Date result = new Date(13863838400000l-0400); 
     System.out.println(result); 
+0

我将如何删除/日期(13863838400000-0400)/,以便我以毫秒为单位 – Kevin

+1

有很多方法,最简单的方法是子字符串..但请确保你确认了这个确实是毫秒的源代码String target =“/ Date(13863838400000-0400)/”; (Long.parseLong(target.substring(6,target.length() - 7)) - Long.parseLong(target.substring(target.length() - 6,target.length())返回页首返回页首返回页首返回页首返回页首返回页首返回页首返回页首-2))); \t \t System.out.println(result); – Joe2013