2012-03-30 49 views
47

我想解析一個字符串到android應用程序中的日期字段,但我似乎無法得到它正確。這是我試圖轉換爲日期「03/26/2012 11:49:00 AM」的字符串。我使用的功能是:在java中將字符串轉換爲日期

private Date ConvertToDate(String dateString){ 
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa"); 
    Date convertedDate = new Date(); 
    try { 
     convertedDate = dateFormat.parse(dateString); 
    } catch (ParseException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return convertedDate; 
} 

但我不斷收到3/1/112上午11點49分的結果..任何幫助,我會很感激。感謝

+2

在哪裏出現「3/1/112 11:49 AM」?返回的值是一個Date,而不是一個字符串,所以你必須做* something *才能看到它作爲字符串結果... – 2012-03-30 14:38:49

+0

我看到2012年3月26日星期一11:49:00作爲輸出。 – 2012-03-30 14:40:25

+0

嘗試使用dateFormat.setLenient(true)將解析設置爲寬鬆,然後檢查解析的結果 – manub 2012-03-30 14:40:41

回答

103

你錯了你展示我猜數據的方式,因爲對我來說:

String dateString = "03/26/2012 11:49:00 AM"; 
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa"); 
    Date convertedDate = new Date(); 
    try { 
     convertedDate = dateFormat.parse(dateString); 
    } catch (ParseException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    System.out.println(convertedDate); 

打印:

Mon Mar 26 11:49:00 EEST 2012 
+0

大家都說得對。它正確轉換,但我使用Date.getYear(),getMonth()和我正在使用不正確的所有。感謝您的幫助。 – bflosabre91 2012-03-30 14:56:45

4
String str_date="13-09-2011"; 
DateFormat formatter ; 
Date date ; 
formatter = new SimpleDateFormat("dd-MM-yyyy"); 
date = (Date)formatter.parse(str_date); 
System.out.println("Today is " +date.getTime()); 

試試這個

17

它去確定當我用Locale.US參數在SimpleDateFormat

String dateString = "15 May 2013 17:38:34 +0300"; 
System.out.println(dateString); 

SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss Z", Locale.US); 
DateFormat targetFormat = new SimpleDateFormat("dd MMM yyyy HH:mm", Locale.getDefault()); 
String formattedDate = null; 
Date convertedDate = new Date(); 
try { 
    convertedDate = dateFormat.parse(dateString); 
System.out.println(dateString); 
formattedDate = targetFormat.format(convertedDate); 
} catch (ParseException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
System.out.println(convertedDate); 
2

這段代碼將幫助你做出如FEB 17 20:49的結果。

String myTimestamp="2014/02/17 20:49"; 

    SimpleDateFormat form = new SimpleDateFormat("yyyy/MM/dd HH:mm"); 
    Date date = null; 
    Date time = null; 
    try 
    { 
     date = form.parse(myTimestamp); 
     time = new Date(myTimestamp); 
     SimpleDateFormat postFormater = new SimpleDateFormat("MMM dd"); 
     SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); 
     String newDateStr = postFormater.format(date).toUpperCase(); 
     String newTimeStr = sdf.format(time); 
     System.out.println("Date : "+newDateStr); 
     System.out.println("Time : "+newTimeStr); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 

結果:

日期:2月17日

時間:20時49

0
GregorianCalendar date; 

CharSequence dateForMart = android.text.format.DateFormat.format("yyyy-MM-dd", date); 

Toast.makeText(LogmeanActivity.this,dateForMart,Toast.LENGTH_LONG).show(); 
1
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); 
String dateInString = "07/06/2013"; 

try { 

    Date date = formatter.parse(dateInString); 
    System.out.println(date); 
    System.out.println(formatter.format(date)); 

} catch (ParseException e) { 
    e.printStackTrace(); 
} 

輸出:

2014/08/06 16:06:54 
2014/08/06 16:06:54