Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get time with respect to another timezone in android [duplicate]

I want to create an alarm application for FIFA 2014 world cup matches in which i have a server which stores the date and time for the matches in Brazil/Acre and my client is an android application and an android device may have any of the possible timezone so the my problem is i want to convert the Brazil/Acre timing to the local android device timing with different timezone and after lot of googled i came to know about joda data and time lib but it is too slow in android so please suggest any code that will work for me.

like image 784
Neeraj Sharma Avatar asked May 31 '14 10:05

Neeraj Sharma


People also ask

How do I change the time zone of an Android device?

Perfecto exposes the Android commands that change the time zone configuration of an Android device in the CQ lab. Perfecto offers three commands: mobile:timezone:get - returns current configured time zone as a String object. mobile:timezone:set - changes device configuration to new time zone.

How to get the current time of a particular timezone?

In order to get the current time of a particular timezone there is a need to use the pytz Python library. Some of the important commands of pytz library are utc – it helps to get the standard UTC time zone timezone () – it helps to get the time zone of a particular location

How to get current time of different time zones in Python?

Most of the time zones are offset from Coordinated Universal Time (UTC), the world’s standard for time zone. In order to get the current time of different time zones, we will be using the pytz python library. How to get the current time ? In order to get the local time, we can use the time module. Some important functions of the time module

How do you manage meeting times when working across time zones?

Set fair meeting times. Working across time zones necessitates setting fair meeting times. You don’t want coworkers or employees getting up for a 5 a.m. meeting or staying up late to meet at 8 p.m. if it’s not in their normal working hours. Consider everyone’s time zone when setting meeting times and try rotating times if needed.


2 Answers

In my opinion Time class is the best for your job. Also it is Android API not general Java API.

Here I mentioned some of useful methods for your job:

void switchTimezone(String timezone)

Convert this time object so the time represented remains the same, but is instead located in a different timezone.

static String getCurrentTimezone()

Returns the timezone string that is currently set for the device.

And if you want to save a time in a timezone independed manner, you can convert to milliseconds (in UTC) by toMillis() method and then retrieve it by set(long millis) method.

If something is unclear please tell me!

UPDATE

Example:

long timeMillis = /* get time milliseconds form the server */
Time time = new Time();
time.set(timeMillis);

/* changing time zone */
time.switchTimezone(/* your desired timezone in string format */);

/* getting time as string  */
String timeString = time.format("%Y%m%dT%H%M%S"); // you can change format as you wish

Here is a table for formatting times

like image 52
frogatto Avatar answered Nov 10 '22 17:11

frogatto


You could use this code, which substracts the hour-difference between Brazil and the local timezone. Just replace yourDate with a Date-object.

//code...
yourDate.setTime(yourDate.getTime() - getDifferenceInMillis());
//code...

public int getDifferenceInMillis() {
    // Local Time
    int localMinute = Calendar.getInstance().get(Calendar.MINUTE);
    int localHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
    int localDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);

    // Brazil Time
    Calendar c = new GregorianCalendar(TimeZone.getTimeZone("Brazil/Acre"));
    c.setTimeInMillis(new Date().getTime());
    int brazilMinute = c.get(Calendar.MINUTE);
    int brazilHour = c.get(Calendar.HOUR_OF_DAY);
    int brazilDay = c.get(Calendar.DAY_OF_MONTH);

    // Difference between Brazil and local
    int minuteDifference = brazilMinute - localMinute;
    int hourDifference = brazilHour - localHour;
    int dayDifference = brazilDay - localDay;
    if (dayDifference != 0) {
        hourDifference = hourDifference + 24;
    }
    return (hourDifference * 60 + minuteDifference) * 60 * 1000;
}
like image 39
Manuel Allenspach Avatar answered Nov 10 '22 18:11

Manuel Allenspach