Swing/TimeZone のバックアップの現在との差分(No.21)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/TimeZone へ行く。
- 1 (2004-04-19 (月) 10:36:59)
- 2 (2004-04-19 (月) 10:42:09)
- 3 (2004-06-02 (水) 10:01:50)
- 4 (2004-08-31 (火) 12:31:53)
- 5 (2004-10-08 (金) 06:27:10)
- 6 (2004-11-04 (木) 10:12:54)
- 7 (2005-04-28 (木) 04:33:10)
- 8 (2005-11-06 (日) 22:00:29)
- 9 (2006-02-27 (月) 16:49:23)
- 10 (2007-03-13 (火) 00:22:11)
- 11 (2013-04-07 (日) 04:44:59)
- 12 (2013-10-10 (木) 11:46:33)
- 13 (2014-10-23 (木) 01:00:47)
- 14 (2015-03-09 (月) 14:46:02)
- 15 (2015-03-16 (月) 17:28:33)
- 16 (2015-11-25 (水) 21:18:58)
- 17 (2016-11-01 (火) 19:34:56)
- 18 (2017-07-10 (月) 17:03:31)
- 19 (2017-12-07 (木) 11:29:57)
- 20 (2017-12-11 (月) 20:06:37)
- 21 (2019-08-21 (水) 18:51:38)
- 22 (2021-04-14 (水) 04:00:58)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- category: swing folder: TimeZone title: TimeZoneによる日付表示の変換 tags: [DateFormat] author: aterai pubdate: 2004-04-19T10:36:59+09:00 description: TimeZoneなどを使って、日付の表示を変換します。 image: https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTVW5Ljb9I/AAAAAAAAAng/mMDH4E_v9ZQ/s800/TimeZone.png --- * 概要 [#summary] `TimeZone`などを使って、日付の表示を変換します。 #download(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTVW5Ljb9I/AAAAAAAAAng/mMDH4E_v9ZQ/s800/TimeZone.png) * サンプルコード [#sourcecode] #code(link){{ SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US); DateFormat df = DateFormat.getDateTimeInstance(); // df.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo")); df.setTimeZone(TimeZone.getDefault()); JButton formatButton = new JButton("format"); formatButton.addActionListener(e -> field.setText(format.format(new Date()))); JButton parseButton = new JButton("parse"); parseButton.addActionListener(e -> { String str = field.getText().trim(); Date date = format.parse(str, new ParsePosition(0)); String o = Optional.ofNullable(date).map(df::format).orElse("error"); textArea.append(o + "\n"); }); }} * 解説 [#explanation] 上記のサンプルは、`Locale.US`の日付文字列を`SimpleDateFormat#parse(...)`メソッドを使用して一旦`Date`に変換し、`DateFormat#format(...)`メソッドでデフォルトロケール`TimeZone`のフォーマットスタイルに変換しています。 - メモ: -- `dateFormat.setTimeZone(TimeZone.getTimeZone("JST"))`のような`3`文字のタイムゾーン`ID`の使用は非推奨 -- `Java 1.7.0`から、タイムゾーンのパターン文字として`X`を使用すると、`ISO 8601`形式に変換可能になった --- `X`: `+09` --- `XX`: `+0900` --- `XXX`: `+09:00` - `dateFormat.setTimeZone(TimeZone.getTimeZone("JST"))`のような`3`文字のタイムゾーン`ID`の使用は非推奨 - `Java 1.7.0`からタイムゾーンのパターン文字`X`で`ISO 8601`形式に変換可能になった -- `X`: `+09` -- `XX`: `+0900` -- `XXX`: `+09:00` #code{{ DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX"); System.out.println("pubdate: " + format.format(new Date())); // pubdate: 2014-09-08T00:05:45+09:00 }} * 参考リンク [#reference] - [https://docs.oracle.com/javase/jp/8/docs/api/java/util/TimeZone.html TimeZone (Java Platform SE 8)] - [https://docs.oracle.com/javase/jp/8/docs/api/java/text/SimpleDateFormat.html SimpleDateFormat (Java Platform SE 8)] * コメント [#comment] #comment #comment