您可以使用DateTime
class plusxx()
methods;
public static void main(String[] args) {
String dateTime = "23.02.2015 14:06:30 +01:00";
DateTimeFormatter dtf = DateTimeFormat.forPattern("dd.MM.yyyy HH:mm:ss Z");
DateTime jodatime = dtf.parseDateTime(dateTime);
DateTimeFormatter dtfOut = DateTimeFormat.forPattern("dd.MM.yyyy HH:mm:ss");
System.out.println(dtfOut.print(jodatime));
DateTime resultDateTime = jodatime.plusHours(1);
System.out.println(dtfOut.print(resultDateTime));
}
而輸出是;
23.02.2015 08:36:30
23.02.2015 09:36:30
結果日期沒有錯,關於這個問題的更多信息請看here。
對於固定日期,我添加了時區withZone(DateTimeZone.forID("Europe/Prague")
;
public static void main(String[] args) {
String dateTime = "23.02.2015 14:06:30 +01:00";
DateTimeFormatter dtf = DateTimeFormat.forPattern("dd.MM.yyyy HH:mm:ss Z");
DateTime jodatime = dtf.withZone(DateTimeZone.forID("Europe/Prague")).parseDateTime(dateTime);
DateTimeFormatter dtfOut = DateTimeFormat.forPattern("dd.MM.yyyy HH:mm:ss");
System.out.println(dtfOut.withZone(DateTimeZone.forID("Europe/Prague")).print(jodatime));
DateTime resultDateTime = jodatime.plusHours(1);
System.out.println(dtfOut.withZone(DateTimeZone.forID("Europe/Prague")).print(resultDateTime));
}
而且輸出是;
23.02.2015 14:06:30
23.02.2015 15:06:30
您的解決方案是哪一個?你爲什麼不認爲這是一個好主意? – 2015-02-23 13:34:29
「23.02.2015 14:06:30 +01:00」與「23.02.2015 15:06:30 +02:00」同時(即時),所以你想操作時區偏移嗎?你還知道爲了得到UTC時間而必須減去正的偏移量嗎? – 2015-02-23 13:47:01
_updated我的問題,並添加了一些解釋,我到目前爲止做了什麼_ – ccDict 2015-02-24 12:26:55