In this article, we will learn how to convert Timestamp to Calendar and vice-versa in Java
Timestamp to Calendar conversion & vice-versa :
It is very simple and easy to convert Timestamp to Calendar and vice–versa using setTimeInMillis() & getTimeInMillis() methods of Calendar –
- Convert Timestamp to Calendar using setTimeInMillis() method
- First, instantiate Timestamp
- And then get Calendar instance using getInstance() method & pass timestamp object after converting into long using getTime() method in setTimeInMillis() method of Calendar
- Convert Calendar to Timestamp using getTimeInMillis() method
- First, get Calendar instance using getInstance() method
- And then instantiate Timestamp and pass Calendar instance after converting into long using getTimeInMillis() in Timestamp constructor
1. Convert Timestamp to Calendar :
- 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 Calendar conversion,
- Get Calendar instance using getInstance() method of Calendar
- And pass timestamp object after converting into long using getTime() method in setTimeInMillis() method of Calendar
- Finally, print Calendar/Date in different formats using DateFormat/SimpleDateFormat classes
ConvertJavaSqlTimestampToCalendar.java
package in.bench.resources.java8.date.examples;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class ConvertJavaSqlTimestampToCalendar {
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 Calendar
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timestamp.getTime());
System.out.println("\nConversion of Timestamp to Calendar :- \n"
+ calendar);
// 3. print Calendar/Date
System.out.println("\nConverted Calendar/Date is :- \n"
+ calendar.getTime());
// 3.1 DateFormatter 1
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String formattedTimestampInStr1 = dateFormat.format(calendar.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(calendar.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(calendar.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:01:00.786
Conversion of Timestamp to Calendar :-
java.util.GregorianCalendar[time=1661790660786,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=1,
SECOND=0,MILLISECOND=786,ZONE_OFFSET=19800000,DST_OFFSET=0]
Converted Calendar/Date is :-
Mon Aug 29 22:01:00 IST 2022
Formatted Date in (dd-MM-yyyy HH:mm:ss) format is :-
29-08-2022 22:01:00
Formatted Date in (dd/MMM/yy HH:mm) format is :-
29/Aug/22 22:01
Formatted Date in (EEEE dd MMMM, yyyy hh:mm:ss a z) format is :-
Monday 29 August, 2022 10:01:00 pm IST
2. Convert Calendar to Timestamp :
- First, get Calendar instance using getInstance() method of Calendar
- For Calendar to Timestamp conversion,
- Instantiate Timestamp object and pass Calendar instance 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 Calendar/Date in different formats using DateFormat/SimpleDateFormat classes
ConvertCalendarToJavaSqlTimestamp.java
package in.bench.resources.java8.date.examples;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class ConvertCalendarToJavaSqlTimestamp {
public static void main(String[] args) {
// 1. get Calendar instance
Calendar calendar = Calendar.getInstance();
System.out.println("Calendar instance is :- \n"
+ calendar);
// 2. print Calendar/Date
System.out.println("\nCalendar/Date is :- \n"
+ calendar.getTime());
// 3. convert Calendar to Timestamp
Timestamp timestamp = new Timestamp(calendar.getTimeInMillis());
System.out.println("\nConverion of Calendar 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:
Calendar instance is :-
java.util.GregorianCalendar[time=1661790695924,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=1,
SECOND=35,MILLISECOND=924,ZONE_OFFSET=19800000,DST_OFFSET=0]
Calendar/Date is :-
Mon Aug 29 22:01:35 IST 2022
Converion of Calendar to Timestamp :-
2022-08-29 22:01:35.924
Formatted Date in (dd-MM-yyyy HH:mm:ss) format is :-
29-08-2022 22:01:35
Formatted Date in (dd/MMM/yy HH:mm) format is :-
29/Aug/22 22:01
Formatted Date in (EEEE dd MMMM, yyyy hh:mm:ss a z) format is :-
Monday 29 August, 2022 10:01:35 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 !!