Java Program to Find Current Date and Time

We can find current date and time in Java in two ways. The first way is to use Date class and second way is to use Calendar class. Below I have shared Java program to find current date and time. First I am defining the date and time format using DateFormat and SimpleDateFormat class and then I am using format() and getTime() method to get current date and time. The program is self explainable but still if you are getting any difficulty then you ask in comment section.

Java Program to Find Current Date and Time

import java.util.*;
import java.text.*;

class DateTimeExample {
   public static void main(String...s) {
       DateFormat dateFormate = new SimpleDateFormat("dd-mm-yy hh:mm:ss");
       Date date = new Date();
       System.out.println(dateFormate.format(date));

       Calendar cal = Calendar.getInstance();
       System.out.println(dateFormate.format(cal.getTime()));
    }
}

 

Output

Java Program to Find Current Date and Time

Leave a Comment

Your email address will not be published. Required fields are marked *