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.
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
.
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.
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