How to print date and time in java Program

public class SQLDateExample 
{
    public static void main(String[] args) 
    {
        long millis = System.currentTimeMillis();
        java.sql.Date date = new java.sql.Date(millis);
        System.out.println(date);
    }
}
// Another Way
import java.util.*;

class GetCurrentDateAndTime 
{
    public static void main(String args[]) 
    {
        int day, month, year;
        int second, minute, hour;

        GregorianCalendar date = new GregorianCalendar();
        day = date.get(Calendar.DAY_OF_MONTH); 
        month = date.get(Calendar.MONTH); 
        year = date.get(Calendar.YEAR);

        second = date.get(Calendar.SECOND); 
        minute = date.get(Calendar.MINUTE); 
        hour = date.get(Calendar.HOUR);

        System.out.println("Current date is " + day + "/" + (month + 1) + "/" + year); 
        System.out.println("Current time is " + hour + " : " + minute + " : " + second);
    }
}

1. SQLDateExample Program

This program is going to give an example of working with the use of the type java.sql.Date in dealing with SQL DATE types in Java.

public class SQLDateExample {

Public Static Void main(String[] Args) {

long millis = System.currentTimeMillis();  // Get the current time in milliseconds

java.sql.Date date = new java.sql.Date(millis);  // Convert milliseconds to a SQL Date

       System.out.println(date);  // Print the SQL Date

   }

}

  • It retrieves the current time in milliseconds from the system clock. System.currentTimeMillis() returns the number of milliseconds since the Unix epoch (January 1, 1970, 00:00:00 GMT).
  • java.sql.Date date = new java.sql.Date(millis);

This call passes the millis value-the number of milliseconds since the Unix epoch-to the constructor for the java.sql.Date class, which represents the date in SQL, not including the time.

The Date object is created using the current system time, but only the date (year, month, day) is represented by this object.

System.out.println(date);: This will print the java.sql.Date object. By default, java.sql.Date will be formatted as YYYY-MM-DD.

Output:

When you run this program, it will look something like this:

2025-01-27

This is the current date (the date when the program is run) in YYYY-MM-DD format.

2. GetCurrentDateAndTime Program

This program demonstrates how to get the current date and time using GregorianCalendar and Calendar classes.

Step-by-step explanation:

1. GregorianCalendar date = new GregorianCalendar();

This creates an instance of GregorianCalendar, which provides a concrete subclass of the Calendar class. It automatically initializes to the current date and time according to the system clock.

2. day = date.get(Calendar.DAY_OF_MONTH);:

This gets the current day of the month (from 1 to 31).

3. month = date.get(Calendar.MONTH);:

Retrieves the current month. Recall that the months in the Calendar class are 0-based; that is, January is 0, February is 1, and so on.

4. year = date.get(Calendar.YEAR);:

This retrieves the current year (such as 2025).

5. second = date.get(Calendar.SECOND);:

Retrieves the current second, ranging from 0 to 59.

6. minute = date.get(Calendar.MINUTE);:

This brings back the current minute (from 0 to 59).

7. hour = date.get(Calendar.HOUR);:

This brings back the current hour in 12-hour format (from 0 to 11).

8. System.out.println(“Current date is ” + day + “/” + (month + 1) + “/” + year);:

This will print the date. As the Calendar.MONTH returns zero-based index we add 1 to it to print it in proper order.

9. System.out.println(“Current time is ” + hour + ” : ” + minute + ” : ” + second);

This will print the current time in hour : minute : second format.

Output:

What you would expect to see from running this program is something like this:

Current date is 27/1/2025

Current time is 6 : 52 : 3

Note:

  • The current date is printed in day/month/year format.
  • The current time is printed in hour : minute : second format (12-hour format).

Summary:

1. SQLDateExample Program:

It uses System.currentTimeMillis() to obtain the current time in milliseconds and converts it into java.sql.Date, which is printed as YYYY-MM-DD.

2. GetCurrentDateAndTime Program:

It uses GregorianCalendar and Calendar to obtain the current day, month, year, hour, minute, and second, then prints them in a formatted output for date and time.

Two programs demonstrate how to make dates and times work in Java: the first using java.sql.Date, and the second using GregorianCalendar and Calendar.

Leave a Comment

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