• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:TimeZoneによる日付表示の変換
#navi(../)
*TimeZoneによる日付表示の変換 [#e2715c6a]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2004-04-19~
更新日:&lastmod;
---
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`などを使って、日付の表示を変換します。

#contents
#download(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTVW5Ljb9I/AAAAAAAAAng/mMDH4E_v9ZQ/s800/TimeZone.png)

**概要 [#abb844cc]
TimeZoneなどを使って、日付の表示を変換します。
* サンプルコード [#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());

#screenshot
JButton formatButton = new JButton("format");
formatButton.addActionListener(e -> field.setText(format.format(new Date())));

**サンプルコード [#fc90bfc8]
   final SimpleDateFormat format = 
     new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
   final DateFormat df = DateFormat.getDateTimeInstance();
   //field.setText("Mon, 19 Apr 2004 16:31:41 +0900");
   outf.setEditable(false);
   df.setTimeZone(TimeZone.getTimeZone("JST"));
   JButton button = new JButton(new AbstractAction("変換") {
     public void actionPerformed(ActionEvent e) {
       String str = field.getText().trim();
       ParsePosition pp = new ParsePosition(0);
       Date date = format.parse(str, pp);
       if(date!=null) {
         outf.setText(df.format(date));
       }else{
         outf.setText("error");
         Logger.global.info(pp.toString());
       }
     }
   });
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");
});
}}

-&jnlp;
-&jar;
-&zip;
* 解説 [#explanation]
上記のサンプルは、`Locale.US`の日付文字列を`SimpleDateFormat#parse(...)`メソッドを使用して一旦`Date`に変換し、`DateFormat#format(...)`メソッドでデフォルトロケール`TimeZone`のフォーマットスタイルに変換しています。

**解説 [#j9b9048c]
サンプルプログラムでは、メールなどのDateを変換するようにしています。
- `dateFormat.setTimeZone(TimeZone.getTimeZone("JST"))`のような`3`文字のタイムゾーン`ID`の使用は非推奨
- `Java 1.7.0`からタイムゾーンのパターン文字`X`で`ISO 8601`形式に変換可能になった
-- `X`: `+09`
-- `XX`: `+0900`
-- `XXX`: `+09:00`

//**参考リンク
**コメント [#k068837c]
#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