I am new to Android and I am currently facing an issue to get current time given the timezone.
I get timezone in the format "GMT-7" i.e. string. and I have the system time.
Is there a clean way to get the current time in the above given timezone? Any help is appreciated. Thanks,
edit : Trying to do this :
public String getTime(String timezone) {
Calendar c = Calendar.getInstance();
c.setTimeZone(TimeZone.getTimeZone(timezone));
Date date = c.getTime();
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
String strDate = df.format(date);
return c.getTime().toString();
}
Yes, you can. By call TimeZone
setDefault()
method.I got it to work like this :
Set the timezone to formatter, not calendar:
Try this:
YOUR_TIMEZONE may be something like: GMT, UTC, GMT-5, etc.
Cleanest way is with SimpleDateFormat
or you can specify the Locale
I found a better and simpler way.
First set time zone of app using
And then call Calander to get date internally it uses default timezone set by above throught app.
It will give current time based on time zone.
D/Los angeles time: Thu Jun 21 13:52:25 PDT 2018