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
import java.util.*;
public class GFG {
public static void main(String args[])
{
Date current_Date = new Date();
System.out.println(current_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
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" );
Date date = new Date();
formatDate.setTimeZone(TimeZone.getTimeZone( "IST" ));
System.out.println(formatDate.format(date));
}
}
|
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
import java.time.*;
public class MyClass {
public static void main(String args[])
{
System.out.println(LocalDateTime.now());
}
}
|
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
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" ));
ZonedDateTime ISTtime = UTCtime.withZoneSameInstant(
ZoneId.of( "Asia/Kolkata" ));
System.out.println(ISTtime);
}
}
|
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
import java.time.*;
class GFG {
public static void main (String[] args)
{
System.out.println(Clock.systemUTC().instant());
}
}
|
Output
2020-11-20T07:12:37.598048Z
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
23 Mar, 2022
Like Article
Save Article