What is the difference between SimpleDateFormat and DateTimeFormatter? ??
I used SimpleDateFormat and DateTimeFormatter in the article I wrote last time. Both were date-time formatters, and I couldn't figure out what was different, so I looked it up.
There is a difference between new and using the provided methods.
Instantiation of SimpleDateFormat is created by new. Not thread safe. Therefore, in the case of multi-thread processing, it is necessary to create an instance each time immediately before using it.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
DateTimeFormatter is instantiated by the ofPattern method. Thread safe. Unlike SimpleDateFormat, there is no need to create an instance each time you use it during multithreaded processing.
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss.SSS");
Try converting the String using SimpleDateFormat and DateTimeFormatter.
Process to convert from String to Date. Just pass the String type as an argument to the parse method. Exception handling of ParseException is required, but omitted in the example below.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
Date date = sdf.parse("2018/05/14 12:34:56.123");
// Mon May 14 12:34:56 UTC 2018
System.out.println(date);
Process to convert from String to Temporal Accessor.
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss.SSS");
TemporalAccessor ta = dtf.parse("2018/05/14 12:34:56.123");
// {},ISO resolved to 2018-05-14T12:34:56.123
System.out.println(ta);
What is TemporalAccessor? It became. The output result also says "ISO resolved to ~~". It seems to be the parent interface of LocalDateTime. It is also the parent of LocalDate, LocalTime, and ZonedDateTime. So you can do this.
TemporalAccessor tAccessor = LocalDateTime.of(2018, 5, 14, 12, 34, 56);
When parsing with DateTimeFommater, it seems that there are many patterns in which the parse method of LocalDateTime is used. It looks like below. I want to remember that this one seems to be used more frequently. (Process to convert from String to LocalDateTime.)
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss.SSS");
LocalDateTime date = LocalDateTime.parse("2018/05/14 12:34:56.123", dtf);
// 2018-05-14T12:34:56.123
System.out.println(date);
Try formatting SimpleDateFormat and DateTimeFormatter as their name suggests.
Processing to convert from Date to String. Pass java.util.Date type as an argument to format method.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
Date date = sdf.parse("2018/05/14 12:34:56.123");
// Mon May 14 12:34:56 UTC 2018
System.out.println(date);
String str = sdf.format(date);
// 2018/05/14 12:34:56.123
System.out.println(str);
Pass TemporalAccessor as an argument to the format method.
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss.SSS");
LocalDateTime date = LocalDateTime.parse("2018/05/14 12:34:56.123", dtf);
// 2018-05-14T12:34:56.123
System.out.println(date);
String str = dtf.format(date);
// 2018/05/14 12:34:56.123
System.out.println(str);
I wrote it as sloppy, but to summarize what I found,
SimpleDateFormat | DateTimeFormatter | |
---|---|---|
Mold | java.util.Conversion to Date | TemporalAccessorConversionto(LocalDateTime),LocalDate,LocalTime,ZonedDateTimeetc.) |
Instance generation | new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS") |
DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss.SSS") |
Thread safe? | × | ◯ |
parse | dtf.parse(str) java.util.It becomes Date. |
dtf.parse(str) TemporalAccessorbecome. Parsing methods such as LocalDateTime are easier to use. |
format | sdf.format(date) |
dtf.format(TemporalAccessor) |
After all it is difficult to handle Java date / time. I feel like PHP has become so difficult. (Maybe I just think so because I haven't dealt with dates / times so much ...)
Recommended Posts