class | Year | Month | Day | time | zone | Use |
---|---|---|---|---|---|---|
ZonedDateTime | ○ | ○ | ○ | ○ | ○ | Strict date and time information |
LocalDateTime | ○ | ○ | ○ | ○ | X | Date and time information for daily use |
LocalDate | ○ | ○ | ○ | X | X | birthday |
LocalTime | X | X | X | ○ | X | Alarm time etc. |
Year | ○ | X | X | X | X | Year of publication, etc. |
YearMonth | ○ | ○ | X | X | X | Card expiration date, etc. |
Month | X | ○ | X | X | X | Settlement month, etc. |
MonthDay | X | ○ | ○ | X | X | Japanese holidays, etc. |
Of these, I used LocalDate to calculate after 10 days.
python
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
//Use Time API added from Java 8
//Get the date 10 days later
public class TenDaysCalendarNewAPI {
public static void main(String[] args) {
LocalDate now = LocalDate.now();
LocalDate future = now.plusDays(10);
//Display without specifying a format
System.out.println("■ Display LocalDate type as it is");
System.out.println(now);
System.out.println(future);
System.out.println();
//Display by specifying the format
System.out.println("■ Display using DateTimeFormatter");
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy year MM month dd day");
System.out.println(now.format(f));
System.out.println(future.format(f));
System.out.println();
System.out.println("■ Display using SimpleDateFormat");
SimpleDateFormat f2 = new SimpleDateFormat("yyyy year MM month dd day");
System.out.println(f.format(now));
System.out.println(f.format(future));
}
}
Execution result
■ Display LocalDate type as it is
2020-10-12
2020-10-22
■ Display using DateTimeFormatter
October 12, 2020
October 22, 2020
■ Display using SimpleDateFormat
October 12, 2020
October 22, 2020
I felt that it was easy to handle intuitively. That is when specifying the format,
SimpleDateFormat passes date information to SimpleDateFormat DateTimeFormatter specifies the format of DateTimeFormatter for date information
That's the part.
I don't know what's in it,
For myself
System.out.println(f.format(now));
It's hard to understand the part, or it's just that way, but I felt somehow uncomfortable.
System.out.println(now.format(f));
It was easier to understand the image like converting the date information of now into the information in the format passed to the argument of the format method
!
Recommended Posts