Pages

Thursday, November 25, 2021

Java 8 – Convert LocalDateTime to Timestamp & Vice Versa

1. Overview

In this tutorial, We'll learn how to convert LocalDateTime object into the Timestamp in java 8 new Date Time API.

If you are new to Java 8 or above, It is recommended to read java 8 new date-time API in java indepth 

And also we will learn how to convert LocalDate to TimeStamp in java 8.

Java 8 – Convert LocalDateTime to Timestamp & Vice Versa


2. Java 8 - Convert LocalDateTime to Timestamp


Use Timestamp.valueOf(LocalDateTime time) method to transform the LocalDateTime into Timestamp.

Timestamp is from the java.sql package but LocalDateTime is from java.time package.

Example 1
package com.javaprogramto.java8.dates.conversion.totimestamp;

import java.sql.Timestamp;
import java.time.LocalDateTime;

public class LocalDateTimeToTimeStampExample {

	public static void main(String[] args) {
		
		
		LocalDateTime localDateTime = LocalDateTime.now();
		
		System.out.println("Local Date Time - "+localDateTime);
		
		Timestamp timestamp = Timestamp.valueOf(localDateTime);
		
		System.out.println("Timestamp from localdatetime is "+timestamp);

	}
}

Output:
Local Date Time - 2021-11-25T19:30:20.893767
Timestamp from localdatetime is 2021-11-25 19:30:20.893767

From the above output, we could see the same values in the localdatetime and timestamp objects.


3. Java 8 - Convert LocalDate to Timestamp


Use Timestamp.valueOf() method with LocalDate also to convert it into Timestamp.

Look at the below example.

Example 2
package com.javaprogramto.java8.dates.conversion.totimestamp;

import java.sql.Timestamp;
import java.time.LocalDate;

public class LocalDateToTimeStampExample {

	public static void main(String[] args) {
		
		
		LocalDate localDate = LocalDate.now();
		
		System.out.println("Local Date - "+localDate);
		
		Timestamp timestamp = Timestamp.valueOf(localDate.atStartOfDay());
		
		System.out.println("Timestamp from localdate is "+timestamp);

	}
}

Output
Local Date - 2021-11-25
Timestamp from localdate is 2021-11-25 00:00:00.0


4. Joda API - LocalDateTime and LocalDate to Timestamp


We can also work with joda time api and convert into the timestamp.

LocalDateTime and LocalDate classes are present in org.joda.time package.

Example 3
package com.javaprogramto.java8.dates.conversion.totimestamp;

import java.sql.Timestamp;

import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;

public class JodaLocalDateToTimeStampExample {

	public static void main(String[] args) {

		// joda localdate to timestamp
		LocalDate localDate = LocalDate.now();

		System.out.println("Joda Local Date - " + localDate);

		Timestamp timestamp = new Timestamp(localDate.toDateTimeAtStartOfDay().getMillis());

		System.out.println("Timestamp from joda localdate is " + timestamp);

		// joda localdatetime to timestamp
		LocalDateTime localDateTime = LocalDateTime.now();

		System.out.println("Joda Local Date Time - " + localDateTime);

		Timestamp timestamp2 = new Timestamp(localDateTime.toDateTime().getMillis());

		System.out.println("Timestamp from joda localdate is " + timestamp2);

	}
}

Output
Joda Local Date - 2021-11-25
Timestamp from joda localdate is 2021-11-25 00:00:00.0
Joda Local Date Time - 2021-11-25T19:42:21.736
Timestamp from joda localdate is 2021-11-25 19:42:21.736

5. Java 8 - Timestamp To LocalDateTime and LocalDate


Viceversa also can be done from timestamp to localdatetime and localdate objects.

Use Timestamp toLocalDate() and toLocalDate() methods.

Example 4
package com.javaprogramto.java8.dates.conversion.totimestamp;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;

public class TimeStampToLocalDateTimeExample {

	public static void main(String[] args) {
		
		Timestamp timestamp = Timestamp.valueOf("2022-01-01 10:10:10");
		System.out.println("Timestamp "+timestamp);
		
		LocalDateTime localDateTime = timestamp.toLocalDateTime();
		System.out.println("Timestamp to LocalDateTime - "+localDateTime);
		
		LocalDate localDate = timestamp.toLocalDateTime().toLocalDate();
		System.out.println("Timestamp to LocalDate - "+localDateTime);
		

	}
}

Output
Timestamp 2022-01-01 10:10:10.0
Timestamp to LocalDateTime - 2022-01-01T10:10:10
Timestamp to LocalDate - 2022-01-01T10:10:10

6. Conclusion


In this tutorial, We've seen how to convert LocalDateTime to Timestamp in java 8 and vice versa.

And also joda dates conversion to timestamp.



No comments:

Post a Comment

Please do not add any spam links in the comments section.