Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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:

public static Timestamp calendarToTimestamp(Calendar calendar) {
    return new Timestamp(calendar.getTimeInMillis());
}
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.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

2 Comments
Oldest
Newest