Write a Java program to display current date without time and current time without date


This Java program retrieves the current local date and time information using the LocalDate and LocalTime classes from the java.time package.

First, the current local date is obtained using the LocalDate.now() method, which returns a LocalDate object representing the current date. The LocalDate object is assigned to a variable d, which is then printed to the console using System.out.println().

Next, the current local time is obtained using the LocalTime.now() method, which returns a LocalTime object representing the current time. The LocalTime object is assigned to a variable t, which is then printed to the console using System.out.println().

Overall, this program is useful for retrieving and printing the current local date and time information, which can be useful for various applications that require date and time information. Note that the java.util.Date class is not used in this program, as it has been deprecated since Java 8 in favor of the new java.time classes.

Source Code

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Date;
 
class Current_DateTime
{
 
    public static void main(String[] args){        
        LocalDate d = LocalDate.now();        
        System.out.println("Current date = " + d);
 
        LocalTime t = LocalTime.now();        
        System.out.println("Current time = " + t);
    }
}

Output

Current date = 2022-11-05
Current time = 12:49:57.453471

Example Programs