Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
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");
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();
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 questionAsk a Question
731 491 924 answers to any question