[Java] Get and display the date 10 days later using the Time API added from Java 8.

Review so far

Try using the newly added Time API

Newly added API

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.


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");
		//Display by specifying the format
		System.out.println("■ Display using DateTimeFormatter");
		DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy year MM month dd day");
		System.out.println("■ Display using SimpleDateFormat");
		SimpleDateFormat f2 = new SimpleDateFormat("yyyy year MM month dd day");


Execution result

■ Display LocalDate type as it is

■ Display using DateTimeFormatter
October 12, 2020
October 22, 2020

■ Display using SimpleDateFormat
October 12, 2020
October 22, 2020

The new API is easier to handle

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


It's hard to understand the part, or it's just that way, but I felt somehow uncomfortable.


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

[Java] Get and display the date 10 days later using the Time API added from Java 8.
[Java] Get date information 10 days later using milliseconds in the Date class
[Java] How to get the current date and time and specify the display format
[Java] Get the date 10 days later with the Calendar class
Display Japanese calendar and days of the week using java8 standard class
Date and time acquisition method using DateAndTime API
[Java] Throw a request and display the screen ② (GET / POST)
Handle Java 8 date and time API with Thymeleaf with Spring Boot
What is the LocalDateTime class? [Java beginner] -Date and time class-
[Java] Throw a request and display the screen (GET / POST)
Set the date and time from the character string with POI
[Java] Use ResolverStyle.LENIENT to handle the date and time nicely
Access Web API on Android with Get and process Json (Java for the time being)
[MySQL] [java] Receive date and time
Get the current date and time by specifying the time zone in Thymeleaf
The design concept of Java's Date and Time API is interesting
The relationship between strict Java date checking and daylight savings time
[Java] How to convert from String to Path type and get the path
Data processing using stream API from Java 8
[Java] Get Json from URL and handle it with standard API (javax.script)
Try using the Stream API in Java
Hit the Salesforce REST API from Java
[Java] How to set the Date time to 00:00:00
Java 8 to start now ~ Date time API ~
How to get the date in java
[Java] Generate a narrowed list from multiple lists using the Stream API
Try using the Emotion API from Android
I translated the grammar of R and Java [Updated from time to time]
[Java8] Search the directory and get the file
[Java] Get the date with the LocalDateTime class
[Java] Program example to get the maximum and minimum values from an array
Impressions and doubts about using java for the first time in Android Studio
Parse the date and time string formatted by the C asctime function in Java
[Deep Learning from scratch] in Java 1. For the time being, differentiation and partial differentiation
Format the date and time given by created_at
ChatWork4j for using the ChatWork API in Java
[Java] Set the time from the browser with jsoup
Try accessing the dataset from Java using JZOS
Display "Hello World" in the browser using Java
Display "Hello World" in the browser using Java
The date time of java8 has been updated
Try using the COTOHA API parsing in Java
[Android development] Get an image from the server in Java and set it in ImageView! !!
Output the maximum value from the array using Java standard output
[Java] I want to calculate the difference from the date
[Java] Sort the list using streams and lambda expressions
Java language from the perspective of Kotlin and C #
Learn for the first time java # 3 expressions and operators
Using the database (SQL Server 2014) from a Java program 2018/01/04
[Java] Date / time operations
Find the address class and address type from the IP address with Java
Get the next business day after the specified date in JAVA
JSON in Java and Jackson Part 1 Return JSON from the server
[Rails API + Vue] Upload and display images using Active Storage
Get to the abbreviations from 5 examples of iterating Java lists
Android app to select and display images from the gallery
[Ruby] Display today's day of the week using Date class
[Kotlin] Get Java Constructor / Method from KFunction and call it
Sample code to parse date and time with Java SimpleDateFormat
Correct the character code in Java and read from the URL
Tips for using Salesforce SOAP and Bulk API in Java