TITLE:TimeZoneによる日付表示の変換

Usage: #tags(tags)
Posted by at 2004-04-19

TimeZoneによる日付表示の変換

TimeZoneなどを使って、日付の表示を変換します。

  • &jnlp;
  • &jar;
  • &zip;
TimeZone.png

サンプルコード

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");
  }
});
View in GitHub: Java, Kotlin

解説

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

コメント