1. Introduction
In this tutorial we’ll learn how to convert a java.sql.Timestamp object to a java.util.Calendar object.
First, we’ll see how Java’s two classes, Timestamp and Calendar, handle time. We’ll then explore the common use-cases for performing the conversion. Finally, we’ll examine how to convert from one object to the other.
2. Time in Java
In general, we deal with time expressed in milliseconds, where one millisecond equals a thousandth of a second. If we need more precision, another commonly used precision is nanoseconds (billionths of a second).
The java.sql.Timestamp class is a subclass of the java.util.Date object with an integer representation of nanoseconds. It’s a class that is intended to be used with timestamp data types coming from SQL databases. Time is expressed as the number of milliseconds since January 1, 1970, 00:00:00 GMT, with the aforementioned fractional seconds field added to increase precision.
On the other hand, the java.util.Calendar class allows us to extract day, month, or year information from a Timestamp. We can also use it to get data about time in the future, or to extend our own Calendar implementation. Calendar also doesn’t handle precision on a level higher than milliseconds, which we’ll explore later.
3. Converting Time Objects
The prime use case for converting the two types is when we need to extract a timestamp field from a database. Our application might need to display a value to a user or take action when a timestamp falls between a certain time range. Normally, we’d try and use the newer, more functional java.time classes introduced in Java 8, but with some legacy APIs that isn’t always possible.
3.1. Timestamp to Calendar
In either case, once we have a Timestamp object, we can convert it to a Calendar:
Timestamp timestamp = new Timestamp(1713544200801L);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timestamp.getTime());
As we see above, we can set the Calendar‘s time using the Timestamp‘s getTime() method. We’ve created a Timestamp using a sample a millisecond value for simplicity’s sake. To ensure that we’ve done the proper conversion, we can even test it:
assertEquals(calendar.getTimeInMillis(), timestamp.getTime());
Once we’ve verified that our conversion is correct, we can continue to use our Calendar as needed.
3.2. Calendar to Timestamp
Our test ensures the milliseconds for both objects match. However, it’s important to note that Timestamp is precise to the nanosecond, and when we convert the Calendar back to a Timestamp we lose that precision:
int nanos = 801789562;
int losslessNanos = 801000000;
Timestamp timestamp = new Timestamp(1713544200801L);
timestamp.setNanos(nanos);
assertEquals(nanos, timestamp.getNanos());
Calendar calendar = SqlTimestampToCalendarConverter.timestampToCalendar(timestamp);
timestamp = SqlTimestampToCalendarConverter.calendarToTimestamp(calendar);
assertEquals(losslessNanos, timestamp.getNanos());
Here, when calling the getTime() method of the Timestamp to convert it to milliseconds, its nanoseconds field is internally divided by 1,000,000 using integer division. In doing so, we end up with 801 milliseconds reduced from the original 801789562 nanoseconds value.
So, we should use another solution if our task requires nanosecond precision.
4. Conclusion
In this article, we learned that Timestamp and Calendar both handle time in terms of milliseconds since epoch time. In addition, we discovered that the Timestamp class tracks nanoseconds, which might be useful based on our requirements.
By exploring the usage of these objects, we learned how to convert a Timestamp to a Calendar, which is useful when interacting with specific calendar fields, like day or month.
As always, the full source code of our examples is available over on GitHub.