Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current time in a given timezone : android

Tags:

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();
}
like image 778
Vishesh Joshi Avatar asked Apr 24 '13 21:04

Vishesh Joshi


People also ask

How do I get GMT time zone?

If the specified string doesn't match the syntax, "GMT" is used. For example, TimeZone. getTimeZone("GMT-8"). getID() returns "GMT-08:00".

What is time offset Javascript?

Javascript date getTimezoneOffset() method returns the time-zone offset in minutes for the current locale. The time-zone offset is the minutes in difference, the Greenwich Mean Time (GMT) is relative to your local time. For example, if your time zone is GMT+10, -600 will be returned.

How do I get the TimeZone on my calendar?

The getTimeZone() method in Calendar class is used to return the current time-zone of this Calendar. Parameters: The method does not take any parameters. Return Value: The method returns timezone of this Calendar object.


1 Answers

I got it to work like this :

TimeZone tz = TimeZone.getTimeZone("GMT+05:30");
Calendar c = Calendar.getInstance(tz);
String time = String.format("%02d" , c.get(Calendar.HOUR_OF_DAY))+":"+
            String.format("%02d" , c.get(Calendar.MINUTE))+":"+
.                   String.format("%02d" , c.get(Calendar.SECOND))+":"+
    .           String.format("%03d" , c.get(Calendar.MILLISECOND));

Also, every other time conversion based on this date should also be used with this timezone, otherwise, the default timezone of device will be used and the time will be converted based on that timezone.

like image 78
Vishesh Joshi Avatar answered Oct 24 '22 15:10

Vishesh Joshi