Display Japanese calendar and days of the week using java8 standard class

Various classes are prepared for the date and time type of java8. Although it is multifunctional, it is difficult to use (Japanese calendar can be handled with Japanese Date, but it does not have time information, etc.), so I thought of a utility method that outputs an arbitrary date and time in an arbitrary format.

DateTimeFormat.java


import java.time.LocalTime;
import java.time.chrono.ChronoLocalDateTime;
import java.time.chrono.JapaneseDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.Locale;
import java.util.function.Function;

@SuppressWarnings("javadoc")
public class DateTimeFormat {

    public static String dateTimeFormat(String format) {
        return dateTimeFormat(format, Function.identity());
    }

    public static String dateTimeFormat(String format, Function<ChronoLocalDateTime<JapaneseDate>, ChronoLocalDateTime<JapaneseDate>> modify) {
        return modify.apply(JapaneseDate.now().atTime(LocalTime.now())).format(DateTimeFormatter.ofPattern(format, Locale.JAPAN));
    }

    public static void main(String[] __) {
        //Current date and time
        System.out.println(dateTimeFormat("uuuu/MM/dd(E) HH:mm:ss"));
        System.out.println(dateTimeFormat("Gy year M month d day(EEEE)H hours m minutes s seconds"));
        System.out.println(dateTimeFormat("GGGGGy.M.d"));
        //Last day of this month
        System.out.println(dateTimeFormat("uuuu/MM/dd(E)", d -> d.with(ChronoField.DAY_OF_MONTH, d.toLocalDate().lengthOfMonth())));
        //After a week
        System.out.println(dateTimeFormat("Gy year M month d day(E)", d -> d.plus(7, ChronoUnit.DAYS)));
        //1st next month
        System.out.println(dateTimeFormat("GGGGGy.M.d", d -> d.with(ChronoField.DAY_OF_MONTH, 1).plus(1, ChronoUnit.MONTHS)));
    }

}

Execution result


2017/06/15(wood) 11:30:05
June 15, 2017(Thursday)11:30:05
H29.6.15
2017/06/30(Money)
June 22, 2017(wood)
H29.7.1

Recommended Posts

Display Japanese calendar and days of the week using java8 standard class
[Ruby] Display today's day of the week using Date class
[Java] Get and display the date 10 days later using the Time API added from Java 8.
[Java] Calculate the day of the week from the date (Calendar class is not used)
[Java] Get the date 10 days later with the Calendar class
Output of the day of the week using Ruby's Date class
Java unit test Judgment of specified day of the week Determines whether ArDate, Date, Calendar, and long are specified days of the week.
[Java] How to display the day of the week acquired by LocalDate, DateTimeformatter in Japanese
Was done in the base year of the Java calendar week
[Java] Get date information 10 days later using milliseconds in the Date class
Read the first 4 bytes of the Java class file and output CAFEBABE
I tried to display the calendar on the Eclipse console using Java.
Six Prime Number Sequence Advent Calendar 2018 Java Edition
Summary of Java Math.random and import (Calendar)
Display Japanese calendar and days of the week using java8 standard class
Memorandum of new graduate SES [Java object-oriented edition]
[Java] Overview of Java
Links for each version (Japanese version) of Java SE API
[Java] Get the day of the specific day of the week
[Java] Inheritance and structure of HttpServlet class
[Java] How to use the Calendar class
Summary of Java Math.random and import (Calendar)
I compared the characteristics of Java and .NET
[Ruby] Code to display the day of the week
About next () and nextLine () of the Scanner class
Display "Hello World" in the browser using Java
Display "Hello World" in the browser using Java
First touch of the Files class (or Java 8)
[Java] How to use Calendar class and Date class
[Final version] Check the operation of the Japanese calendar using ICU to support the new era.
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
Getting a little stuck on the last day of the month using Java's Calendar class
[Rails] How to display the weather forecast of the registered address in Japanese using OpenWeatherMap
[Ubuntu 20.04] Display the day of the week on the date / clock
Output the maximum value from the array using Java standard output
Please note the division (division) of java kotlin Int and Int
The comparison of enums is ==, and equals is good [Java]
[Java] Sort the list using streams and lambda expressions
Try implementing the Eratosthenes sieve using the Java standard library
Organizing the current state of Java and considering the future
Java language from the perspective of Kotlin and C #
I summarized the types and basics of Java exceptions
Use of Abstract Class and Interface properly in Java
[Ruby] Class nesting, inheritance, and the basics of self
[Java10] Be careful of using var and generics together
[Java] Handling of character strings (String class and StringBuilder class)
Summarize the additional elements of the Optional class in Java 9
Judgment of the calendar
Send and receive simple messages using the Kafka component of the Apache Camel framework (Java DSL)
[Note] Java Output of the sum of odd and even elements
Implement Thread in Java and try using anonymous class, lambda
I didn't understand the behavior of Java Scanner and .nextLine ().
[Java] Throw a request and display the screen ② (GET / POST)
Find the address class and address type from the IP address with Java
I tried to summarize the basics of kotlin and java
[Java] Comparison method of character strings and comparison method using regular expressions
When using the constructor of Java's Date class, the date advances by 1900.
[Java] Set structure of collection class (about HashSet and TreeSet)
Command to check the number and status of Java threads
[Java] Throw a request and display the screen (GET / POST)
Check the status of Java application without using monitoring tool
Java beginners briefly summarized the behavior of Array and ArrayList
[Ruby] Creating code using the concept of classes and instances
[Java] Try editing the elements of the Json string using the library
The story of pushing Java to Heroku using the BitBucket pipeline
[Java] I thought about the merits and uses of "interface"