2013-07-16 58 views
1

我在字符串的日期使用這一個我:如何格式化日期android?

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm"); 
dataUltima = sdf.format(sdf); 

格式化的日期。但是這個方法返回系統的實際日期。我怎樣才能在不改變日期的情況下格式化日期。

Obs。我碰到一個服務器這個日期,它來找我YYY-MM-DD,我想下面這dd/MM/yyyy

+0

什麼是從服務器獲取日期的類型? – pooyan

+0

我得到一個字符串形式的服務器。 – Roland

回答

1

我不知道你有什麼問題。

試試吧。也許幫忙。

String server_format = "2013-01-02"; //server comes format ? 
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
try { 

    Date date = sdf.parse(server_format); 
    System.out.println(date); 
    String your_format = new SimpleDateFormat("dd/MM/yyyy").format(date); 
    System.out.println(your_format); //you want format ? 

} catch (ParseException e) { 
    System.out.println(e.toString()); //date format error 
} 
+0

它的工作,謝謝。 – Roland

0

泰:

try 
{ 
    //create SimpleDateFormat object with source string date format 
    SimpleDateFormat sdfSource = new SimpleDateFormat("yyy-dd-MM"); 

    //parse the original date string(strDate) that you are getting from server into Date object 
    Date date = sdfSource.parse(strDate); 

    //create SimpleDateFormat object with desired date format 
    SimpleDateFormat sdfDestination = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss"); 

    //parse the date into another format 
    strDate = sdfDestination.format(date); 

    System.out.println("Date is converted"); 
    System.out.println("Converted date is : " + strDate); 

} 
catch(ParseException pe) 
{ 
    System.out.println("Parse Exception : " + pe); 
} 

來源:Convert date string from one format to another format using SimpleDateFormat

希望這有助於。

+0

我收到一個錯誤,nullpointexception 07-16 14:41:10.709:E/AndroidRuntime(28291):導致:java.lang.NullPointerException 07-16 14:41:10.709:E/AndroidRuntime(28291): \t at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1001) 07-16 14:41:10.709:E/AndroidRuntime(28291):\t at java.text.DateFormat.parse(DateFormat.java:624) 07-16 14:41:10.709:E/AndroidRuntime(28291):\t at com.example.Tappa.SegundaTelaActivity.onCreate(SegundaTelaActivity.java:171) – Roland

+0

你的活動中有哪條「171」行?還要確保'strDate'不是null,然後再將其傳遞給此日期以進行轉換。你記錄來自服務器的日期嗎?它是否爲非空? –

0

試試這個:

String date = "2011-11-12 11:11:11"; //Fill with sample date received from server 
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-dd-MM HH:mm:ss"); 
Date testDate = null; 
try { 
    testDate = sdf.parse(date); 
}catch(Exception ex){ 
    ex.printStackTrace(); 
} 
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm"); 
String dataUltima = formatter.format(testDate); 
System.out.println("Date is "+dataUltima); 
+0

我收到了一個錯誤:java.lang.IllegalArgumentException 07-16 14:50:00.239:E/AndroidRuntime(29237):\t at java.text.DateFormat.format(DateFormat.java:365) 07-16 14:50:00.239:E/AndroidRuntime(29237):\t位於java.text.Format.format(Format.java:93) – Roland

+0

什麼是您的服務器日期作爲?如果從服務器獲得的日期只有任何時間組件的形式爲「yyy-dd-MM」,這對我有用。這是怎麼來的?如果沒有,它會拋出一個錯誤 – Slartibartfast

+0

它作爲一個字符串來爲我:「yyyy-dd-MM HH:mm:ss」,我想它dd/MM/yyy HH:mm – Roland

0

試試這個代碼:

Date newDate = new Date(); 

    // Print the date with the server format 
    SimpleDateFormat sdfServer = new SimpleDateFormat("yyy-dd-MM "); 
    System.out.println("Server date " + sdfServer.format(newDate)); 

    // Create a new formatter to get the date in the format you want 
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm"); 
    String dataUltima = sdf.format(newDate); 
    System.out.println("System date " + dataUltima);