[Java] Get the date 10 days later with the Calendar class

TenDaysCalendar.java


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TenDaysCalendar {

	public static void main(String[] args) {
		//Get current date information
		Date now = new Date();
		//Set to Calendar
		Calendar c = Calendar.getInstance(); //Calendar does not use new
		c.setTime(now); //Date type as an argument
		
		//Try to display the date
		System.out.println("***********Current date and time***********");
		System.out.println("Calendar.DATE: " + Calendar.DATE); // 5
		System.out.println("c.get(Calendar.DATE): " + c.get(Calendar.DATE)); //Today's date
		
		//Output by specifying the display format
		SimpleDateFormat f = new SimpleDateFormat("yyyy year MM month dd day"); //Specify display format
		
		//I want to add 10 to the date and get the date 10 days later
		c.add(Calendar.DATE, 10);
		System.out.println();
		System.out.println("***********10 days later***********");
		System.out.println("Date 10 days later: " + f.format(c.getTime()));
		System.out.println("Calendar.DATE: " + Calendar.DATE); // 5
		System.out.println("c.get(Calendar.DATE): " + c.get(Calendar.DATE));
		
		//Set the date 10 days later and try to get it
		System.out.println();
		System.out.println("***********After another 10 days***********");
		System.out.println("c.set(Calendar.DATE, (Calendar.DATE + 10))Try to date as 10 days later");
		c.set(Calendar.DATE, (Calendar.DATE + 10));
		System.out.println("Date 10 days later: " + f.format(c.getTime()));
		System.out.println("Calendar.DATE is a constant of 5, so adding 10 gives 15 days.");
		
		System.out.println();
		System.out.println("c.set(Calendar.DATE, (c.get(Calendar.DATE) + 10))Try to date as 10 days later");
		c.set(Calendar.DATE, (c.get(5) + 10));
		System.out.println("Date 10 days later: " + f.format(c.getTime()));
		System.out.println("Since the DATE was set on the 15th above, add 10 to get the 25th.");
	}
}

Execution result


***********Current date and time***********
Calendar.DATE: 5
c.get(Calendar.DATE): 11

***********10 days later***********
Date 10 days later:October 21, 2020
Calendar.DATE: 5
c.get(Calendar.DATE): 21

***********After another 10 days***********
c.set(Calendar.DATE, (Calendar.DATE + 10))Try to date as 10 days later
Date 10 days later:October 15, 2020
Calendar.DATE is a constant of 5, so adding 10 gives 15 days.

c.set(Calendar.DATE, (c.get(Calendar.DATE) + 10))Try to date as 10 days later
Date 10 days later:October 13, 2020
Since the DATE was set on the 15th above, add 10 to get the 25th.

Calendar.DATE is a final constant and cannot be changed

Why can I do a set because it's final?

I couldn't understand this at all. I mean, I still don't understand it well. As a result of various verifications for the time being, there is a constant field value Calendar.DATE seems to contain a constant 5.

You can get the date by doing get


c.get(Calendar.DATE);

Then you can get the date properly. What do you mean? It was like that.

For the time being, Calendar.DATE is a constant 5, so

c.get(5);

I was able to get the date as.

Is the date information stored elsewhere instead of DATE? !!

I did some research, but I didn't understand, but when I passed 5 as an argument to get, The constant value of DATE has arrived! I have to return the date! So, I wonder if it's okay to have the image of fetching the date in another place.

Like doing on the basis

int name = "tanaka";
getName(){
  return name;
}

Is it different from the image of?

I will update the article as soon as I understand it, but for the time being, the above understanding was clear and there was no problem in handling it.

Recommended Posts

[Java] Get the date 10 days later with the Calendar class
[Java] Get the date with the LocalDateTime class
[Java] Get date information 10 days later using milliseconds in the Date class
Get date without using Calendar class
[Java1.8 +] Get the date of the next x day of the week with LocalDate
How to get the date in java
[Java] How to use the Calendar class
Display Japanese calendar and days of the week using java8 standard class
Ask for n business days later with JAVA
[Java] How to use Calendar class and Date class
[Java] Calculate the day of the week from the date (Calendar class is not used)
[Java] Get the file path in the folder with List
Why the get method is needed in the Calendar class
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
What is the LocalDateTime class? [Java beginner] -Date and time class-
[Android] Get the date on Monday
Follow the link with Selenium (Java)
Find the address class and address type from the IP address with Java [No. 2 decoction]
Let's express the result of analyzing Java bytecode with a class diagram
[Java] How to get the current date and time and specify the display format
[Java] How to get to the front of a specific string using the String class
[Java] Get MimeType from the contents of the file with Apathce Tika [Kotlin]
Get the result of POST in Java
Java date data type conversion (Date, Calendar, String)
[Java] Get List / Map elements with Iterator
[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
java Calendar class (time set, comparison, format)
Get Timestamp with Azure BlobStorage Java SDK
[Java8] Search the directory and get the file
Try using the Wii remote with Java
Java unit test Judgment of specified day of the week Determines whether ArDate, Date, Calendar, and long are specified days of the week.
Increment with the third argument of iterate method of Stream class added from Java9
[LeJOS] Let's control the EV3 motor with Java
[Java] Set the time from the browser with jsoup
Understanding the MVC framework with server-side Java 1/4 View
[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
Calculate the similarity score of strings with JAVA
[Java] Get images with Google Custom Search API
The Java neighborhood feels very good these days
Java type conversion (String, int, Date, Calendar, etc.)
The date time of java8 has been updated
[Java] How to get the redirected final URL
[Java] How to get the authority of the folder
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
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
You also need to specify the host when debugging remotely with Java 9 or later
What to do if you cannot execute with the command "Java package name / class name"