6

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.

Neeraj Sharma
  • 463
  • 1
  • 9
  • 27
  • What format are your dates in (strings, milliseconds etc etc?) – Squonk May 31 '14 at 11:09
  • year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute – Neeraj Sharma May 31 '14 at 11:22
  • So as a String? Edit your question to show an example. – Squonk May 31 '14 at 11:23
  • String sdate=""+cal.get(Calendar.YEAR)+"-"+cal.get(Calendar.MONTH)+"-"+cal.get(Calendar.DAY_OF_MONTH)+" "+cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE)+":"+cal.get(Calendar.SECOND); – Neeraj Sharma May 31 '14 at 11:24
  • I mean an example of one of the date strings. Also...edit your question instead of posting code in a comment. – Squonk May 31 '14 at 11:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/54845/discussion-between-neeraj-sharma-and-squonk). – Neeraj Sharma May 31 '14 at 11:30
  • why dont you save server time in in UTC time and covert those time according to device time . – dharmendra Jun 02 '14 at 11:24
  • i have to convert at the device's local time zone and server has the Brazil/Acre time saved and i cant change that timing i am not authorized for that – Neeraj Sharma Jun 02 '14 at 11:25
  • have you checked this http://stackoverflow.com/questions/9108356/get-time-of-different-time-zones-on-selection-of-time-from-time-picker and this too http://stackoverflow.com/questions/9429357/date-and-time-conversion-to-some-other-timezone-in-java – surhidamatya Jun 02 '14 at 11:34
  • if u get `TimeStamp` from server then it will be easy to convert that in `Current DateTime` format – Kaushik Jun 02 '14 at 11:35
  • http://singztechmusings.wordpress.com/2011/06/23/java-timezone-correctionconversion-with-daylight-savings-time-settings/ – surhidamatya Jun 02 '14 at 11:35
  • try out :http://stackoverflow.com/questions/8238661/how-to-convert-date-time-from-one-time-zone-to-another-time-zone – Richa Jun 02 '14 at 13:02

5 Answers5

8

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

frogatto
  • 28,539
  • 11
  • 83
  • 129
2

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;
}
Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76
0

You should store your date has a long or timestamp in your server. If you don't want you can anyway send your date as a long generate from your database date. Then create your calendar instance like that :

        Calendar c = Calendar.getInstance();
        c.setTime(new Date(yourdatelong));

You can have to multiplie and add some constant in your long. In java the definition is "the number of milisecond since 1970 1/1 00:00:00". It is differents in C#, for exemple ( number of nanoseconde from 1/1/1900, if I remenber well).

Like that you are sure to set the same date in all your device. When it is done, you just have to put the timezone that you want in your calendar to display the local time.

http://developer.android.com/reference/java/util/Calendar.html

You can have many option to manage time and display it in this class.

Eliott Roynette
  • 716
  • 8
  • 21
0

If dates are stored in server time zone (Brazil/Acre), you should load date time from DB, convert it to UTC time zone and send to client. On client side change UTC to local time zone:

Server side:

DateTime dateOnServer = // load date from db
DateTime dateUTC = dateOnServer.withZone(DateTimeZone.UTC); // convert to UTC  
String dateAsStringUTC = dateUTC.toString("yyyy-MM-ddTHH:mm:ss");
// send 'dateAsStringUTC' to client  

Client side:

String dateAsStringUTC = // receive date from server
DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-ddTHH:mm:ss"); // parser for date 
DateTime dateOnClient= dtf.parseDateTime(dateAsStringUTC); 
// 'dateOnClient' will be in client time zone 'DateTimeZone.getDefault()'
Ilya
  • 29,135
  • 19
  • 110
  • 158
-2

I faced same problem like you...

I got the solution using SimpleDateFormat

SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sourceFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date parsed = format.parse("2011-03-01 15:10:37"); // => Date is in UTC now

TimeZone tz = TimeZone.getTimeZone("America/Chicago");
SimpleDateFormat destFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
destFormat.setTimeZone(tz);

String result = destFormat.format(parsed);

this may help you..

frogatto
  • 28,539
  • 11
  • 83
  • 129
Pragnesh Ghoda シ
  • 8,318
  • 3
  • 25
  • 40
  • for more detail you can refer this link [SimpleDateFormat](http://developer.android.com/reference/java/text/SimpleDateFormat.html) it will give you detain information about your problem – Pragnesh Ghoda シ May 31 '14 at 10:39
  • All that does is format a date in a different way. It doesn't convert a UTC time to a local time based on timezone and daylight savings variations. – Squonk May 31 '14 at 10:59
  • **"there is my code"** : No, that's not your code. You've copied and pasted it from another answer here on SO and you haven't attributed the original answer. – Squonk May 31 '14 at 11:14
  • 1
    first time when i gave answer it was my code...but when i edited the answer i forgot to remove that line...brother. – Pragnesh Ghoda シ Jun 02 '14 at 05:21
  • 1
    dont worry prag it happens but keep answering i will vote you up when your are right – Neeraj Sharma Jun 02 '14 at 05:48