イントロダクション
JavaのSimpleDateFormat
およびDateFormat
クラスは、日付のフォーマットに使用されます。これは主に、Javaの日付および時刻機能を表示または利用する必要がある場合に使用されます。
JavaのDateFormat
DateFormat
は、指定されたロケールに基づいて日付をString
にフォーマットするために使用されます。- ロケールは、ユーザーにとってよりローカルなコードを作成するために、地域と言語を指定するために使用されます。
- 世界の異なる地域では、日付の書き方が異なります。たとえば、
2017年12月31日
は、インドでは31-12-2017
として書かれますが、アメリカでは12-31-2017
として書かれます。 DateFormat
クラスは同期されていないため、各スレッドごとに別のインスタンスを作成することが推奨されています。
A DateFormat
object can be created using the getDateInstance()
and getTimeInstance()
method of the DateFormat
class.
Locale loc = new Locale("en", "US");
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, loc);
注意: バージョン19以降では、コンストラクタLocale(String, String)は非推奨です。
Locale loc = new Locale.Builder().setLanguage("en").setRegion("US").build();
DateFormat
のgetDateInstance
メソッドには2つの入力パラメータが必要です。最初のパラメータは使用するDateFormat
を指定し、2番目のパラメータはロケールです。
format
を使用
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
)地域で日付をフォーマットします:
Output3 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
)地域で時間をフォーマットします:
Output11:03:01
Java SimpleDateFormat
SimpleDateFormat
はDateFormat
に似ています。 それらの間の唯一の主な違いは、SimpleDateFormat
がロケールサポート付きでフォーマット(Date
をString
に変換)および解析(String
をDate
に変換)に使用できることであり、一方DateFormat
はロケールサポートを持たないことです。 DateFormat
は日付のフォーマットと解析のための基本的なサポートを提供する抽象クラスです。SimpleDateFormat
はDateFormat
クラスを拡張した具象クラスです。
SimpleDateFormat
はSimpleDateFormat
コンストラクタを使用して作成できます。 このコンストラクタはパラメーターとしてStringパターンが必要です。
String pattern = "MM-dd-yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
文字列pattern
は、日付をフォーマットするために使用されるパターンであり、出力はそのパターンで生成されます、例えば“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 |
例
今度は、日付と時刻の異なる形式のいくつかの例を見てみましょう。
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
」を出力します:
Output01-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
」を出力します:
Output13:03:15.454+0530
このパターンは「時」、「分」、「秒」、「ミリ秒」、「タイムゾーンオフセット(RFCパターン)」を生成します。
このコードは時間を「13:03:15.454+0530
」として表示します。
parse()
を使用する
解析は文字列を`java.util.Date`インスタンスに変換することです。`SimpleDateFormat`クラスの`parse()`メソッドを使用して文字列を日付インスタンスに解析できます。日付を解析するには、`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
)が出力されます。
OutputSat 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
)が出力されます。
OutputThu Jan 01 22:00:03 IST 1970
日付が指定されていないため、プログラムはエポックを日付として考慮しました(つまり、01-Jan-1970
)。
Locale
の使用
ロケールは、地域に基づいて使用されることがわかるように、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
)の地域でフォーマットします。
Outputmardi janvier 2018 14:51:02.354+0530
日付と月は、入力として提供されたLocale
に基づいてフランス語で名前が付けられます – mardi
は「火曜日」で、janvier
は「1月」です。
結論
この記事では、JavaのSimpleDateFormat
とDateFormat
について学びました。
参考:
Source:
https://www.digitalocean.com/community/tutorials/java-simpledateformat-java-date-format