2012-03-17 5 views
4

私はSimpleDateFormatのサブクラスをいくつか作成して、Androidで話しているAzureフィードの処理を簡略化しました。次のようにそれらの一つは、次のとおりです。SimpleDateFormatサブクラスは年に1900を追加します

public class ISO8601TLDateFormat extends SimpleDateFormat { 

    private static String mISO8601T = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; 

    public ISO8601TLDateFormat() { 
     super(mISO8601T); 
    } 

    public ISO8601TLDateFormat(Locale inLocale) { 
     super(mISO8601T, inLocale); 
    } 
} 

あなたは意志が

2012-03-17T00のように見えるの日付を生成または解釈することで見ることができるように:00:何である00.000 + 0100

Azureサービスは期待しています。私は、このようにDatePickerから構築Dateオブジェクト内に送り込むときしかし、:

mDate = new Date(mDatePicker.getYear(), mDatePicker.getMonth(), mDatePicker.getDayOfMonth()); 

ISO8601TLDateFormatの出力は

3912-03-17T00です:00:00.000 + 0100

あなたが見ることができるように、年は私よりも1900以上、誰かが未来からが必要です。私はDateオブジェクトをAzureフィードシステム全体に精査し、その日付が2012年であると報告しました。これは私が予想したものです。 SimpleDateFormatはなぜ壊れていますか?

+0

0, 0, 1ている第一に日付を構築することに留意する必要があります1900年です。 –

+1

私はそこに十分な情報はないと思う...あなたのSimpleDateFormatに行っている特定の呼び出しを知り、間違った出力を生成したり、初期化されているかどうかなどを調べる必要がある。 – FrankieTheKneeMan

+0

私は答えがある(下記)、私は 'Date'オブジェクトをどうやって間違って構築したかを明確にします。 –

答えて

4

問題は、Dateのコンストラクタが期待したものを受け取らないということです。 Javaのdocumentationから撮影:

public Date(int year, 
      int month, 
      int date) 
Deprecated. As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date) or GregorianCalendar(year + 1900, month, date). 
Allocates a Date object and initializes it so that it represents midnight, local time, at the beginning of the day specified by the year, month, and date arguments. 
Parameters: 
year - the year minus 1900. 
month - the month between 0-11. 
date - the day of the month between 1-31. 

あなたはその年のためそれは、 `Date`関与*どこか*があります可能性がありますあなたが1月1900

+0

ありがとうございます - 私は、 'Date#getYear()'も年から1900を引いたものをあなたに与えると仮定します。 (それは確かに私に2012年を与えている)。 –

+0

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Date.html#getYear()。ドキュメントを読んでみると、正しいことがわかります。 –

+0

このリンクを追加してもよいかどうか:http://stackoverflow.com/questions/5621825/how-can-i-utilize-simpledateformat-with-calendar? –

関連する問題