- Class Date
- Constructor Summary
- Set Time to Start of Day or Midnight (00:00:00) in Java
- How to Set Time to Midnight (00:00:00) in Java
- Using java.util.Calendar class
- Using the java.time Package classes
- Using Joda-time
- Freqeuntly Asked Questions(FAQs)
- 1. How to set the time to midnight in Java?
- 2. How do you set a specific time on Java?
- 3. How to set time based on TimeZone Java?
- 4. How to set time in LocalDateTime Java?
- You may also like:
Class Date
Prior to JDK 1.1, the class Date had two additional functions. It allowed the interpretation of dates as year, month, day, hour, minute, and second values. It also allowed the formatting and parsing of date strings. Unfortunately, the API for these functions was not amenable to internationalization. As of JDK 1.1, the Calendar class should be used to convert between dates and time fields and the DateFormat class should be used to format and parse date strings. The corresponding methods in Date are deprecated.
Although the Date class is intended to reflect coordinated universal time (UTC), it may not do so exactly, depending on the host environment of the Java Virtual Machine. Nearly all modern operating systems assume that 1 day = 24 × 60 × 60 = 86400 seconds in all cases. In UTC, however, about once every year or two there is an extra second, called a «leap second.» The leap second is always added as the last second of the day, and always on December 31 or June 30. For example, the last minute of the year 1995 was 61 seconds long, thanks to an added leap second. Most computer clocks are not accurate enough to be able to reflect the leap-second distinction.
Some computer standards are defined in terms of Greenwich mean time (GMT), which is equivalent to universal time (UT). GMT is the «civil» name for the standard; UT is the «scientific» name for the same standard. The distinction between UTC and UT is that UTC is based on an atomic clock and UT is based on astronomical observations, which for all practical purposes is an invisibly fine hair to split. Because the earth’s rotation is not uniform (it slows down and speeds up in complicated ways), UT does not always flow uniformly. Leap seconds are introduced as needed into UTC so as to keep UTC within 0.9 seconds of UT1, which is a version of UT with certain corrections applied. There are other time and date systems as well; for example, the time scale used by the satellite-based global positioning system (GPS) is synchronized to UTC but is not adjusted for leap seconds. An interesting source of further information is the United States Naval Observatory (USNO):
and the material regarding «Systems of Time» at:
which has descriptions of various different time systems including UT, UT1, and UTC.
- A year y is represented by the integer y — 1900 .
- A month is represented by an integer from 0 to 11; 0 is January, 1 is February, and so forth; thus 11 is December.
- A date (day of month) is represented by an integer from 1 to 31 in the usual manner.
- An hour is represented by an integer from 0 to 23. Thus, the hour from midnight to 1 a.m. is hour 0, and the hour from noon to 1 p.m. is hour 12.
- A minute is represented by an integer from 0 to 59 in the usual manner.
- A second is represented by an integer from 0 to 61; the values 60 and 61 occur only for leap seconds and even then only in Java implementations that actually track leap seconds correctly. Because of the manner in which leap seconds are currently introduced, it is extremely unlikely that two leap seconds will occur in the same minute, but this specification follows the date and time conventions for ISO C.
In all cases, arguments given to methods for these purposes need not fall within the indicated ranges; for example, a date may be specified as January 32 and is interpreted as meaning February 1.
Constructor Summary
Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.
As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date) or GregorianCalendar(year + 1900, month, date) .
As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date, hrs, min) or GregorianCalendar(year + 1900, month, date, hrs, min) .
As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date, hrs, min, sec) or GregorianCalendar(year + 1900, month, date, hrs, min, sec) .
Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as «the epoch», namely January 1, 1970, 00:00:00 GMT.
Set Time to Start of Day or Midnight (00:00:00) in Java
Let me guess, you don’t know how to set the midnight time in Java?
You are lucky because in this article, we will cover how to initialize a date-time object with time set as the start of the day or midnight or at 00:00:00 AM in Java.
- We will do so using the legacy java.util.Date and Calendar class
- We will also cover how this can be achieved using Java 8 java.time classes like Instant , ZoneId , LocalDate etc.
When we say set time to start of day or midnight, we mean, if the date today is 3rd September 2019, and the time is any time of the say, when I run my code, I should get the date and time as 3rd September 2019 00:00:00 AM i.e. the time of the start of the current day.
How to Set Time to Midnight (00:00:00) in Java
Using java.util.Calendar class
This can be achieved using the Calendar class, but at times when we set the timezone to our specific timezone after creating a Calendar class instance, the results tend to vary. But if your usecase doesn’t worry about the timezone factor you can use this approach. Here is the code for it,
Using the java.time Package classes
With Java 8, the new java.time package was introduced which came with many better options than java.util.Date and java.util.Calendar class. The java.time package is close to joda-time library which provides many advanced functions for date and time processing.
Here is the code utilizing the java.time classes:
You can use the following code too, to get the same output. In the code below we have used java.time.LocalDateTime class doesn’t consider the timezone, so if you don’t care about the timezone and want minimum code for some sample application, use it.
import java.time.LocalTime; import java.time.LocalDateTime; LocalDateTime now = LocalDateTime.now(); System.out.println(now.with(LocalTime.MIN)); // 2019-09-03T00:00 System.out.println(now.with(LocalTime.MIDNIGHT)); // 2019-09-03T00:00
Using Joda-time
If your project uses the joda-time jar file, you can use the following code for accomplishing this, although we recommend using the second technique where we use the java.time package classes.
import org.joda.time.*; DateTime now = new DateTime(); DateTime firstMomentOfToday = now.withTimeAtStartOfDay(); System.out.println( "now: " + now ); System.out.println( "firstMomentOfToday: " + firstMomentOfToday );
now: 2019-09-03T23:00:23.785-08:00 firstMomentOfToday: 2019-09-03T00:00:00.000-08:00
Hope this article helped you with this. And one of the above-specified ways proved useful. If it did, then do share it around or bookmark it for later.
Freqeuntly Asked Questions(FAQs)
1. How to set the time to midnight in Java?
To set the time to midnight (00:00) in Java, you can use the LocalTime class and the of method to create a specific time instance.
LocalTime midnight = LocalTime.of(0, 0);
2. How do you set a specific time on Java?
To set a specific time in Java, you can use the LocalTime class and the of method to create a time instance with the desired hour, minute, and second values.
LocalTime specificTime = LocalTime.of(9, 30, 0);
3. How to set time based on TimeZone Java?
To set the time based on a specific time zone in Java, you can use the ZonedDateTime class along with the ZoneId class. Like the following code sets the current time based on the «America/New_York» time zone. —
ZoneId timeZone = ZoneId.of("America/New_York"); ZonedDateTime currentTime = ZonedDateTime.now(timeZone);
4. How to set time in LocalDateTime Java?
To set the time in a LocalDateTime object in Java, you can use the withHour , withMinute , and withSecond methods to modify the existing object. Here’s an example:
LocalDateTime dateTime = LocalDateTime.now(); LocalDateTime newDateTime = dateTime.withHour(10).withMinute(30).withSecond(0);
This example sets the time to 10:30:00, while keeping the date portion unchanged.