M
M
Muhammad2016-01-09 07:24:49
Java
Muhammad, 2016-01-09 07:24:49

Find out the time in the time zone I need?

How can I find out the current time in the time zone I need? Or rather UTC-0?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Dorofeev, 2016-01-09
@TechCloud

We use the SimpleDateFormat class:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = format.parse("2015-01-23T09:10:00");

U
utor, 2016-01-09
@utor

in java.util.Date the time is stored in GMT, but using toString() adds the default TimeZone from the JVM settings see java.util.Date#toString and carefully java.util.Date#normalize. To get java.util.Date corrected:

Calendar c = Calendar.getInstance();
    TimeZone z = c.getTimeZone();
    int offset = z.getRawOffset();
    if(z.inDaylightTime(new Date())){
        offset = offset + z.getDSTSavings();
    }
    int offsetHrs = offset / 1000 / 60 / 60;
    int offsetMins = offset / 1000 / 60 % 60;
    c.add(Calendar.HOUR_OF_DAY, (-offsetHrs));
    c.add(Calendar.MINUTE, (-offsetMins));

   return с.getTime();

A
achist, 2016-01-11
@achist

Calendar calendar = Calendar.getInstance(TimeZone)
The calendar will just have the current time in the desired timezone, and then you can do anything with this calendar, for example, get a Long or Date representation.
TimeZone is also easy to get TimeZone.getTimeZone(ID), ID - zone identifier string

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question