2012-07-03 62 views
0

我工作的倒計時插件.The下面Android的日期爲米勒斯(ISO 8601)

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

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

問題解釋了爲什麼這個happend? 。現在用喬達

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

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

你在哪裏找到?你在哪個地方運行它?最有可能的是,解析發生在您的本地,但轉換回格林威治標準時間 - 或反過來。 –

+0

我在印度,我從​​我們那裏得到 – Bytecode

回答

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(); 
     } 
0

這似乎是一個老問題,但無論如何,我會回答這個問題,因爲其他人可能會發現這一點。

在喬達也爲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(); 

在另一方面,你似乎是問題與時區有關。當您從millis轉換回日期字符串時,將使用本地時區進行轉換。如果你想獲得的日期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。