1.简介
DateTimeFormatter
和 SimpleDateFormat
都是用于格式化日期和时间的类,但是它们有一些区别。
SimpleDateFormat
是 Java 早期版本中提供的日期格式化类,他是线程不安全的。由于 SimpleDateFormat
是线程不安全的,所以如果在多线程环境中使用会出现异常,通常的解决方法要么是每个线程独立维护一份 SimpleDateFormat
对象实例,要么是将 SimpleDateFormat
放到 ThreadLocal
中。
在Java 8中新增了DateTimeFormatter
类,它比 SimpleDateFormat
更加灵活和安全。DateTimeFormatter
是不变对象,且是线程安全的。所以在使用 DateTimeFormatter
的时候,可以以静态成员变量的方式,在一个类中只维护一份对象实例,用于格式化多个日期和时间值,而无需担心线程安全问题。
2.区别
DateTimeFormatter
和 SimpleDateFormat
都是 Java 中用于日期和时间格式化的类,但它们之间存在以下区别:
-
线程安全性:
DateTimeFormatter
是线程安全的,而SimpleDateFormat
不是。 -
类型支持:
DateTimeFormatter
支持多种日期和时间类型,例如LocalDate
、LocalTime
、LocalDateTime
、ZonedDateTime
、OffsetDateTime
等。而SimpleDateFormat
只支持Date
类型。 -
解析能力:
DateTimeFormatter
可以将字符串解析为日期和时间类型,而SimpleDateFormat
不支持解析操作。 -
ローカリゼーション:
DateTimeFormatter
ローカリゼーションをサポートし、さまざまな言語や国の習慣に応じて日付と時刻をフォーマットできます。SimpleDateFormat
ローカリゼーションもサポートしていますが、使用するのがさらに面倒です。
たとえば、フランス語のローカリゼーションで日付をフォーマットするには、次のコードを使用できます。
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(Locale.FRANCE);
LocalDate date = LocalDate.now();
String formattedDate = date.format(formatter);
System.out.println(formattedDate);
- API 設計:
DateTimeFormatter
より使いやすく、読みやすい、よりスムーズな API 設計を提供します。
全体として、単純な日付と時刻の書式設定SimpleDateFormat
には、DateTimeFormatter
柔軟性、スレッド セーフ、型サポート、解析機能、ローカリゼーションの点でより強力で安全です。Java 8 以降では、 を使用することをお勧めしますDateTimeFormatter
。
3.SimpleDateFormat
SimpleDateFormat
次のようないくつかの一般的な書式設定オプションがサポートされています。
書式設定オプション | 説明 |
---|---|
yyyy |
年 (2023 など) |
MM |
月、例: 05 |
dd |
日付、例: 04 |
HH |
時 (24 時間形式)、例: 13 |
mm |
分、例: 30 |
ss |
秒、例: 45 |
S |
ミリ秒、例: 789 |
E |
曜日、例: 火曜日 |
Z |
タイムゾーン、例: +0800 |
3.1 文字列を日付に変換する
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timeStr = "2023-05-04 21:54:27";
Date date = sdf.parse(timeStr);
System.out.println(date);
出力は次のとおりです。
2023-05-04 22:15:21
上記の通り、SimpleDateFormat
文字列を日付に変換する方法は主にparse
この、定義に合った文字列をDate
型に変換することができます。
3.2 日付を文字列に変換
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date();
String formattedDate = sdf.format(now);
System.out.println(formattedDate);
出力は次のとおりです。
Thu May 04 21:54:27 CST 2023
上に示したように、日付を文字列に変換する方法はformat
、Date
日付型のオブジェクトを文字列の日付形式に変換して表示します。
4.DateTimeFormatter
DateTimeFormatter
SimpleDateFormat
としていくつかの一般的な書式設定オプションをサポートすることに加えて、次のような追加の書式設定オプションもサポートします。
書式設定オプション | 説明 |
---|---|
uuuu |
年、BC エポックにyyyy 似ていますがマイナスです |
G |
西暦や紀元前などの時代 |
q |
四半期(第 1 四半期など) |
L |
月、例: 1 または |
DateTimeFormatter
LocalDate
、LocalTime
、LocalDateTime
、など、さまざまな日付と時刻の種類をフォーマットするために使用できますZonedDateTime
。OffsetDateTime
4.1 文字列を日付に変換する
たとえば、文字列「2023-05-04 13:30:45」LocalDateTime
を type、次のコードを使用できます。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String dateString = "2023-05-04 13:30:45";
LocalDateTime dateTime = LocalDateTime.parse(dateString, formatter);
System.out.println(dateTime);
出力結果の形式は次のとおりです。
2023-05-04T13:30:45
上に示したように、文字列を型に変換するparse
メソッドを使用します。ここでは直接使用する静的メソッドを使用し、日付の文字列と形式の形式を入力パラメータとして渡します。LocalDateTime
LocalDateTime
parse
4.2 日付を文字列に変換
たとえば、LocalDate を「yyyy-MM-dd」形式の文字列としてフォーマットするには、次のコードを使用できます。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date = LocalDate.now();
String formattedDate = date.format(formatter);
System.out.println(formattedDate);
出力は次のとおりです。
2023-05-04
知らせ:
SimpleDateFormat
の使用とは異なり、 を使用する場合は、オブジェクトに対して、またはオブジェクトからSimpleDateFormat
直接使用されます。SimpleDateFormat
sdf
parse
format
また、 using はまたは のオブジェクトをDateTimeFormatter
直接使用するわけではありません。渡すことで日付に変換され、渡されたオブジェクトによって文字列に変換されます。DateTimeFormatter
parse
format
LocalDateTime.parse(dateString, formatter)
LocalDate
date.format(formatter)
拡大
LocalDate
の日付クラスと同様に、日付は次の方法で作成できます。
LocalDate newDate = LocalDate.of(2022, 1, 1);
LocalDate now = LocalDate.now();
現在の日付に加算または減算する必要がある場合は、次の方法を使用できます。
Period oneMonth = Period.ofMonths(1);
newDate = newDate.plus(oneMonth);
このようにして、newDateの日付に1か月が加算されます。
LocalDate
クラス内には実用的な静的メソッドや共通メソッドも多数あり、実際の使用シナリオに応じて選択できます。