Signup/Sign In

Set Time to Start of Day or Midnight (00:00:00) in Java

Posted in Programming   LAST UPDATED: AUGUST 13, 2021

    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 and we will also cover how this can be achieved using the 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 Sptember 2019 00:00:00 AM i.e. the time of the start of the current day.


    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 utilising 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 which doesn't take into consideration 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 );
    

    Output:

    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.

    You may also like:

    About the author:
    This is the official profile of the Studytonight content team.
    Tags:JavaJava 8Java DateJava Time
    IF YOU LIKE IT, THEN SHARE IT

    Want to learn coding and don't know where to start?

    Try out our Interactive Courses for Free 🥳 😯 🤩
    learn to code footer Ad
     

    RELATED POSTS