• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:TimeZoneによる日付表示の変換
#navi(../)
#tags()
RIGHT:Posted by &author(aterai); at 2004-04-19
*TimeZoneによる日付表示の変換 [#e2715c6a]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2004-04-19~
更新日:&lastmod;

#contents

**概要 [#abb844cc]
TimeZoneなどを使って、日付の表示を変換します。

#screenshot

**サンプルコード [#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());
       }
     }
   });

-&jnlp;
-&jar;
-&zip;

//#screenshot
#ref(http://lh3.ggpht.com/_9Z4BYR88imo/TQTVW5Ljb9I/AAAAAAAAAng/mMDH4E_v9ZQ/s800/TimeZone.png)

**サンプルコード [#fc90bfc8]
#code(link){{
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);
    outf.setText((date!=null)?df.format(date):"error");
  }
});
}}

**解説 [#j9b9048c]
サンプルプログラムでは、メールなどのDateを変換するようにしています。

//**参考リンク
**コメント [#k068837c]
#comment