0

カウントダウンウィジェットに取り組んでいます。問題は以下で説明されています

'2012-07-04T15:00:00Z' - >  '1341414000000'

 '1341414000000' - > indicate 2012 july 4th 20:30 

なぜこれが起こったのですか?. ジョダを使っています

final String format = "yyyy-MM-dd'T'HH:mm:ssZ";

DateTimeFormatter formatter = DateTimeFormat.forPattern(format);
            DateTime endTime = formatter.parseDateTime(strDate);
                long diff=endTime.getMillis();
4

2 に答える 2

0

これは古い質問のように思えますが、他の人がこれを見つけるかもしれないので、とにかく答えます.

Joda には ISO 8601 形式のクラスがあるため、形式を手動で指定する代わりに、次のようにそのクラスを使用できます。

import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import org.joda.time.DateTime;

String strDate = "2012-07-04T15:00:00Z";

DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis();
DateTime endTime = formatter.parseDateTime(strDate);
long diff=endTime.getMillis();

一方、あなたが抱えていると思われる問題は、タイムゾーンに関連しています。ミリ秒から日付文字列に戻すと、ローカル タイム ゾーンを使用して変換されます。日付を UTC として取得する場合は、次の手順を実行する必要があります。

import org.joda.time.DateTimeZone;
import org.joda.time.DateTime;

DateTime dt = new DateTime(1341414000000).withZone(DateTimeZone.UTC);

期待どおり 2012-07-04T15:00:00.000Z が返されます。ミリ秒なしでフォーマットしたい場合は、以前と同じフォーマッターを使用できます。

import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import org.joda.time.DateTime;

DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis();
DateTime dt = new DateTime(1341414000000).withZone(DateTimeZone.UTC);
formatter.print(dt)

そして、2012-07-04T15:00:00Z を返します。

于 2013-08-21T02:50:59.373 に答える
0
String time="2012-07-04T15:00:00Z";
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
        df.setTimeZone(TimeZone.getTimeZone("UTC"));
      //  time.replace("Z","");
        try {
            Date date=df.parse(time);
            long diff=date.getTime()-System.currentTimeMillis();
            System.out.println("Date "+diff);
        } catch (ParseException e) {

            e.printStackTrace();
        }
于 2012-07-03T18:17:43.497 に答える