Getting Timestamps for Specific Dates and Times in Java

Chintan Joshi
5 min readMar 17, 2023

Timestamps are an important part of many applications that deal with date and time data. A timestamp is a numeric value that represents a specific point in time, usually measured in milliseconds since the Unix epoch (January 1, 1970, 00:00:00 UTC). In Java, timestamps are typically represented using the long data type, which can hold a value up to 2^63-1.

When working with timestamps in Java, it is often necessary to get the timestamp for a specific date and time, such as the start or end of a day, or a particular time of day. One way to do this is by using the Calendar class, which provides methods for working with dates and times in Java.

Creating a Calendar Object

To use the Calendar class, you first need to create a Calendar object. There are several ways to create a Calendar object, but one of the simplest is to use the Calendar.getInstance() method, which returns a Calendar object initialized to the current date and time in the default time zone and locale.

Calendar calendar = Calendar.getInstance();

Once you have a Calendar object, you can use its methods to set or get various date and time fields.

Setting a Specific Date

To set a specific date on a Calendar object, you can use the set() method, which takes two arguments: the field to set, and the value to set it to. The field is represented by a constant from the Calendar class, such as Calendar.YEAR, Calendar.MONTH, or Calendar.DAY_OF_MONTH. The value is an integer representing the desired value for the field.

For example, to set the date to January 1, 2023, you would use the following code:

calendar.set(Calendar.YEAR, 2023);
calendar.set(Calendar.MONTH, Calendar.JANUARY);
calendar.set(Calendar.DAY_OF_MONTH, 1);

Note that the Calendar.MONTH field is zero-based, so January is represented by the constant Calendar.JANUARY, which has a value of 0.

Setting a Specific Time

To set a specific time on a Calendar object, you can use the same set() method, but with different field constants. For example, to set the time to 3:45 PM, you would use the following code:

calendar.set(Calendar.HOUR_OF_DAY, 15);
calendar.set(Calendar.MINUTE, 45);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);

Sure, here’s the complete article:

Getting Timestamps for Specific Dates and Times in Java

Timestamps are an important part of many applications that deal with date and time data. A timestamp is a numeric value that represents a specific point in time, usually measured in milliseconds since the Unix epoch (January 1, 1970, 00:00:00 UTC). In Java, timestamps are typically represented using the long data type, which can hold a value up to 2^63-1.

When working with timestamps in Java, it is often necessary to get the timestamp for a specific date and time, such as the start or end of a day, or a particular time of day. One way to do this is by using the Calendar class, which provides methods for working with dates and times in Java.

Creating a Calendar Object

To use the Calendar class, you first need to create a Calendar object. There are several ways to create a Calendar object, but one of the simplest is to use the Calendar.getInstance() method, which returns a Calendar object initialized to the current date and time in the default time zone and locale.

Calendar calendar = Calendar.getInstance();

Once you have a Calendar object, you can use its methods to set or get various date and time fields.

Setting a Specific Date

To set a specific date on a Calendar object, you can use the set() method, which takes two arguments: the field to set, and the value to set it to. The field is represented by a constant from the Calendar class, such as Calendar.YEAR, Calendar.MONTH, or Calendar.DAY_OF_MONTH. The value is an integer representing the desired value for the field.

For example, to set the date to January 1, 2022, you would use the following code:

calendar.set(Calendar.YEAR, 2022);
calendar.set(Calendar.MONTH, Calendar.JANUARY);
calendar.set(Calendar.DAY_OF_MONTH, 1);

Note that the Calendar.MONTH field is zero-based, so January is represented by the constant Calendar.JANUARY, which has a value of 0.

Setting a Specific Time

To set a specific time on a Calendar object, you can use the same set() method, but with different field constants. For example, to set the time to 3:45 PM, you would use the following code:

calendar.set(Calendar.HOUR_OF_DAY, 15);
calendar.set(Calendar.MINUTE, 45);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);

The Calendar.HOUR_OF_DAY field represents the hour in 24-hour format, and takes values from 0 to 23. The Calendar.MINUTE field represents the minute, and takes values from 0 to 59. The Calendar.SECOND field represents the second, and takes values from 0 to 59. The Calendar.MILLISECOND field represents the millisecond, and takes values from 0 to 999.

Note that you must set the Calendar.SECOND and Calendar.MILLISECOND fields to 0 if you want to set the time to the beginning of the minute.

Getting a Timestamp

Once you have set the desired date and time fields on a Calendar object, you can get a timestamp for that date and time using the getTimeInMillis() method, which returns the number of milliseconds since the Unix epoch.

long timestamp = calendar.getTimeInMillis();

Using Timestamps

Once you have a timestamp for a specific date and time, you can use it in a variety of ways. One common use case is to compare timestamps to determine which is more recent, or to calculate the time elapsed between two timestamps.

Here is an example of comparing two timestamps:

long timestamp1 = calendar1.getTimeInMillis();
long timestamp2 = calendar2.getTimeInMillis();

if (timestamp1 > timestamp2) {
// timestamp1 is later than timestamp2
} else if (timestamp1 < timestamp2) {
// timestamp1 is earlier than timestamp2
} else {
// timestamp1 and timestamp2 are the same
}

Here is an example of calculating the time elapsed between two timestamps:

long timestamp1 = calendar1.getTimeInMillis();
long timestamp2 = calendar2.getTimeInMillis();

long elapsedTime = timestamp2 - timestamp1;

// convert elapsed time to seconds, minutes, hours, etc.
long seconds = elapsedTime / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
long days = hours / 24;

You can also use timestamps to represent time intervals or durations, rather than specific points in time. For example, you could represent a duration of 30 minutes as a timestamp value of 30 * 60 * 1000 (i.e. 1,800,000 milliseconds).

Conclusion

In this article, we’ve seen how to get timestamps for specific dates and times in Java using the Calendar class. We've also seen some examples of how to use timestamps in comparisons and calculations. Timestamps are a fundamental part of many Java applications that deal with date and time data, and understanding how to work with them is an important skill for any Java developer.

--

--

Chintan Joshi

Android developer. Proficient in Java/Kotlin. Passionate about creating user-friendly apps. Constantly exploring new tech. Open-source contributor.