2017-01-04 8 views
0

SimpleDateFormatを使用して日付を書式設定するときに、日付と月が正しく取得されます。しかし、今年は1つ減らされます。何が問題なの?Androidの日付形式のエラー

yyyy-mm-dd HH:MM:SS 

それはそう

yyyy-MM-dd HH:mm:ss 

あなた見当違いのようになります。

public static String getFormattedDate(String date) { 

    SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-mm-dd HH:MM:SS"); 
    Date testDate = null; 
    String newFormat="space"; 

    try { 
     testDate = sourceFormat.parse(date); 
     SimpleDateFormat formatter = new SimpleDateFormat("d/MMM/yyyy"); 
     newFormat = formatter.format(testDate); 
     Log.i("Formatted Date",newFormat); 
    }catch(Exception ex){ 
     ex.printStackTrace(); 
    } 

    return newFormat; 
} 

実際の日付と書式設定された日付は、あなたがここにmistkeを持っているためです

01-04 15:34:00.233 21858-21858/com.cube_me.cubeme I/Actual Date: 2016-12-19 00:00:00 
01-04 15:34:00.233 21858-21858/com.cube_me.cubeme I/Formatted Date: 19/Dec/2015 
+1

あなたのソースフォーマットである4桁の年、2桁の分、2桁の日月、2桁の時間、2桁の月、2桁のミリ秒です。それは本当にあなたが望むものですか? IMHO yyyy-MM-dd HH:mm:ss "' - >日付形式は大文字と小文字が区別されます。 –

+0

分と秒を小文字に変更して問題を修正しました。答えてくれてありがとう。 –

答えて

-1

文字テーブルを使用する

SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS"); 

代わりの

SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-mm-dd HH:MM:SS");