2011-09-28 25 views
1

我有一個問題,我有日期字符串格式爲「2011-09-28T10:33:53.694 + 0000」。我想在MM/DD/YYYY中更改此日期格式,不知道如何?請建議我爲正確的結果。轉換特定格式的DateString

回答

0
String string = "2011-09-28T10:33:53.694+0000"; 
SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.ENGLISH); 
SimpleDateFormat outFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH); 
Date date; 
String output; 
try { 
    date = inFormat.parse(string); 
    output = outFormat.format(date); // 28/09/2011 
} catch (ParseException e) { 
    e.printStackTrace(); 
} 
1

就是這樣。檢查「from」格式的詳細信息。

DateFormat from = new SimpleDateFormat("yyyy-MM-ddThh:mm:ss"); // not sure about the details 
from.setLenient(false); 
DateFormat to = new SimpleDateFormat("MM/dd/yyyy"); 
to.setLenient(false); 
Date d = from.parse(dateStr); 
System.out.println(to.format(d)); 
0

這樣來做---->

  1. 創建對象Date類 日期日期=新的日期();
  2. 創建簡單日期格式對象類 SimpleDateFormat sdf = new SimpleDateFormat(「MM/dd/yyyy」); 注意 - > MM - 會給出月份的字符,dd和yyyy會給出數字的日期和年份
  3. 將日期轉換爲字符串 String s = sdf.format(date);

,你用它做...

相關問題