In this article, we will learn how to get current Timestamp in different ways
Get Current Timestamp :
- Using System.currentTimeMillis() method
- Using Date Object
- Using Calendar instance
1. Using System.currentTimeMillis(); method :
- Instantiate Timestamp object and pass System.currentTimeMillis(); in long type as constructor–argument 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 constructor–argument 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 constructor–argument 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:
- Java 8 – How to get current Timestamp in different ways ?
- Java 8 – How to convert java.sql.Timestamp to LocalDate and vice-versa ?
- Java 8 – How to convert java.sql.Timestamp to LocalTime and vice-versa ?
- Java 8 – How to convert java.sql.Timestamp to LocalDateTime and vice-versa ?
- Java 8 – How to convert java.sql.Timestamp to ZonedDateTime and vice-versa ?
- Java 8 – How to convert java.sql.Timestamp to OffsetDateTime and vice-versa ?
- Java 8 – How to convert java.sql.Timestamp to Instant and vice-versa ?
- Java 8 – How to convert java.sql.Timestamp to java.util.Date and vice-versa ?
- Java 8 – How to convert java.sql.Timestamp to Calendar and vice-versa ?
- Java 8 – How to convert java.sql.Timestamp to GregorianCalendar and vice-versa ?
- Java 8 – How to convert java.sql.Timestamp to XMLGregorianCalendar and vice-versa ?
References:
- https://docs.oracle.com/javase/8/docs/api/java/sql/Timestamp.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!