2017-01-12 70 views
0

當我運行這段代碼我得到意想不到的結果:斯卡拉日期時間怪異的結果

var DateFormat_in = new SimpleDateFormat("mm/dd/yyyy HH:mm:ss"); 
    var DateFormat_out = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss"); 
    var m ="4/29/2016 12:12:12" 
    println(str_timestamp(m,DateFormat_in,DateFormat_out)) // third 
    def str_timestamp(date_str:String,format_in:SimpleDateFormat,format_out: SimpleDateFormat): DateTime = 
    { 
    val date = format_in.parse(date_str) 
    val date_modified_str = format_out.format(date) 
    println(date_modified_str) // first 
    val temp = format_out.parse(date_modified_str); 
    println(temp)  // second 
    val dateTime: DateTime = new DateTime(temp.getTime) 
    dateTime 
    } 

我得到以下幾點:

2016年12月29日12時12分十二秒

週五01月29日12點12分十二秒CET 2016

2016-01-29T12:12:12.000 + 01:00

爲什麼這個月從一個變爲一個?如何解決它?

回答

6

請諮詢SimpleDateFormat的javadoc。月份的正確模式符號是「M」(大寫字母),分鐘「m」(小)。