What is the difference between SimpleDateFormat and DateTimeFormatter? ??

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.

Difference in instantiation

There is a difference between new and using the provided methods.

For SimpleDateFormat

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");

For DateTimeFormatter

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");

parse

Try converting the String using SimpleDateFormat and DateTimeFormatter.

For SimpleDateFormat

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);

For DateTimeFormatter

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);

format

Try formatting SimpleDateFormat and DateTimeFormatter as their name suggests.

For SimpleDateFormat

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);

For DateTimeFormatter

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);

Summary

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

What is the difference between SimpleDateFormat and DateTimeFormatter? ??
What is the difference between a class and a struct? ?? ??
[Rails] What is the difference between redirect and render?
[JAVA] What is the difference between interface and abstract? ?? ??
What is the difference between skip and pending? [RSpec]
What is the difference between Java EE and Jakarta EE?
What is the difference between an action and an instance method?
What is the difference between a web server and an application server?
[Java] What is the difference between form, entity and dto? [Bean]
Understand the difference between each_with_index and each.with_index
Jersey --What is Difference Between bind and bindAsContract in HK2?
About the difference between irb and pry
What is the difference between the responsibilities of the domain layer and the application layer in the onion architecture [DDD]
[Java] Understand the difference between List and Set
[iOS] Understand the difference between frame and bounds
[Rails / ActiveRecord] About the difference between create and create!
Understand the difference between abstract classes and interfaces!
Difference between vh and%
Difference between i ++ and ++ i
[Ruby] What is the slice method? Let's solve the example and understand the difference from slice!
[Ruby] I thought about the difference between each_with_index and each.with_index
[Rails] I investigated the difference between redirect_to and render.
[Swift] UITextField taught me the difference between nil and ""
Difference between product and variant
Difference between redirect_to and render
What is the pluck method?
Rails: Difference between resources and resources
Difference between puts and print
Difference between redirect_to and render
Difference between CUI and GUI
What is the BufferedReader class?
Difference between mockito-core and mockito-all
Difference between class and instance
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
Difference between .bashrc and .bash_profile
What is the constructor for?
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
What is Microservices? And Microservices Frameworks
What is the initialize method?
Is there a performance difference between Oracle JDK and OpenJDK at the end of 2017?
Difference between Java and JavaScript (how to find the average)
What is the LocalDateTime class? [Java beginner] -Date and time class-
About the difference between "(double quotation)" and "single quotation" in Ruby
[Ruby] About the difference between 2 dots and 3 dots of range object.
[Java] Check the difference between orElse and orElseGet with IntStream
Let's override the difference between == (identity) and equals method (equivalence)
The difference between programming with Ruby classes and programming without it
Is short-circuit evaluation really fast? Difference between && and & in Java
Now in the third year, the misunderstanding that I noticed is the difference between the equals method and ==
[Ruby] Difference between get and post
Difference between instance method and class method
Difference between render method and redirect_to
Find the difference between List types
Difference between interface and abstract class
[Java] Difference between Hashmap and HashTable
[Terminal] Difference between irb and pry