In this article, we will learn how to convert Timestamp to GregorianCalendar and vice-versa in Java
Timestamp to GregorianCalendar conversion & vice-versa :
It is very simple and easy to convert Timestamp to GregorianCalendar and vice–versa using setTimeInMillis() & getTimeInMillis() methods of GregorianCalendar –
- Convert Timestamp to GregorianCalendar using setTimeInMillis() method
- First, instantiate Timestamp
- And then instantiate GregorianCalendar object & pass timestamp object after converting into long using getTime() method in setTimeInMillis() method of Calendar
- Convert GregorianCalendar to Timestamp using getTimeInMillis() method
- First, instantiate GregorianCalendar object
- And then instantiate Timestamp and pass GregorianCalendar object after converting into long using getTimeInMillis() in Timestamp constructor
1. Convert Timestamp to GregorianCalendar :
- Instantiate Timestamp class and pass current date/time in millisecond using System.currentTimeMillis() which prints current Timestamp (date/time) in (yyyy-MM-dd HH:mm:ss.nnn) format
- For Timestamp to GregorianCalendar conversion,
- Instantiate GregorianCalendar object
- And pass timestamp object after converting into long using getTime() method in setTimeInMillis() method of GregorianCalendar
- Finally, print GregorianCalendar/Date in different formats using DateFormat/SimpleDateFormat classes
ConvertJavaSqlTimestampToGregorianCalendar.java
package in.bench.resources.java8.date.examples;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
public class ConvertJavaSqlTimestampToGregorianCalendar {
public static void main(String[] args) {
// 1. get Timestamp
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
System.out.println("Current Timestamp :- \n" + timestamp);
// 2. convert Timestamp to GregorianCalendar
GregorianCalendar gregorianCalendar = new GregorianCalendar();
gregorianCalendar.setTimeInMillis(timestamp.getTime());
System.out.println("\nConversion of Timestamp to GregorianCalendar :- \n"
+ gregorianCalendar);
// 3. print GregorianCalendar/Calendar/Date
System.out.println("\nConverted GregorianCalendar/Calendar/Date is :- \n"
+ gregorianCalendar.getTime());
// 3.1 DateFormatter 1
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String formattedTimestampInStr1 = dateFormat.format(gregorianCalendar.getTime());
System.out.println("\nFormatted Date in (dd-MM-yyyy HH:mm:ss) format is :- \n"
+ formattedTimestampInStr1);
// 3.2 DateFormatter 1
dateFormat = new SimpleDateFormat("dd/MMM/yy HH:mm");
String formattedTimestampInStr2 = dateFormat.format(gregorianCalendar.getTime());
System.out.println("\nFormatted Date in (dd/MMM/yy HH:mm) format is :- \n"
+ formattedTimestampInStr2);
// 3.3 DateFormatter 1
dateFormat = new SimpleDateFormat("EEEE dd MMMM, yyyy hh:mm:ss a z");
String formattedTimestampInStr3 = dateFormat.format(gregorianCalendar.getTime());
System.out.print("\nFormatted Date in (EEEE dd MMMM, yyyy hh:mm:ss a z) format is :- \n"
+ formattedTimestampInStr3);
}
}
Output:
Current Timestamp :-
2022-08-29 22:49:35.6
Conversion of Timestamp to GregorianCalendar :-
java.util.GregorianCalendar[time=1661793575600,areFieldsSet=true,areAllFieldsSet=true,
lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,
dstSavings=0,useDaylight=false,transitions=7,lastRule=null],firstDayOfWeek=1,
minimalDaysInFirstWeek=1,ERA=1,YEAR=2022,MONTH=7,WEEK_OF_YEAR=36,
WEEK_OF_MONTH=5,DAY_OF_MONTH=29,DAY_OF_YEAR=241,DAY_OF_WEEK=2,
DAY_OF_WEEK_IN_MONTH=5,AM_PM=1,HOUR=10,HOUR_OF_DAY=22,MINUTE=49,
SECOND=35,MILLISECOND=600,ZONE_OFFSET=19800000,DST_OFFSET=0]
Converted GregorianCalendar/Calendar/Date is :-
Mon Aug 29 22:49:35 IST 2022
Formatted Date in (dd-MM-yyyy HH:mm:ss) format is :-
29-08-2022 22:49:35
Formatted Date in (dd/MMM/yy HH:mm) format is :-
29/Aug/22 22:49
Formatted Date in (EEEE dd MMMM, yyyy hh:mm:ss a z) format is :-
Monday 29 August, 2022 10:49:35 pm IST
2. Convert GregorianCalendar to Timestamp :
- First, instantiate GregorianCalendar object
- For GregorianCalendar to Timestamp conversion,
- Instantiate Timestamp object and pass GregorianCalendar object after converting in long using getTimeInMillis() method in Timestamp’s constructor which will return Timestamp in (yyyy-MM-dd HH:mm:ss.nnn) format
- Finally, print GregorianCalendar/Date in different formats using DateFormat/SimpleDateFormat classes
ConvertGregorianCalendarToJavaSqlTimestamp.java
package in.bench.resources.java8.date.examples;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
public class ConvertGregorianCalendarToJavaSqlTimestamp {
public static void main(String[] args) {
// 1. get GregorianCalendar instance
GregorianCalendar gregorianCalendar = new GregorianCalendar();
System.out.println("GregorianCalendar instance is :- \n"
+ gregorianCalendar);
// 2. print Calendar/Date
System.out.println("\nGregorianCalendar/Calendar/Date is :- \n"
+ gregorianCalendar.getTime());
// 3. convert GregorianCalendar to Timestamp
Timestamp timestamp = new Timestamp(gregorianCalendar.getTimeInMillis());
System.out.println("\nConverion of GregorianCalendar to Timestamp :- \n"
+ timestamp);
// 3.1 DateFormatter 1
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String formattedTimestampInStr1 = dateFormat.format(timestamp);
System.out.println("\nFormatted Date in (dd-MM-yyyy HH:mm:ss) format is :- \n"
+ formattedTimestampInStr1);
// 3.2 DateFormatter 1
dateFormat = new SimpleDateFormat("dd/MMM/yy HH:mm");
String formattedTimestampInStr2 = dateFormat.format(timestamp);
System.out.println("\nFormatted Date in (dd/MMM/yy HH:mm) format is :- \n"
+ formattedTimestampInStr2);
// 3.3 DateFormatter 1
dateFormat = new SimpleDateFormat("EEEE dd MMMM, yyyy hh:mm:ss a z");
String formattedTimestampInStr3 = dateFormat.format(timestamp);
System.out.print("\nFormatted Date in (EEEE dd MMMM, yyyy hh:mm:ss a z) format is :- \n"
+ formattedTimestampInStr3);
}
}
Output:
GregorianCalendar instance is :-
java.util.GregorianCalendar[time=1661793543651,areFieldsSet=true,areAllFieldsSet=true,
lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,
dstSavings=0,useDaylight=false,transitions=7,lastRule=null],firstDayOfWeek=1,
minimalDaysInFirstWeek=1,ERA=1,YEAR=2022,MONTH=7,WEEK_OF_YEAR=36,
WEEK_OF_MONTH=5,DAY_OF_MONTH=29,DAY_OF_YEAR=241,DAY_OF_WEEK=2,
DAY_OF_WEEK_IN_MONTH=5,AM_PM=1,HOUR=10,HOUR_OF_DAY=22,MINUTE=49,
SECOND=3,MILLISECOND=651,ZONE_OFFSET=19800000,DST_OFFSET=0]
GregorianCalendar/Calendar/Date is :-
Mon Aug 29 22:49:03 IST 2022
Converion of GregorianCalendar to Timestamp :-
2022-08-29 22:49:03.651
Formatted Date in (dd-MM-yyyy HH:mm:ss) format is :-
29-08-2022 22:49:03
Formatted Date in (dd/MMM/yy HH:mm) format is :-
29/Aug/22 22:49
Formatted Date in (EEEE dd MMMM, yyyy hh:mm:ss a z) format is :-
Monday 29 August, 2022 10:49:03 pm IST
Related Articles:
- 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 ?
- Java – How to convert java.util.Date to String in different formats ?
- Java – How to convert String to java.util.Date in different formats ?
- Java – How to get Date/time with AM/PM marker and Zone ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/sql/Timestamp.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
- https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html
- https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html
- https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Happy Coding !!
Happy Learning !!