[Java] How to get the current date and time and specify the display format

Get the current date and time and specify the display format (LocalDateTime class)

LocalDateTime class is a class that gets the current date and time and specifies the display format. Handles dates/hours without a timezone. By using DateTimeFormatter class, you can format the acquired date and time into the specified display format.

In the process of ①, the display format is specified by the ofPattern method of the DateTimeFormatter class. In the process of ②, it is set by the format method of the DateTimeFormatter class.

public class Sample {
	public static void main(String[] args) {
        
        //Get the current date and time
		LocalDateTime nowDate = LocalDateTime.now();
		System.out.println(nowDate); //2020-12-20T13:32:48.293

        //Specify display format
		DateTimeFormatter dtf1 =
			DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss.SSS"); // ①
		String formatNowDate = dtf1.format(nowDate); // ②
		System.out.println(formatNowDate); // 2020/12/20 13:32:48.293

        //Specify display format
		DateTimeFormatter dtf2 =
		  DateTimeFormatter.ofPattern("yyyy year MM month dd day HH hour mm minute ss second E day");
		String formatNowDate = dtf2.format(nowDate);
		System.out.println(formatNowDate); //December 20, 2020 13:32:48 Sunday

        //Specify display format
		DateTimeFormatter dtf3 =
			DateTimeFormatter.ofPattern("yyyyMMddHHmm");
				String formatNowDate = dtf3.format(nowDate);
				System.out.println(formatNowDate); // 202012201332
 	}
}

Get the current date and time and specify the display format (Date class)

The Date class is a date and time class based on Unix time (the elapsed time from 00:00:00 on January 1, 1970). The SimpleDateFormat class can format the acquired date and time into a specified display format.

public class Test1 {
	public static void main(String[] args) {

		//Get the current date and time
		Date nowDate = new Date();
		System.out.println(nowDate); //Sun Dec 20 13:56:23 JST 2020

		//Specify display format
		SimpleDateFormat sdf1
		= new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
		String formatNowDate = sdf1.format(nowDate);
		System.out.println(formatNowDate); // 2020/12/20 13:56:23.372

		//Specify display format
		SimpleDateFormat sdf2
		= new SimpleDateFormat("yyyy year MM month dd day HH hour mm minute ss second");
		String formatNowDate = sdf2.format(nowDate);
		System.out.println(formatNowDate); //December 20, 2020 13:56:23
 	}
}

reference

Java Date and Time

Recommended Posts

[Java] How to get the current date and time and specify the display format
[Java] How to set the Date time to 00:00:00
[Java] How to get the current directory
How to get the date in java
How to get the current date as a string in yyyyMMdd format
[Java] Get and display the date 10 days later using the Time API added from Java 8.
[Java] Use ResolverStyle.LENIENT to handle the date and time nicely
Get the current date and time by specifying the time zone in Thymeleaf
[Java] How to convert from String to Path type and get the path
[Java] How to get and output standard input
How to get and study java SE8 Gold
[Java] How to get the redirected final URL
[Java] How to get the authority of the folder
How to create your own annotation in Java and get the value
[Java] How to use Calendar class and Date class
How to display today's date and time by default: Remove seconds or less
[Java] How to get the URL of the transition source
How to set the display time to Japan time in Rails
[Java] How to get the maximum value of HashMap
[Ruby] How to get the tens place and the ones place
[Java] How to display Wingdings
Java Unit Test Library-Determine if Artery-Date, Calendar, long is the current date and time (specify tolerance in minutes)
[Java] How to get the key and value stored in Map by iterative processing
How to get the class name / method name running in Java
[Java] Throw a request and display the screen ② (GET / POST)
Difference between Java and JavaScript (how to find the average)
What is the LocalDateTime class? [Java beginner] -Date and time class-
[Java] Throw a request and display the screen (GET / POST)
Sample code to parse date and time with Java SimpleDateFormat
How to use Java HttpClient (Get)
[Swift] Get the current time numerically
[MySQL] [java] Receive date and time
[Rails] How to get the URL of the transition source and redirect
[Swift5] How to get an array and the complement of arrays
The relationship between strict Java date checking and daylight savings time
How to get the length of an audio file in java
How to implement date calculation in Java
How to get date data in Ruby
[Java] How to use the File class
Introduction to java for the first time # 2
[Java] How to use the hasNext function
[Java] How to use the HashMap class
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
[Java] How to output and write files!
[Java] How to measure program execution time
Java 8 to start now ~ Date time API ~
[Processing × Java] How to use the class
How to install the legacy version [Java]
How to find the tens and ones
[Processing × Java] How to use the function
[Java] How to use the Calendar class
[Java8] Search the directory and get the file
[Java] Get the date with the LocalDateTime class
[Java] How to get to the front of a specific string using the String class
How to get the value after "_" in Windows batch like Java -version
How to get the absolute path of a directory running in Java
[Java improvement case] How to reach the limit of self-study and beyond
I translated the grammar of R and Java [Updated from time to time]
Which class should I use to get the date and time in my Rails app (Time, DateTime, TimeWithZone)