Sriram
Sriram

Reputation: 2999

java - Getting current date and time based on the locale (country)

I would like to fetch the current date / time based on the locale. If I pass locale object, I need to get the relevant date / time of the country.

Upvotes: 2

Views: 12181

Answers (3)

dehasi
dehasi

Reputation: 2773

Since Java 8 you have LocalDateTime and ZonedDateTime

So you can do the same as @Babar said:

Get a zone id

ZoneId zoneId = ZoneId.of("Europe/Brussels");

And then get date and time

ZonedDateTime zonedDateTime = ZonedDateTime.now(zoneId);

Upvotes: 2

Babar
Babar

Reputation: 2826

If you use Joda Time then following should work. (Don't have the compiler at hand)

DateTimeZone tz = DateTimeZone.forID("America/Winnipeg");
DateTime dt = new DateTime(tz);

You can get the Timezone Ids here. Please check the link given by dantuch too.

Upvotes: 0

zawhtut
zawhtut

Reputation: 8561

    public static void main(String[] arg){
        String[] timeZoneIDList = TimeZone.getAvailableIDs();
        //List of Time Zones
        for(String timeZoneID:timeZoneIDList){
            System.out.println(timeZoneID);
        }
        //What time is it in Fiji
        Calendar fijiCalendar = Calendar.getInstance(TimeZone.getTimeZone("Pacific/Fiji"));
        System.out.println("Time in Fiji->"+fijiCalendar.get(Calendar.HOUR)+":"+fijiCalendar.get(Calendar.MINUTE));
    }

Upvotes: 0

Related Questions