[Java] Get date information 10 days later using milliseconds in the Date class

Practice handling the Date class

I was studying about Date,

I feel that there were some parts that I didn't understand very well in these two points, so I will summarize them.

Actually, it was swamped because the fields of the Calendar class were not well understood, and it seems that Java 8 has a new date API and it can be handled a little more intuitively, but it is also a primitive Date class. I felt that it was better to start understanding, so I tried to draw while verifying various things in my own way.

__ Version using Calendar class is also helpful __

Let's display the date information 10 days later using the Date class

TenDaysDate.java


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

public class TenDaysDate {

	public static void main(String[] args) {
		
		//Change the long value of Date type to get the date 10 days later
		Date d = new Date();									//Get the current date and time
		long ts = System.currentTimeMillis(); //Get the current epoch milliseconds
		
		//Compare the return value of getTime method of Date type with the value of currentTimeMillis
		System.out.println("d.getTime:\t\t\t" + d.getTime());				// Date.getTime value
		System.out.println("currentTimeMillis:\t" + ts);						//value of currentTimeMillis
		if(ts == d.getTime()) System.out.println("Together");			//long values match
		
		//Display Date type date information by specifying the format
		SimpleDateFormat f = new SimpleDateFormat("yyyy year MM month dd day"); //Specify display format
		System.out.println("Today's date:\t" + f.format(d));						//Pass Date type argument to SimpleDateFormat type
		
		//Add 10 days worth of milliseconds with the Date type setTime method and change to date information 10 days later.
		d.setTime(d.getTime() + (long)(24 * 60 * 60 * 1000) * 10);	// ()Is a millisecond of the day
		System.out.println("Date 10 days later:\t" + f.format(d));					//Pass to SimpleDateFormat type as an argument of Date type
	}

}

Output result


d.getTime:			1602415769204
currentTimeMillis:	1602415769204
Together
Today's date:October 11, 2020
Date 10 days later:October 21, 2020

get and set


d.getTime(); //Epoch milliseconds are returned
d.setTime(Epoch milliseconds); // Epoch millisecondsを引数に渡して日付情報をセットする

Epoch milliseconds

January 1, 1970 00:00:00 Milliseconds elapsed from UTC Also known as Unix Time One day's worth of milliseconds is 24 * 60 * 60 * 1000. Multiply this by 10 for 10 days worth of milliseconds.

Also, by setting getTime () + 24 * 60 * 60 * 1000 * 10, you can get the date information 10 days after the lapse of milliseconds from the current time.

Do you need to cast to a long type as (long) 24 * 60 * 60 * 1000 in some cases?

It may not be the date you expected depending on the year of the moist

If a leap year comes out 10 years later or something like that, even if the millisecond one year later is 24 * 60 * 60 * 1000 * 365 * 10, it will not be the same month and day 10 years later. ..

When calculating over a long span, you have to add it to the year field etc. It's complicated.

Recommended Posts

[Java] Get date information 10 days later using milliseconds in the Date class
[Java] Get the date 10 days later with the Calendar class
[Java] Get and display the date 10 days later using the Time API added from Java 8.
How to get the date in java
[Java] Get the date with the LocalDateTime class
Get EXIF information in Java
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
Get the next business day after the specified date in JAVA
Get date without using Calendar class
Get the result of POST in Java
Try using the Stream API in Java
[Java] How to get to the front of a specific string using the String class
Display Japanese calendar and days of the week using java8 standard class
ChatWork4j for using the ChatWork API in Java
When you get lost in the class name
How to get Class from Element in Java
Library "OSHI" to get system information in Java
Display "Hello World" in the browser using Java
Display "Hello World" in the browser using Java
Try using the COTOHA API parsing in Java
Get the URL of the HTTP redirect destination in Java
Get the name of the test case in the JUnit test class
Source used to get the redirect source URL in Java
[Java] Get the file in the jar regardless of the environment
[Java] Get the file path in the folder with List
Output of the day of the week using Ruby's Date class
Try global hooking in Java using the JNativeHook library
Date manipulation in Java 8
Differences in code when using the length system in Java
Why the get method is needed in the Calendar class
Summarize the additional elements of the Optional class in Java 9
Implement Thread in Java and try using anonymous class, lambda
What is the LocalDateTime class? [Java beginner] -Date and time class-
When using the constructor of Java's Date class, the date advances by 1900.
[Android, Java] Convenient method to calculate the difference in days
[Ruby] Display today's day of the week using Date class
Get error information using DefaultErrorAttributes and ErrorAttributeOptions in Spring Boot 2.3
Try using RocksDB in Java
[Java] Get KClass in Java [Kotlin]
View current date in Java
Examine the system information of AWS Lambda operating environment in Java
[Java1.8 +] Get the date of the next x day of the week with LocalDate
[Java] Get the dates of the past Monday and Sunday in order
Get the current date and time by specifying the time zone in Thymeleaf
[Java] Get data from DB using singleton service in Spring (Boot)
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
Examine the list of timezone IDs available in the Java ZoneId class
Get the public URL of a private Flickr file in Java
How to solve the unknown error when using slf4j in Java
How to get the length of an audio file in java
Which class should I use to get the date and time in my Rails app (Time, DateTime, TimeWithZone)
Access the network interface in Java
Guess the character code in Java
[Ethereum] Get block information using web3j
Date processing in Java (LocalDate: Initialization)
[Android] Get the date on Monday
Specify the java location in eclipse.ini
Encrypt using RSA cryptography in Java
How to learn JAVA in 7 days
Get Null-safe Map values in Java
Java comparison using the compareTo () method