What is the best way and how to convert this string "2015-07-18T13:32:56.971-0400" to date and converting to user's phone's local timezone in android?
Asked
Active
Viewed 1,834 times
0
-
3possible duplicate of [Parse a String to Date in Java](http://stackoverflow.com/questions/11446420/parse-a-string-to-date-in-java) – Andreas Aug 26 '15 at 22:54
-
duplicate of [How to get TimeZone from android mobile?](http://stackoverflow.com/questions/7672597/how-to-get-timezone-from-android-mobile) for 2nd question. – Andreas Aug 26 '15 at 22:54
-
My string is a bit different and my aim to display this on user's local date time. – Ali Karaca Aug 27 '15 at 08:52
2 Answers
2
I listed the duplicate link because you're asking how to parse a date string. That has been answered a gazillion times.
The other link shows that the user's phone's local timezone in android is the default timezone of the JVM, so you don't have to do any conversion.
To see for yourself, run the following code in any JVM (Android, PC, Mac, Linux, ...):
String input = "2015-07-18T13:32:56.971-0400";
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").parse(input);
String output = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss.SSS a").format(date);
System.out.println(input + " -> " + output);
If you're in Los Angeles, CA, USA, you get this:
2015-07-18T13:32:56.971-0400 -> 07/18/2015 10:32:56.971 AM
Note that 13:32 was adjusted to 10:32 AM (10:32).

Andreas
- 154,647
- 11
- 152
- 247
1
Just use these to get the time and date
public String getCurrentDate(){
Time dtNow = new Time();
dtNow.setToNow();
return dtNow.format("%Y.%m.%d %H:%M"); // YYYYMMDDTHHMMSS
}
public String getCurrentTime(){
Time dtNow = new Time();
dtNow.setToNow();
return dtNow.format("%H:%M"); // YYYYMMDDTHHMMSS
}
the first function will return time and the second function returns date

Ibukun Muyide
- 1,294
- 1
- 15
- 23
-
its basically getting the time and date which is what you want to get in the real sense. – Ibukun Muyide Aug 27 '15 at 15:53