[Java] Get the date with the LocalDateTime class

First you need to import

import java.time.LocalDateTime;

You need to declare the use of the package in. If you are using ** Eclipse **, it seems that the import statement will be inserted automatically if you instantiate LocalDateTime before declaring it. It's super convenient.

Let's get the current date

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; //This package declaration is required to specify the format

public class Sample_01 {

	public static void main(String[] args) {
		LocalDateTime d = LocalDateTime.now(); //Get an instance of the current time
//		LocalDateTime d = LocalDateTime.of(2016,  1, 1, 10. 10, 10); //Get an instance for a specific date
//		LocalDateTime d = LocalDateTime.parse("2016-02-02T10:10:10"); //Get an instance of a specific date in the format specified by iso
//		
		System.out.println("d: " + d);
		System.out.println("getyear(): " + d.getYear()); //Get the year
		System.out.println("getMonth(): " + d.getMonth()); //Get the year
		System.out.println("getMonth().getValue(): " + d.getMonth().getValue()); //Get the year
		System.out.println("plusMonths(2).minusDays(3): " + d.plusMonths(2).minusDays(3)); //Get the year
		LocalDateTime nd = d.plusMonths(2).minusDays(3); //Date calculation
		System.out.println("nd: " + nd); //Output in a variable
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd/");
		System.out.println("DateTimeFormatter.ofPattaern: " + d.format(dtf));
	}

}

Execution result


d: 2020-09-12T14:42:11.438
getyear(): 2020
getMonth(): SEPTEMBER
getMonth().getValue(): 9
plusMonths(2).minusDays(3): 2020-11-09T14:42:11.438
nd: 2020-11-09T14:42:11.438
DateTimeFormatter.ofPattaern: 2020/09/12/

point

Recommended Posts

[Java] Get the date with the LocalDateTime class
[Java] Get the date 10 days later with the Calendar class
How to get the date in java
[Java] Get date information 10 days later using milliseconds in the Date class
[Java] Get the file path in the folder with List
Get started with serverless Java with the lightweight framework Micronaut!
How to get the class name / method name running in Java
Find the address class and address type from the IP address with Java
Get the next business day after the specified date in JAVA
Create an immutable class with JAVA
Get date without using Calendar class
[Android] Get the date on Monday
Follow the link with Selenium (Java)
Get the result of POST in Java
[Java] Get List / Map elements with Iterator
[Java] How to use the File class
[Java] Get the day of the specific day of the week
[Java] How to use the HashMap class
[Java] How to set the Date time to 00:00:00
[Java] How to get the current directory
[Processing × Java] How to use the class
Get Timestamp with Azure BlobStorage Java SDK
[Java] How to use the Calendar class
[Java8] Search the directory and get the file
Find the address class and address type from the IP address with Java [No. 2 decoction]
[Java] How to get the current date and time and specify the display format
[Java] Get MimeType from the contents of the file with Apathce Tika [Kotlin]
Java class methods
[Java] Class inheritance
[LeJOS] Let's control the EV3 motor with Java
java Scanner class
[Java] Set the time from the browser with jsoup
[Java] Get and display the date 10 days later using the Time API added from Java 8.
Understanding the MVC framework with server-side Java 1/4 View
When you get lost in the class name
[Java] Get the length of the surrogate pair string
java (abstract class)
[JAVA] Get only the file name, excluding the extension
Understanding the MVC framework with server-side Java 3/4 Controller
Get block information with Hyperledger Iroha's Java SDK
How to get Class from Element in Java
[Java] Nested class
Java anonymous class
[Java] Get images with Google Custom Search API
Increment with the third argument of iterate method of Stream class added from Java9
About Java class
The date time of java8 has been updated
[Java] How to get the redirected final URL
[java] abstract class
[Java] Calculate the day of the week from the date (Calendar class is not used)
Java local class
Java starting with JShell-A peek into the Java world
[LeJOS] Get EV3 sensor value remotely with Java
Understanding the MVC framework with server-side Java 2/4 Model
First touch of the Files class (or Java 8)
Get along with Java containers in Cloud Run
[Java] How to use Calendar class and Date class
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
What to do if you cannot execute with the command "Java package name / class name"
[Java] How to get the URL of the transition source
Get the URL of the HTTP redirect destination in Java