Java – タイムゾーン間の日付と時刻を変換する
このチュートリアルでは、いくつかの例を示します(https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html[ZonedDateTime(Java 8)、http://docs)。 oracle.com/javase/8/docs/api/java/util/Date.html[日付]、http://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html[カレンダ]およびhttp://www.joda.org/joda-time/[Joda Time])を使用して、異なるタイムゾーン間の日付と時刻を変換します。
すべての例は、日付と時刻を
(UTC+8:00) Asia/Singapore - Singapore Time Date : 22-1-2015 10:15:55 AM
に
(UTC-5:00) America/New__York - Eastern Standard Time Date : 21-1-2015 09:15:55 PM
フレームワークはこのライブラリに触発されています)
1. ZonedDateTime
この新しいJava 8の `java.time.ZonedDateTime`を常に使用して、タイムゾーンを含む日付と時刻を表します。
ZonedDateTimeExample.java
package com.mkyong.date; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; public class ZonedDateTimeExample { private static final String DATE__FORMAT = "dd-M-yyyy hh:mm:ss a"; public static void main(String[]args) { String dateInString = "22-1-2015 10:15:55 AM"; LocalDateTime ldt = LocalDateTime.parse(dateInString, DateTimeFormatter.ofPattern(DATE__FORMAT)); ZoneId singaporeZoneId = ZoneId.of("Asia/Singapore"); System.out.println("TimeZone : " + singaporeZoneId); //LocalDateTime + ZoneId = ZonedDateTime ZonedDateTime asiaZonedDateTime = ldt.atZone(singaporeZoneId); System.out.println("Date (Singapore) : " + asiaZonedDateTime); ZoneId newYokZoneId = ZoneId.of("America/New__York"); System.out.println("TimeZone : " + newYokZoneId); ZonedDateTime nyDateTime = asiaZonedDateTime.withZoneSameInstant(newYokZoneId); System.out.println("Date (New York) : " + nyDateTime); DateTimeFormatter format = DateTimeFormatter.ofPattern(DATE__FORMAT); System.out.println("\n---DateTimeFormatter---"); System.out.println("Date (Singapore) : " + format.format(asiaZonedDateTime)); System.out.println("Date (New York) : " + format.format(nyDateTime)); } }
出力
TimeZone : Asia/Singapore Date (Singapore) : 2015-01-22T10:15:55+08:00[Asia/Singapore]TimeZone : America/New__York Date (New York) : 2015-01-21T21:15:55-05:00[America/New__York] ---DateTimeFormatter--- 日付(シンガポール):22-1-2015 10:15:55 AM 日付(ニューヨーク):21-1-2015 09:15:55 PM
-
注意**
このリンクを参照してください://java8/java-8-zoneddatetime-examples/[ZonedDateTime
チュートリアル]より多くのタイムゾーン、カスタムオフセット、夏時間
(DST)の例です。
2.日付
-
注意**
`java.util.Date`はタイムゾーンの概念を持たず、
Unixエポック時間から経過した秒数 –
1970-01-01T00:00:00Z。しかし、Dateオブジェクトを直接印刷すると、
Dateオブジェクトは、デフォルトのシステムタイムゾーンで常に出力されます。
`Date.toString()`ソースコードを確認してください。
2.1タイムゾーンを
DateFormat`に設定し、
java.util.Date`をフォーマットします。
SimpleDateFormat sdfAmerica =新しいSimpleDateFormat( "dd-M-yyyy hh:mm:ss a"); sdfAmerica.setTimeZone(TimeZone.getTimeZone( "America/New__York")); 文字列sDateInAmerica = sdfAmerica.format(日付);
2.2完全な例
DateExample.java
パッケージcom.mkyong.date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; パブリッククラスDateExample { プライベート静的最終文字列DATE__FORMAT = "dd-M-yyyy hh:mm:ss a"; public static void main(String[]args)throws ParseException { SimpleDateFormatフォーマッタ=新しいSimpleDateFormat(DATE__FORMAT); String dateInString = "22-01-2015 10:15:55 AM"; Date date = formatter.parse(dateInString); TimeZone tz = TimeZone.getDefault(); //From TimeZone Asia/Singapore System.out.println("TimeZone : " + tz.getID() + " - " + tz.getDisplayName()); System.out.println("TimeZone : " + tz); System.out.println("Date (Singapore) : " + formatter.format(date)); //To TimeZone America/New__York SimpleDateFormat sdfAmerica = new SimpleDateFormat(DATE__FORMAT); TimeZone tzInAmerica = TimeZone.getTimeZone("America/New__York"); sdfAmerica.setTimeZone(tzInAmerica); 文字列sDateInAmerica = sdfAmerica.format(日付);//最初に文字列に変換します。DateInAmerica = formatter.parse(sDateInAmerica);//新しいDateオブジェクトを作成する System.out.println( "\ nTimeZone:" + tzInAmerica.getID()+ " - " + tzInAmerica.getDisplayName()); System.out.println( "TimeZone:" + tzInAmerica); System.out.println( "日付(ニューヨーク)(文字列):" + sDateInAmerica); System.out.println( "日付(ニューヨーク)(オブジェクト):" + formatter.format(dateInAmerica)); } }
出力
TimeZone:アジア/クアラルンプール - マレーシア時間TimeZone:sun.util.calendar.ZoneInfo[id = "アジア/クアラルンプール"、...]日付(シンガポール):22-1-2015 10:15:55 AM TimeZone:America/New__York - 東部標準時 TimeZone:sun.util.calendar.ZoneInfo[id = "America/New__York"、...]日付(ニューヨーク)(文字列):21-1-2015 09:15:55 PM 日付(ニューヨーク)(オブジェクト):21-1-2015 09:15:55 PM
3.カレンダー
3.1タイムゾーンを設定するカレンダーの例:
カレンダーカレンダー=新しいGregorianCalendar(); calendar.setTime(date); calendar.setTimeZone(tzInAmerica);
非常によくある間違いは、 `java.util.Date`を直接このようにすることです
:
//Wrong, it will display 22-1-2015 10:15:55 AM, time is still in the system default time zone! 日付dateInAmerican = calendar.getTime());
上記の例では、カレンダーで設定した時間帯に関係なく、
Dateオブジェクトは常にデフォルトのシステム時刻で出力されます
ゾーン。 ( `Date.toString()`ソースコードをチェックしてください)
3.2正しい方法は `DateFormat`を使ってそれをフォーマットすることです:
SimpleDateFormat sdfAmerica =新しいSimpleDateFormat( "dd-M-yyyy hh:mm:ss a"); TimeZone tzInAmerica = TimeZone.getTimeZone( "America/New__York"); sdfAmerica.setTimeZone(tzInAmerica); sdfAmerica.format(calendar.getTime())
`calendar.get()`で日付を取得する:
int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH);//Jan = 0、dec = 11 int dayOfMonth = calendar.get(Calendar.DAY__OF__MONTH); int hour = calendar.get(Calendar.HOUR);//12時間クロック int hourOfDay = calendar.get(Calendar.HOUR__OF__DAY);//24時間制 int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); int ampm = calendar.get(Calendar.AM__PM);//0 = AM、1 = PM
3.3完全な例
CalendarExample.java
パッケージcom.mkyong.date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.TimeZone; パブリッククラスCalendarExample { プライベート静的最終文字列DATE__FORMAT = "dd-M-yyyy hh:mm:ss a"; public static void main(String[]args)throws ParseException { SimpleDateFormatフォーマッタ=新しいSimpleDateFormat(DATE__FORMAT); String dateInString = "22-01-2015 10:15:55 AM"; Date date = formatter.parse(dateInString); TimeZone tz = TimeZone.getDefault(); //From TimeZone Asia/Singapore System.out.println("TimeZone : " + tz.getID() + " - " + tz.getDisplayName()); System.out.println("TimeZone : " + tz); System.out.println("Date (Singapore) : " + formatter.format(date)); //To TimeZone America/New__York SimpleDateFormat sdfAmerica = new SimpleDateFormat(DATE__FORMAT); TimeZone tzInAmerica = TimeZone.getTimeZone("America/New__York"); sdfAmerica.setTimeZone(tzInAmerica); カレンダーカレンダー=新しいGregorianCalendar(); calendar.setTime(date); calendar.setTimeZone(tzInAmerica); System.out.println( "\ nTimeZone:" tzInAmerica.getID() " - " tzInAmerica.getDisplayName()); System.out.println( "TimeZone:" tzInAmerica); //Wrong! It will print the date with the system default time zone System.out.println("Date (New York) (Wrong!): " + calendar.getTime()); //Correct! need formatter System.out.println("Date (New York) (Correct!) : " + sdfAmerica.format(calendar.getTime())); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH);//Jan = 0、dec = 11 int dayOfMonth = calendar.get(Calendar.DAY__OF__MONTH); int hour = calendar.get(Calendar.HOUR);//12時間のクロックint hourOfDay = calendar.get(Calendar.HOUR__OF__DAY);//24時間の時計int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); int ampm = calendar.get(Calendar.AM__PM);//0 = AM、1 = PM //Correct System.out.println("\nyear \t\t: " + year); System.out.println("month \t\t: " + month + 1); System.out.println("dayOfMonth \t: " + dayOfMonth); System.out.println("hour \t\t: " + hour); System.out.println("minute \t\t: " + minute); System.out.println("second \t\t: " + second); System.out.println("ampm \t\t: " + ampm); } }
出力
TimeZone:アジア/クアラルンプール - マレーシア時間TimeZone:sun.util.calendar.ZoneInfo[id = "アジア/クアラルンプール"、...]日付(シンガポール):22-1-2015 10:15:55 AM TimeZone:America/New__York - 東部標準時TimeZone:sun.util.calendar.ZoneInfo[id = "America/New__York"、...]]日付(ニューヨーク)(間違った!):Thu Jan 22 10:15:55 MYT 2015日付(ニューヨーク)(正解!):21-1-2015 09:15:55 PM 年:2015 月:01 dayOfMonth:21 時間:9 分:15 秒:55 ampm:1
4.ジョーダの時間
4.1タイムゾーンを設定するJoda Timeの例:
DateTime dt =新しいDateTime(日付); DateTimeZone dtZone = DateTimeZone.forID( "America/New__York"); DateTime dtus = dt.withZone(dtZone);
繰り返しますが、一般的な間違いは、このようなDateを直接取得することです。
ゾーンは失われます。
//Output : 22-1-2015 10:15:55 AM Date dateInAmerica = dtus.toDate();
The correct way is converted to Joda
LocalDateTime
first.
//Output : 21-1-2015 09:15:55 PM Date dateInAmerica = dtus.toLocalDateTime().toDate();
4.2 Full example
JodaTimeExample.java
パッケージcom.mkyong.date; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; パブリッククラスJodaTimeExample { プライベート静的最終文字列DATE__FORMAT = "dd-M-yyyy hh:mm:ss a"; public static void main(String[]args)throws ParseException { SimpleDateFormatフォーマッタ=新しいSimpleDateFormat(DATE__FORMAT); String dateInString = "22-01-2015 10:15:55 AM"; Date date = formatter.parse(dateInString); TimeZone tz = TimeZone.getDefault(); //From TimeZone Asia/Singapore System.out.println("TimeZone : " + tz.getID() + " - " + tz.getDisplayName()); System.out.println("TimeZone : " + tz); System.out.println("Date (Singapore) : " + formatter.format(date)); //To TimeZone America/New__York SimpleDateFormat sdfAmerica = new SimpleDateFormat(DATE__FORMAT); DateTime dt = new DateTime(date); DateTimeZone dtZone = DateTimeZone.forID("America/New__York"); DateTime dtus = dt.withZone(dtZone); TimeZone tzInAmerica = dtZone.toTimeZone(); Date dateInAmerica = dtus.toLocalDateTime().toDate();//Convert to LocalDateTime first sdfAmerica.setTimeZone(tzInAmerica); System.out.println( "\ nTimeZone:" + tzInAmerica.getID()+ " - " + tzInAmerica.getDisplayName()); System.out.println( "TimeZone:" + tzInAmerica); System.out.println( "DateTimeZone:" + dtZone); System.out.println( "DateTime:" + dtus); System.out.println( "dateInAmerica(Formatter):" + formatter.format(dateInAmerica)); System.out.println( "dateInAmerica(Object):" + dateInAmerica); } }
出力
TimeZone:アジア/クアラルンプール - マレーシア時間TimeZone:sun.util.calendar.ZoneInfo[id = "アジア/クアラルンプール"、...]日付(シンガポール):22-1-2015 10:15:55 AM TimeZone:America/New__York - 東部標準時 TimeZone:sun.util.calendar.ZoneInfo[id = "America/New__York"、...]DateTimeZone:America/New__York 日時:2015-01-21T21:15:55.000-05:00 dateInAmerica(フォーマッター):21-1-2015 09:15:55 PM dateInAmerica(オブジェクト):Wed Jan 21 21:15:55 MYT 2015
P.S Joda-time 2.9.4
でテスト済み
===参考文献
。
Date
JodaTimeを使用したJavaの時間操作
。
ワールドタイムサーバー
。リンク://java8/java-8-zoneddatetime-examples/[Java 8 – ZonedDateTime
例]。
Java
8 – 日付をLocalDateとLocalDateTimeに変換
。
ZonedDateTime
Javadoc
。
Calendar
JavaDoc
。
Date
JavaDoc
。
SimpledateFormat
JavaDoc
calendar
date
joda-time
time
timezone
zoneddatetime