JavaのSimpleDateFormatは、Javaの日付形式です。

イントロダクション

JavaのSimpleDateFormatとDateFormatクラスは、日付の書式設定に使用されます。これは、Javaの日付と時刻の機能を表示や利用する必要がある場所で主に使用されます。これらのクラスは、com.textパッケージに存在しています。

JavaのDateFormat

  • DateFormat is used for formatting a date into String based on specific locale that is provided as input.
  • The locale is used for specifying the region and language for making the code more locale to the user.
  • The way of writing date is different in different regions of the world. For example, 31st Dec 2017 will be written in India as 31-12-2017 but the same thing will be written in the United States of America as 12-31-2017.
  • DateFormat classes are not synchronized, it’s recommended to create separate instance for each thread.

DateFormatクラスのgetDateInstance()メソッドとgetTimeInstance()メソッドを使用して、DateFormatオブジェクトを作成することができます。

Locale loc = new Locale("en", "US");
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, loc);

Note

注:バージョン19以降、Locale(String, String)コンストラクタは非推奨です。
Locale loc = new Locale.Builder().setLanguage(“en”).setRegion(“US”).build();

DateFormatのgetDateInstanceメソッドは、2つの入力パラメータが必要です。最初のパラメータは使用するDateFormatを指定し、2番目のパラメータはロケールです。

フォーマットを使用して、以下を日本語で言い換えてください。オプションは1つだけ必要です。

フォーマットを利用して、下記の内容を日本語に自然な表現で言い換えてください。一つのオプションだけを示していただければ結構です。

DateFormatクラスには、フォーマットを担当するformatメソッドがあります。

Locale locale = new Locale("fr", "FR");
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, locale);
String date = dateFormat.format(new Date());
System.out.print(date);

このコードは、日付をフランス語(fr)で表示し、フランス(FR)地域の形式に整えます。

Output

3 janv. 2018

getTimeInstance() を使用

DateFormatのインスタンスを作成するために、getDateInstance()メソッドを使用しています。

時間フォーマットを実行するためには、時間のインスタンスが必要です。時間のインスタンスを取得するためには、getTimeInstance()メソッドを使用します。

Locale locale = new Locale("fr", "FR");
DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale);
String date = dateFormat.format(new Date());
System.out.print(date);

このコードは、フランス語(fr)の言語とフランス(FR)の地域で時間をフォーマットします。

Output

11:03:01

JavaのSimpleDateFormat

SimpleDateFormatはDateFormatに類似しています。両者の主な違いは、SimpleDateFormatはロケールのサポートを持つフォーマット(日付を文字列に変換する)と解析(文字列を日付に変換する)に使用できる点ですが、DateFormatはロケールのサポートを持ちません。DateFormatは日付のフォーマットと解析の基本的なサポートを提供する抽象クラスです。SimpleDateFormatはそれを拡張した具象クラスです。

SimpleDateFormatはSimpleDateFormatのコンストラクタを使用して作成することができます。そのコンストラクタはパラメータ化されたコンストラクタであり、パラメータとして文字列のパターンが必要です。

String pattern = "MM-dd-yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

「Stringのパターン」は、日付のフォーマットに使われるパターンであり、出力は「MM-dd-yyyy」のパターンで生成されます。

パターン

フォーマットパターンに使用するべきパターン構文を見てみましょう。

Letter for Pattern Date or Time component Examples
G Era designator AD
y Year 2018 (yyyy), 18 (yy)
M Month in year July (MMMM), Jul (MMM), 07 (MM)
w Results in week in year 16
W Results in week in month 3
D Gives the day count in the year 266
d Day of the month 09 (dd), 9(d)
F Day of the week in month 4
E Day name in the week Tuesday, Tue
u Day number of week where 1 represents Monday, 2 represents Tuesday and so on 2
a AM or PM marker AM
H Hour in the day (0-23) 12
k Hour in the day (1-24) 23
K Hour in am/pm for 12 hour format (0-11) 0
h Hour in am/pm for 12 hour format (1-12) 12
m Minute in the hour 59
s Second in the minute 35
S Millisecond in the minute 978
z Timezone Pacific Standard Time; PST; GMT-08:00
Z Timezone offset in hours (RFC pattern) -0800
X Timezone offset in ISO format -08; -0800; -08:00

月という言葉の使い方には、結果が異なる場合でも、異なる量の文字が使われるべきです。

Type Pattern Example Output
Full Month MMMM July
Abbreviated Month MMM Jul
Numeric Month MM 07

以下の例文を日本語で自然な言い方に言い換えてください。1つのオプションが必要です:

例:
1. I like to eat sushi.

答え:
1. 寿司を食べるのが好きです。

さあ、日付と時間の異なる形式のいくつかの例を見てみましょう。

Pattern Result
MM/dd/yyyy 01/02/2018
dd-M-yyyy hh:mm:ss 02-1-2018 06:07:59
dd MMMM yyyy 02 January 2018
dd MMMM yyyy zzzz 02 January 2018 India Standard Time
E, dd MMM yyyy HH:mm:ss z Tue, 02 Jan 2018 18:07:59 IST

SimpleDateFormat()を使用しています。

日付を解析するためには、コンストラクタを使用してSimpleDateFormatのインスタンスを作成し、その後format()メソッドを使用する必要があります。

String pattern = "MM-dd-yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(new Date());
System.out.println(date);

このコードは「MM-dd-yyyy」と出力します。

Output

01-02-2018

このパターンは、”年の中の月”、”月の中の日”、および”年”を出力します。文字の種類(大文字と小文字)や文字数は、文字列に寄与します。月は2桁、日は2桁、年は4桁で表されます。

このコードは「2018年1月2日」の日付を「01-02-2018」と表示しました。

時間の解析のために、SimpleDateFormatのインスタンス作成時にパターンを変更する必要があります。

String pattern = "HH:mm:ss.SSSZ";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(new Date());
System.out.println(date);

このコードは「HH:mm:ss.SSSZ」を出力します。 (Kono kōdo wa “HH:mm:ss.SSSZ” o shutsuryoku shimasu.)

Output

13:03:15.454+0530

このパターンは、「時間」「分」「秒」「ミリ秒」と「時差オフセット(RFCパターン)」を表示します。

このコードは時間を「13:03:15.454+0530」として表示しました。

parse() を使っています。

解析とは、Stringをjava.util.Dateのインスタンスに変換することです。SimpleDateFormatクラスのparse()メソッドを使用して、文字列を日付のインスタンスに解析することができます。StringをDateに解析するためには、SimpleDateFormatクラスのインスタンスとクラスのコンストラクタの入力としての文字列パターンが必要です。

String pattern = "MM-dd-yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
Date date = simpleDateFormat.parse("12-01-2018");
System.out.println(date);

これは指定された時刻なしで日付 (12-01-2018) を出力します。

Output

Sat Dec 01 00:00:00 IST 2018

今度は、時間を解析するためのSimpleDateFormatの例を見てみましょう。

String pattern = "HH:mm:ss";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
Date date = simpleDateFormat.parse("22:00:03");
System.out.println(date);

これは、指定された日付なしで、時刻 (22:00:03) を出力します。

Output

Thu Jan 01 22:00:03 IST 1970

日付が指定されていないため、プログラムはエポックを日付として考慮しました(つまり、1970年1月1日)。

ロケールを使用する

私たちは、DateFormatの一部としてLocaleと一緒に作業してきましたが、ロケールは地域に基づいて使用されることが分かりました。

String pattern = "EEEEE MMMMM yyyy HH:mm:ss.SSSZ";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, new Locale("fr", "FR"));
Date date = simpleDateFormat.format(new Date());
System.out.println(date);

このコードは、現在の時間をフランス語(fr)で、およびフランス(FR)地域でフォーマットします。

Output

mardi janvier 2018 14:51:02.354+0530

ロケールに基づいて、日付と月はフランス語で名前が付けられます。mardiは「火曜日」を意味し、janvierは「1月」を意味します。

結論

この記事では、JavaのSimpleDateFormatとDateFormatについて学びました。

参照:

  • SimpleDateFormat API Doc
  • DateFormat API Doc
コメントを残す 0

Your email address will not be published. Required fields are marked *