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.
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".
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