Java Program to return a Date set to noon to the closest possible millisecond of the day



To set a Date object to noon to the closest possible millisecond of the day, we will make sure that the hour is set to noon, and both the minutes, seconds, and milliseconds are set to zero. This precise setting can be achieved using the Calendar class in Java.

By setting these specific fields, we can ensure that the time is 12:00:00.000 PM, providing the closest possible millisecond representation of noon.

Returning a date set to noon to the closest possible millisecond of the day in Java is quite easy. Let's learn the following ways:

Using Calendar Class

To return a Date set to noon to the closest possible millisecond of the day. We have a class named Calender Class in Java. It is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, etc and belongs to java.util package.

Example

In the example below, we initialize a Calendar object with the current date and time. Then, we adjust the time components to set the hour to noon and the minutes, seconds, and milliseconds to zero. It converts this adjusted Calendar object to a Date object.

import java.util.Calendar;
import java.util.Date;

public class Main {
   public static void main(String[] args) {
      Calendar calendar = Calendar.getInstance();
      calendar.set(Calendar.HOUR_OF_DAY, 12); // 12 PM
      calendar.set(Calendar.MINUTE, 0);
      calendar.set(Calendar.SECOND, 0);
      calendar.set(Calendar.MILLISECOND, 0);
      Date dateAtNoon = calendar.getTime();
      System.out.println("Date set to noon: " + dateAtNoon);
   }
}

Output

The above program produce the following result ?

Date set to noon: Tue Jan 07 12:00:00 GMT 2025

Using LocalDateTime and ZoneId Class

To return a Date set to noon to the closest possible millisecond of the day. We have another class as ZoneId class that represents a time zone identifier, for example, India/Europe. It provides a way to access the time zone rules for a particular region.

Example

Here, we initialize a LocalDateTime object with the current date and time and set it to noon with zeroed minutes, seconds, and nanoseconds. then converts this local date-time to a ZonedDateTime using the system's default time zone and converts it into a Date object.

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

public class Main {
   public static void main(String[] args) {
      LocalDateTime localNoon = LocalDateTime.now()
                                             .withHour(12)
                                             .withMinute(0)
                                             .withSecond(0)
                                             .withNano(0);
      ZonedDateTime zonedNoon = localNoon.atZone(ZoneId.systemDefault());	
      Date dateAtNoon = Date.from(zonedNoon.toInstant());	
      System.out.println("Date set to noon: " + dateAtNoon);
   }
}

Output

The above program produce the following result ?

Date set to noon: Tue Jan 07 12:00:00 GMT 2025

Using GregorianCalendar Class

The Gregorian Calendar class is another class in Java that concrete subclass of the Calendar class, and it provides the standard calendar system used by most of the world today.

Example

The program initializes a 'GregorianCalendar' object with the current date and time and sets the hour to noon and the minutes, seconds, and milliseconds to zero. then converts this calendar instance to a 'Date' object.

import java.util.GregorianCalendar;
import java.util.Date;
import java.util.Calendar;

public class Main {
   public static void main(String[] args) {
   
      GregorianCalendar calendar = new GregorianCalendar();
      
      calendar.set(Calendar.HOUR_OF_DAY, 12); 
      calendar.set(Calendar.MINUTE, 0);
      calendar.set(Calendar.SECOND, 0);
      calendar.set(Calendar.MILLISECOND, 0);
      
      Date dateAtNoon = calendar.getTime();
      System.out.println("Date set to noon: " + dateAtNoon);
   }
}

Output

The above program produce the following result ?

Date set to noon: Tue Jan 07 12:00:00 GMT 2025

Using getMinimum() Method

To return a Date set to noon to the closest possible millisecond of the day. We have a method called the getMinimum() method which belongs to the Calendar class in Java and returns the minimum value for the given calendar field.

Example

In this example, we initialize a 'Calendar' object with the current date and time, then set the hour to noon and the minutes, seconds, and milliseconds to their minimum values (zero).

import java.util.Calendar;

public class Main {
   public static void main(String[] argv) throws Exception {
      Calendar dateNoon = Calendar.getInstance();
      dateNoon.set(Calendar.HOUR_OF_DAY, 12);
      dateNoon.set(Calendar.MINUTE, dateNoon.getMinimum(Calendar.MINUTE));
      dateNoon.set(Calendar.SECOND, dateNoon.getMinimum(Calendar.SECOND));
      dateNoon.set(Calendar.MILLISECOND, dateNoon.getMinimum(Calendar.MILLISECOND));
      System.out.println("Date set to noon: " + dateNoon.getTime());
   }
}

Output

The above program produce the following result ?

Date set to noon: Tue Jan 07 12:00:00 GMT 2025
Revathi Satya Kondra
Revathi Satya Kondra

Technical Content Writer, Tutorialspoint

Updated on: 2025-01-14T03:35:32+05:30

321 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements