Wednesday, September 18, 2024

How to get current Date Timestamps in Java on GMT or Local Timezone? Example Tutorial

Getting current data and timestamps in java is a requirement for most java enterprise applications. The formatting date in java or parsing date in Java is such a common functionality that you will need it now and then. Though most of the programmers look at Google for finding current date and timestamps code in Java, it's worth to have an idea how you can do that by using java.util.Date class. sometimes you might need formatted data string say "yyyy-MM--dd:HH:mm:ss".


That is also possible by using dateformat in Java. I have touched base on string to date conversion and now in this article, we will see that in detail in terms of the current date, time-stamp, and timezone values

f you are working in a global core java application that runs on different timezones you must need to include timezone in your date and timestamp to avoid any confusion.

By the way, from JDK 8 onwards you can use modern classes like LocalTime, LocalDate, Instant, and ZonedDateTime to get the current date and timestamp in your local timzezon or any given timezone like UTC or GMT

Here is summary of 4 different ways to get the current timestamp in Java, it include both old and new ways

How to get current Date Timestamps in Java on GMT or Local Timezone Example



And, If you are new to the Java world then I also recommend you go through these Java Online Courses to learn Java in a better and more structured way. This is one of the best and up-to-date courses to learn Java online.


Find Current Date Timestamps in Java Timezone Example

find current date timestamp on different timezone in java exampleGMT timezone is sort of standard timezone for recording date and timestamp. if your application is not using any local timezone that you can use GMT for storing values on server. 

here is quick example of getting current date and timestamp value in GMT timezone in Java.



Example of Current Date TimeStamp in Java SimpleDateFormat


SimpleDateFormat gmtDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
gmtDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

//Current Date Time in GMT
System.out.println("Current Date and Time in GMT time zone: " + gmtDateFormat.format(new Date()));

Output:
Current Date and Time in GMT timezone: 2012-01-10 07:02:59




Code Example of current date and timestamp in GMT

Current timestamp and date in local time zone GMT is not always everybody requires some time you need that in your local timezone or any other time zone. what you need to do is reset timezone of date formatter to your local timezone. 

Here is another quick example of getting current date and timestamp values in local timezone:

SimpleDateFormat Example current Date Timestamps in Java

  
//Current Date time in Local time zone
SimpleDateFormat localDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//Current Date Time in Local Timezone
System.out.println("Current Date and Time in local timezone: " + localDateFormat.format( new Date()));

Output:
Current Date and Time in local timezone: 2012-01-10 02:32:59


Use LocalDate, LocalTime and ZonedDateTime from Java 8 onwards

From Java 8 onwards, you can also use LocalDate, LocalTime, and ZonedDateTime class to get the current date and timestamp in any timezone like GMT or UTC

Here is the code example you can use if you want current Date and Time in Java 17 or Java 21

public class ZoneDateTimeExample {

    public static void main(String[] args) {

        // Get the current instant in UTC (GMT)
        Instant currentInstant = Instant.now();

        // Get the current instant in the local time zone
        ZonedDateTime currentZonedDateTime = ZonedDateTime.now();

        // Print the timestamps
        System.out.println("Current UTC timestamp: " + currentInstant);
        System.out.println("Current local time zone timestamp: " 
               + currentZonedDateTime);

    }

}

By the way, GMT and UTC are same timezone so don't get confused with that. 



That’s  all on this quick tip of getting the current date and timestamp value on different timezones in Java. You just need to be a bit careful because DateFormat and SimpleDateFormat are not Thread-safe in Java and should not be shared between multiple threads, otherwise, it can create very subtle issues like incorrect Date.


Some other Quick tips on Java which you may like

2 comments: