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.
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/
Recommended Posts