Open In App

Java Program to Display Current Date and Time

Improve
Improve
Like Article
Like
Save
Share
Report

Java is a most powerful programming language, by which we can do many things and Java is an industry preferable language. So it has a huge field of features. Here we discuss one of the best features of Java, that is how to represent the current date and time using Java.

There are many ways to do this, there are many classes by which it can be possible to display the current Date and Time. 

Method 1: Using Date class

There is a class called Date class which can represent the current date and time in GMT form. We can get the IST form by adding 5 hours and 30 minutes to the GMT form. This class comes under the util package of Java.

Implementation:

Java




// Java Program to Display Current Date and Time
import java.util.*;
 
public class GFG {
    public static void main(String args[])
    {
        Date current_Date = new Date();
        //"Date" class
        //"current_Date" is Date object
 
        System.out.println(current_Date);
        // print the time and date
    }
}


Output

Fri Nov 20 07:12:42 UTC 2020

Conversion of GMT form to IST using two classes called TimeZone, it also comes under util package of Java, and SimpleDateFormat, which comes under Text package of Java.

Implementation:

Java




// Java Program to Display Current Date and Time
import java.text.*;
import java.util.*;
public class GFG {
    public static void main(String args[])
    {
        SimpleDateFormat formatDate = new SimpleDateFormat(
            "dd/MM/yyyy  HH:mm:ss z");
        //"SimpleDateFormat" class initialize with object
        //"formatDate" this class acceptes the format of
        // date and time as ""dd/MM/yyyy" and "HH:mm:ss z""
        //"z" use for print the time zone
 
        Date date = new Date();
        // initialize "Date" class
 
        formatDate.setTimeZone(TimeZone.getTimeZone("IST"));
        // converting to IST or format the Date as IST
 
        System.out.println(formatDate.format(date));
        // print formatted date and time
    }
}


Output

20/11/2020  12:42:41 IST

Method 2: Using LocalDateTime class

There is a class called LocalDateTime class which can also represent the current date and time in GMT form. We can get the IST form by adding 5 hours and 30 minutes to the GMT form. This class comes under the Time package of Java.

Implementation:

Java




// Java Program to Display Current Date and Time
import java.time.*;
public class MyClass {
    public static void main(String args[])
    {
        System.out.println(LocalDateTime.now());
        //"LocalDateTime" is the class
        //"now()" is a method, represent the
        // current date and time
    }
}


Output

2020-11-20T07:12:43.158549

We can convert this GMT form to IST using a class called ZonedDateTime, it also comes under Time package of Java. This class accepts a time by specifying a time zone and convert it to a specific time zone. Here we convert the time to Asia/Kolkata form.

Implementation:

Java




// Java Program to Display Current Date and Time
import java.util.*;
import java.time.*;
public class GFG {
    public static void main(String[] args)
    {
        Date date = new Date();
 
        LocalDateTime d = LocalDateTime.now();
 
        ZonedDateTime UTCtime = d.atZone(ZoneId.of("UTC"));
        //"d" is the current date and
        //"ZonedDateTime" accepts "d" as UTC
        //"atZone" specifies the time zone
 
        // converting to IST
        ZonedDateTime ISTtime = UTCtime.withZoneSameInstant(
            ZoneId.of("Asia/Kolkata"));
        //"withZoneSameInstant" convert the time
        // to given time zone
 
        System.out.println(ISTtime);
        // print the time and date
    }
}


Output

2020-11-20T12:42:42.723246+05:30[Asia/Kolkata]

Method 3: Using Clock class

There is a class called Clock class which can also represent the current date and time in UTC form. This class comes under the Time package of Java.

Implementation:

Java




/*package whatever //do not write package name here */
 
import java.time.*;
 
class GFG {
    public static void main (String[] args)
    {
        System.out.println(Clock.systemUTC().instant());
        //"Clock" is the class
        //"systemUTC()" is the method which represent the time in UTC form
    }
}


Output

2020-11-20T07:12:37.598048Z


Last Updated : 23 Mar, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads