Java 8 – How to convert Date to Timestamp and vice-versa ?

In this article, we will learn how to convert Date to Timestamp and vice-versa in Java

Date to Timestamp conversion & vice-versa :

It is very simple and easy to convert Date to Timestamp and viceversa using their constructors

  • Convert Date to Timestamp using Date constructor passing time in millisecond in long format
    • Timestamp timestamp = new Timestamp(longTimeInMilliSecond);
  • Convert Timestamp to Date using Timestamp constructor passing time in millisecond in long format
    • Date date = new Date(longTimeInMilliSecond);

1. Convert Date to Timestamp :

  • Instantiate Date class which will return current Date/time/zone in (E MMM dd HH:mm:ss z yyyy) format
  • For Date to Timestamp conversion, instantiate Timestamp class passing above obtained Date in long format using getTime() method as shown in the below illustration
  • Timestamp prints current date/time information in (yyyy-MM-dd HH:mm:ss.nnn) format
  • Note: Timestamp is the sub-class of Date class
  • Finally, print Timestamp in different formats using DateFormat/SimpleDateFormat classes

ConvertJavaUtilDateToTimestamp.java

package in.bench.resources.java8.date.examples;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ConvertJavaUtilDateToTimestamp {

	public static void main(String[] args) {

		// 1. get current date/time using java.util.Date
		Date date = new Date();
		System.out.println("Current Date/time is :- \n" 
				+ date);


		// 2. convert Date -> Timestamp
		Timestamp timestamp = new Timestamp(date.getTime());
		System.out.println("\nTimestamp is :- \n" + timestamp);


		// 2.1 DateFormatter 1
		DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
		String formattedTimestampInStr1 = dateFormat.format(timestamp);
		System.out.println("\nFormatted Timestamp in (dd-MM-yyyy HH:mm:ss) format is :- \n" 
				+ formattedTimestampInStr1);


		// 2.2 DateFormatter 1
		dateFormat = new SimpleDateFormat("dd/MMM/yy HH:mm");
		String formattedTimestampInStr2 = dateFormat.format(timestamp);
		System.out.println("\nFormatted Timestamp in (dd/MMM/yy HH:mm) format is :- \n" 
				+ formattedTimestampInStr2);


		// 2.3 DateFormatter 1
		dateFormat = new SimpleDateFormat("EEEE dd MMMM, yyyy hh:mm:ss a z");
		String formattedTimestampInStr3 = dateFormat.format(timestamp);
		System.out.print("\nFormatted Timestamp in (EEEE dd MMMM, yyyy hh:mm:ss a z) format is :- \n" 
				+ formattedTimestampInStr3);
	}
}

Output:

Current Date/time is :- 
Fri Aug 26 19:46:48 IST 2022

Timestamp is :- 
2022-08-26 19:46:48.071

Formatted Timestamp in (dd-MM-yyyy HH:mm:ss) format is :- 
26-08-2022 19:46:48

Formatted Timestamp in (dd/MMM/yy HH:mm) format is :- 
26/Aug/22 19:46

Formatted Timestamp in (EEEE dd MMMM, yyyy hh:mm:ss a z) format is :- 
Friday 26 August, 2022 07:46:48 pm IST

2. Convert Timestamp to Date :

  • Instantiate Timestamp class and pass current date/time in millisecond using System.currentTimeMillis() which prints Timestamp (date/time) in (yyyy-MM-dd HH:mm:ss.nnn) format
  • For Timestamp to Date conversion, instantiate Date class passing above obtained Timestamp in long format using getTime() method as shown in the below illustration which will return current Date/time/zone in (E MMM dd HH:mm:ss z yyyy) format
  • Note: Date is the super-class of Timestamp class
  • Finally, print Timestamp (date/time) in different formats using DateFormat/SimpleDateFormat classes

ConvertTimestampToJavaUtilDate.java

package in.bench.resources.java8.date.examples;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ConvertTimestampToJavaUtilDate {

	public static void main(String[] args) {

		// 1. get current Timestamp
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		System.out.println("Timestamp is :- \n" + timestamp);


		// 2. convert Timestamp -> Date
		Date date = new Date(timestamp.getTime());
		System.out.println("\nCurrent Date/time is :- \n" + date);


		// 2.1 DateFormatter 1
		DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
		String formattedTimestampInStr1 = dateFormat.format(date);
		System.out.println("\nFormatted Date in (dd-MM-yyyy HH:mm:ss) format is :- \n" 
				+ formattedTimestampInStr1);


		// 2.2 DateFormatter 1
		dateFormat = new SimpleDateFormat("dd/MMM/yy HH:mm");
		String formattedTimestampInStr2 = dateFormat.format(date);
		System.out.println("\nFormatted Date in (dd/MMM/yy HH:mm) format is :- \n" 
				+ formattedTimestampInStr2);


		// 2.3 DateFormatter 1
		dateFormat = new SimpleDateFormat("EEEE dd MMMM, yyyy hh:mm:ss a z");
		String formattedTimestampInStr3 = dateFormat.format(date);
		System.out.print("\nFormatted Date in (EEEE dd MMMM, yyyy hh:mm:ss a z) format is :- \n" 
				+ formattedTimestampInStr3);
	}
}

Output:

Timestamp is :- 
2022-08-26 19:48:23.438

Current Date/time is :- 
Fri Aug 26 19:48:23 IST 2022

Formatted Date in (dd-MM-yyyy HH:mm:ss) format is :- 
26-08-2022 19:48:23

Formatted Date in (dd/MMM/yy HH:mm) format is :- 
26/Aug/22 19:48

Formatted Date in (EEEE dd MMMM, yyyy hh:mm:ss a z) format is :- 
Friday 26 August, 2022 07:48:23 pm IST

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert Date to Calendar and vice-versa ?
Java 8 – How to convert LocalTime to XMLGregorianCalendar and vice-versa ?