2

I'm currently a developing a sync module for one of my applications. I'm syncing the records between local and server based on LastUpdated time, which is nothing but a timestamp. The server is of Singapore, so how can I set the timezone to Singapore in my Android application?

I have tried,

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.ENGLISH);
sdf.setTimeZone(TimeZone.getTimeZone("VST") );

and,

Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("Asia/Singapore"));
System.out.println(calendar.get(Calendar.HOUR_OF_DAY));

Still no luck.

Or is there any way I can set the common timezone for both local and server? Server side language is ASP.Net

Sagar Pudi
  • 4,634
  • 3
  • 32
  • 51
Aniruddha
  • 4,477
  • 2
  • 21
  • 39
  • possible duplicate of [How to set time zone of a java.util.Date?](http://stackoverflow.com/questions/2891361/how-to-set-time-zone-of-a-java-util-date) – Basil Bourque Oct 29 '14 at 07:13

3 Answers3

3

Your SimpleDateFormat example is almost correct. I believe you want SGT for Singapore and you need to call DateFormat.format(Date). You might also pass the TimeZone to Calendar.getInstace(TimeZone) like

TimeZone tz = TimeZone.getTimeZone("SGT");
sdf.setTimeZone(tz);
System.out.println(sdf.format(Calendar.getInstance().getTime()));
System.out.println(Calendar.getInstance(tz));
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • I tried your solution and got the result as `AM_PM=0,HOUR=2,HOUR_OF_DAY=2,MINUTE=18`, but I believe the time in Singapore would be around `12:45 PM`. Why there is difference? – Aniruddha Oct 29 '14 at 04:48
  • The time in [Singapore](http://en.wikipedia.org/wiki/Singapore_Standard_Time) is also obtainable with "UTC+8", or a google search. You can also use "UCT-8", according to this [table](http://docs.oracle.com/cd/B10191_01/calendar.903/b10094/timezone.htm). But if I have the wrong TimeZone, it should be possible to find one that works. – Elliott Frisch Oct 29 '14 at 04:55
  • Thank you, I have tried that also, but it is still the same. Anything can be done on server side? Like setting it to some common timezone? – Aniruddha Oct 29 '14 at 04:57
  • You could always do everything in UTC. – Elliott Frisch Oct 29 '14 at 05:00
2

Joda-Time

When doing anything significant with date-time work, you should avoid the java.util.Date and java.util.Calendar classes as they are notoriously troublesome, confusing, and flawed. Instead use the Joda-Time library which does work in Android.

ISO 8601

Your string input is close to standard ISO 8601 format. Just replace the SPACE with a T. Then pass the result to the constructor of DateTime along with a DateTimeZone object representing the intended time zone of that string. Search StackOveflow for hundreds of examples.

UTC

As Elliott Frisch commented, on the server-side you should be working and storing date-time values all in UTC. Convert to local zoned values only when expected by the user in the user interface.

The server itself should be set to UTC, or if not possible then set to Reykjavík Iceland. But your programming should never assume the server is so set. Your programming should always specify the desired time zone.

Search StackOverflow for hundreds of examples and discussion of these points.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

Finally, this is how I solved it.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.ENGLISH);
TimeZone tz = TimeZone.getTimeZone("Asia/Singapore"); 
sdf.setTimeZone(tz); 

java.util.Date date= new java.util.Date();
Timestamp local = new Timestamp(date.getTime());
String strDate = sdf.format(date);
System.out.println("Local in String format " + strDate);
Aniruddha
  • 4,477
  • 2
  • 21
  • 39