Java Get Current Date for a Specific Time Zone


In order to get the Date in Java for a specific TimeZone, you can make use of the LocalDate class from the Date-Time API and pass in the ZoneId for the timezone in the static now method.


Example:
import java.time.LocalDate;
import java.time.ZoneId;

public class GetDateTimeZoned {

    public static void main(String[] args) {
        
        LocalDate dateInChicago = LocalDate.now(ZoneId.of("America/Chicago"));
        LocalDate dateInMumbai = LocalDate.now(ZoneId.of("Asia/Kolkata"));
        LocalDate dateInTokyo = LocalDate.now(ZoneId.of("Asia/Tokyo"));
        LocalDate dateInLondon = LocalDate.now(ZoneId.of("Europe/London"));


        System.out.println("Date in Chicago " + dateInChicago);
        System.out.println("Date in Mumbai: " + dateInMumbai);
        System.out.println("Date in Tokyo: " + dateInTokyo);
        System.out.println("Date in London: " + dateInLondon);

    }
}

Output:

Date based on Time Zone Example Java

Make sure to pass in the correct timezone String or else you will get ZoneRulesException: Unknown time-zone exception.

You can find the complete list of TimeZone on this reference page: Time Zone and Off-Set List

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright ยฉ Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap