Get current date and time with LocalDateTime class

Before Java 8 we had to either create an instance of the Date class or use the Calendar class to get the current date and time. But with the new date API that arrived with Java 8 there are new classes that can accomplish this. To get the current date and time we can use the LocalDateTime class together with the TextStyle class to get the date and time and print them out.
This is how it’s done:

The Date is printed out in different formats. The format is decided by the String.format class where we specify how it should look. In the call to String.format we specify the format for the output. The %d means we will insert a digit at that position, and %s means we will insert a string. The values follow as parameters to the method after the initial format parameter.
The output from the code above is (or was when this was written):

Search for more Java info on this site here:
Custom Search

Please type any questions here.