Java 8 –文字列をLocalDateに変換する方法
文字列を新しいJava8 Date APIに変換する方法を示すJavaの例はほとんどありません–java.time.LocalDate
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy");
String date = "16/08/2016";
//convert String to LocalDate
LocalDate localDate = LocalDate.parse(date, formatter);
Note
日時フォーマッタのその他の例については、この公式のDateTimeFormatterJavaDocを参照してください。
Note
この古典的なjava.util.Dateの例に興味があるかもしれません–How to convert String to
Date in Java
1. 文字列= 2016-08-16
文字列がISO_LOCAL_DATEのようにフォーマットされている場合は、文字列を直接解析でき、変換する必要はありません。
TestNewDate1.java
package com.example.java8.date;
import java.time.LocalDate;
public class TestNewDate1 {
public static void main(String[] argv) {
String date = "2016-08-16";
//default, ISO_LOCAL_DATE
LocalDate localDate = LocalDate.parse(date);
System.out.println(localDate);
}
}
出力
2016-08-16
2. 文字列= 2016年8月16日
TestNewDate2.java
package com.example.java8.date;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class TestNewDate2 {
public static void main(String[] argv) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d-MMM-yyyy");
String date = "16-Aug-2016";
LocalDate localDate = LocalDate.parse(date, formatter);
System.out.println(localDate); //default, print ISO_LOCAL_DATE
System.out.println(formatter.format(localDate));
}
}
出力
2016-08-16 16-Aug-2016
3. 文字列= 16/08/2016
TestNewDate3.java
package com.example.java8.date;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class TestNewDate3 {
public static void main(String[] argv) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy");
String date = "16/08/2016";
LocalDate localDate = LocalDate.parse(date, formatter);
System.out.println(localDate);
System.out.println(formatter.format(localDate));
}
}
出力
2016-08-16 16/08/2016
4. 文字列= 2016年8月16日火曜日
TestNewDate4.java
package com.example.java8.date;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class TestNewDate4 {
public static void main(String[] argv) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E, MMM d yyyy");
String date = "Tue, Aug 16 2016";
LocalDate localDate = LocalDate.parse(date, formatter);
System.out.println(localDate);
System.out.println(formatter.format(localDate));
}
}
出力
2016-08-16 Tue, Aug 16 2016
5. 文字列= 2016年8月16日火曜日12:10:56 PM
この例では、文字列をjava.time.LocalDateTimeに変換します
TestNewDate5.java
package com.example.java8.date;
package com.example.pageview;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class TestNewDate5 {
public static void main(String[] argv) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, MMM d, yyyy HH:mm:ss a");
String date = "Tuesday, Aug 16, 2016 12:10:56 PM";
LocalDateTime localDateTime = LocalDateTime.parse(date, formatter);
System.out.println(localDateTime);
System.out.println(formatter.format(localDateTime));
}
}
出力
2016-08-16T12:10:56 Tuesday, Aug 16, 2016 12:10:56 PM
6. 文字列= 2016-08-16T15:23:01Z
「Z」サフィックスはUTCを意味します。java.time.instantに直接変換して、タイムゾーンで表示できます。
TestNewDate6.java
package com.example.java8.date;
import java.time.*;
public class TestNewDate6 {
public static void main(String[] argv) {
String dateInString = "2016-08-16T15:23:01Z";
Instant instant = Instant.parse(dateInString);
System.out.println("Instant : " + instant);
//get date time only
LocalDateTime result = LocalDateTime.ofInstant(instant, ZoneId.of(ZoneOffset.UTC.getId()));
//get localdate
System.out.println("LocalDate : " + result.toLocalDate());
//get date time + timezone
ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of("Asia/Tokyo"));
System.out.println(zonedDateTime);
//get date time + timezone
ZonedDateTime zonedDateTime2 = instant.atZone(ZoneId.of("Europe/Athens"));
System.out.println(zonedDateTime2);
}
}
出力
Instant : 2016-08-16T15:23:01Z LocalDate : 2016-08-16 2016-08-17T00:23:01+09:00[Asia/Tokyo] 2016-08-16T18:23:01+03:00[Europe/Athens]
7. 文字列= 2016-08-16T10:15:30 + 08:00
文字列→ZonedDateTime→LocalDate
TestNewDate7.java
package com.example.java8.date;
import java.time.*;
import java.time.format.DateTimeFormatter;
public class TestNewDate7 {
public static void main(String[] argv) {
String date = "2016-08-16T10:15:30+08:00";
ZonedDateTime result = ZonedDateTime.parse(date, DateTimeFormatter.ISO_DATE_TIME);
System.out.println("ZonedDateTime : " + result);
System.out.println("TimeZone : " + result.getZone());
LocalDate localDate = result.toLocalDate();
System.out.println("LocalDate : " + localDate);
}
}
出力
ZonedDateTime : 2016-08-16T10:15:30+08:00 TimeZone : +08:00 LocalDate : 2016-08-16