Java 8 – How to get current Timestamp in different ways ?

In this article, we will learn how to get current Timestamp in different ways

Get Current Timestamp :

  1. Using System.currentTimeMillis() method
  2. Using Date Object
  3. Using Calendar instance

1. Using System.currentTimeMillis(); method :

  • Instantiate Timestamp object and pass System.currentTimeMillis(); in long type as constructorargument which will return current Timestamp in (yyyy-MM-dd HH:mm:ss.nnn) format as shown in the below illustration

GetCurrentTimestamp.java

package in.bench.resources.java8.current.date.time;

import java.sql.Timestamp;

public class GetCurrentTimestamp {

	public static void main(String[] args) {

		// get current Timestamp
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		System.out.print("Current Timestamp using System.currentTimeMillis() :- \n"
				+ timestamp);
	}
}

Output:

Current Timestamp using System.currentTimeMillis() :- 
2022-09-12 16:23:03.991

2. Using Date object :

  • Instantiate Date object which will return current Date/Time in the default (EEE MMM dd HH:mm:ss zzz yyyy) format
  • And then instantiate Timestamp object and pass Date object obtained from previous step in long type (using getTime() method) as constructorargument which will return current Timestamp in (yyyy-MM-dd HH:mm:ss.nnn) format as shown in the below illustration

GetCurrentTimestampUsingDateObject.java

package in.bench.resources.java8.current.date.time;

import java.sql.Timestamp;
import java.util.Date;

public class GetCurrentTimestampUsingDateObject {

	public static void main(String[] args) {

		// instantiate Date object
		Date date = new Date();
		System.out.println("Current Date/time is :- \n"
				+ date);


		// get current Timestamp using Date object
		Timestamp timestamp = new Timestamp(date.getTime());
		System.out.print("\nCurrent Timestamp using Date object :- \n" 
				+ timestamp);
	}
}

Output:

Current Date/time is :- 
Mon Sep 12 16:25:22 IST 2022

Current Timestamp using Date object :- 
2022-09-12 16:25:22.08

3. Using Calendar instance :

  • Get Calendar instance and convert Calendar instance to Date object using getTime() method of Calendar
  • And then instantiate Timestamp object and pass Date object obtained from previous step in long type (using getTime() method) as constructorargument which will return current Timestamp in (yyyy-MM-dd HH:mm:ss.nnn) format as shown in the below illustration

GetCurrentTimestampUsingCalendarInstance.java

package in.bench.resources.java8.current.date.time;

import java.sql.Timestamp;
import java.util.Calendar;

public class GetCurrentTimestampUsingCalendarInstance {

	public static void main(String[] args) {

		// get Calendar instance
		Calendar calendar = Calendar.getInstance();
		System.out.println("Current Date/time is :- \n"
				+ calendar.getTime());


		// get current Timestamp using Calendar instance
		Timestamp timestamp = new Timestamp(calendar.getTime().getTime());
		System.out.print("\nCurrent Timestamp using Calendar instance :- \n" 
				+ timestamp);
	}
}

Output:

Current Date/time is :- 
Mon Sep 12 16:27:22 IST 2022

Current Timestamp using Calendar instance :- 
2022-09-12 16:27:22.586

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to check whether a given Date/Year is leap year ?
Java 8 – How to get current Date in different ways ?