I want to use the Java 8 DateTime API slowly (now)

It's very new, but I tend to be shunned for some reason, so I want to use it from where it can be used. Note that.

Time unit

So far: literal or java.util.concurrent.TimeUnit

https://docs.oracle.com/javase/jp/8/docs/api/java/util/concurrent/TimeUnit.html It's actually an "hourly" class from Java 5.

// int sec = 60*3; //This is
long sec = TimeUnit.MINUTES.toSeconds(3); //Will be like this

Wow, something useful at this point. In fact, Java 5 no longer had to do 60 * 3 or 1 * 24 * 60 * 60 at the "old days"!

Java8: java.time.Duration https://docs.oracle.com/javase/jp/8/docs/api/java/time/Duration.html

// int sec = 60*3; //This is
long sec = Duration.ofMinutes(3).getSeconds(); //This happens

Honestly, I think it doesn't matter which one you use (at least it's better than 60 * 3). However, this one is newer, and above all, I like this one because it can be read as "hour unit, 3 minutes in seconds".

Format / Perth

So far: java.text.SimpleDateFormat

https://docs.oracle.com/javase/jp/8/docs/api/java/text/SimpleDateFormat.html

slow! It's not thread safe! → It's annoying!

java.time.format.DateTimeFormatter https://docs.oracle.com/javase/jp/8/docs/api/java/time/format/DateTimeFormatter.html

This class is immutable and thread safe.

http://www.ne.jp/asahi/hishidama/home/tech/java/DateTimeFormatter.html

DateTimeFormatter is thread-safe, so you can define it in a static field and use it in multiple threads. Incidentally, instantiation is also lighter than SimpleDateFormat. Regarding formatting, it seems to be slower than SimpleDateFormat, but DateTimeFormatter is also faster for parsing.

** We wanted this! ** ** You don't have to rely on Apache commons anymore!

However,

However, in the case of DateTimeFormatter, whitespace in the target string is accepted only when whitespace is specified in the format string.

And,

If you make whitespace an optional format, it will be accepted with or without whitespace. However, it cannot handle multiple spaces. (Multiple options can be specified, but it cannot handle any number)

Please note that there are subtle differences. (But if it's such a difference, you can just cleanse it!)

Recommended Posts

I want to use the Java 8 DateTime API slowly (now)
I want to use java8 forEach with index
[Java] I want to calculate the difference from the date
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
I want you to use Scala as Better Java for the time being
[Beginner] I want to modify the migration file-How to use rollback-
[java8] To understand the Stream API
I want to use screen sharing on the login screen on Ubuntu 18
I want to use FormObject well
I want to use the sanitize method other than View.
Java 8 ~ Stream API ~ to start now
I tried to summarize Java 8 now
I want to simplify the conditional if-else statement in Java
I want to use Java Applet easily on the command line without using an IDE
I want to return to the previous screen with kotlin and java!
[Java] I want to perform distinct with the key in the object
[Java] How to use the HashMap class
I want to use DBViewer with Eclipse 2018-12! !!
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
Java 8 to start now ~ Date time API ~
[Processing × Java] How to use the class
I tried to summarize the Stream API
I want to stop Java updates altogether
[Processing × Java] How to use the function
I want to use @Autowired in Servlet
I went to the Java Women's Club # 1
[Java] How to use the Calendar class
I want to display the number of orders for today using datetime.
I called the COTOHA API parser 100 times in Java to measure performance.
I want to get the IP address when connecting to Wi-Fi in Java
I want to get the field name of the [Java] field. (Old tale tone)
[Java Spring MVC] I want to use DI in my own class
I want you to use Enum # name () for the Key of SharedPreference
I want to output the day of the week
[Java] How to use Thread.sleep to pause the program
I want to send an email in Java.
I want to use arrow notation in Ruby
I want to var_dump the contents of the intent
When you want to use the method outside
rsync4j --I want to touch rsync in Java.
How to use Java API with lambda expression
How to use the replace () method (Java Silver)
I want to write quickly from java to sqlite
I want to truncate after the decimal point
I want to get the value in Ruby
I want to play a GIF image on the Andorid app (Java, Kotlin)
[Eclipse] I want to use the completion function, but I want to manage to confirm the completion with spaces.
I want to separate the handling of call results according to the API caller (call trigger)
I want to use Clojure's convenient functions in Kotlin
I want to do something like "cls" in Java
I want to use NetBeans on Mac → I can use it!
I want to embed any TraceId in the log
I want to use fish shell in Laradock too! !!
Use Java lambda expressions outside of the Stream API
I want to judge the range using the monthly degree
I want to know the answer of the rock-paper-scissors app
[Rails] I don't know how to use the model ...
I want to display the name of the poster of the comment
I want to dark mode with the SWT app