2014-01-16 44 views
0

當我嘗試將字符串轉換爲日期的異常:Android的字符串轉換爲日期異常

String da="16-Jan-2014 10:25:00"; 
SimpleDateFormat sdf = new SimpleDateFormat("dd-mmm-yyyy HH:mm:SS", Locale.ENGLISH); 
Date dd = sdf.parse(da); 

什麼錯在我的代碼嗎?

java.text.ParseException: Unparseable date:"16-Jan-2014 10:25:00" (at offset 3) 

在此先感謝...

回答

1

考慮例

String text = "2014-01-17T00:00:00.000-0500"; 
DateFormat sdf = new SimpleDateFormat("yyyy-MMM-dd'T'HH:mm:ss.SSSZ"); 

希望在此之後能夠幫助

+0

這是行不通的。你仍然需要三個M。兩個MM將以這種格式解析日期:16-01-2014。但是給定的格式有三個字符的月份。 –

+0

@Tim Kranen正確我錯過了它:) – nitesh

1

正如@Tim Kranen酒店所提,月份格式爲大寫字母。所以它應該是MMM

要理解格式化幾個月考慮這短短的代碼 -

/* 
    Formatting month using SimpleDateFormat 
    This example shows how to format month using Java SimpleDateFormat class. Month can 
    be formatted in M, MM, MMM and MMMM formats. 
*/ 


import java.text.SimpleDateFormat; 
import java.util.Date; 

public class FormattingMonth { 

    public static void main(String[] args) { 

    //create Date object 
    Date date = new Date(); 

    //formatting month in M format like 1,2 etc 
    String strDateFormat = "M"; 
    SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat); 

    System.out.println("Current Month in M format : " + sdf.format(date)); 

    //formatting Month in MM format like 01, 02 etc. 
    strDateFormat = "MM"; 
    sdf = new SimpleDateFormat(strDateFormat); 
    System.out.println("Current Month in MM format : " + sdf.format(date)); 

    //formatting Month in MMM format like Jan, Feb etc. 
    strDateFormat = "MMM"; 
    sdf = new SimpleDateFormat(strDateFormat); 
    System.out.println("Current Month in MMM format : " + sdf.format(date)); 

    //formatting Month in MMMM format like January, February etc. 
    strDateFormat = "MMMM"; 
    sdf = new SimpleDateFormat(strDateFormat); 
    System.out.println("Current Month in MMMM format : " + sdf.format(date)); 

    } 
} 

/* 
Typical output would be 
Current Month in M format : 2 
Current Month in MM format : 02 
Current Month in MMM format : Feb 
Current Month in MMMM format : February 
* 

檢查Java Date Formatting舉例如上所述理解的格式。