Display date according to the time zone of device
2010-06-14 02:21:49-0400 or 2010-06-14 02:21:49+0400
The above is the string coming from my webservice... is there a way to con开发者_如何学Govert this string to the date according to the local machine time zone
I used Simpledateformatter .. but not able to display correctly.. please help
Use java.text.SimpleDateFormat
for date formatting (from date string to date object). Pattern will be yyyy-MM-dd HH:mm:ssZ
Example:
String dateStr = "2010-06-14 02:21:49-0400";
String pattern = "yyyy-MM-dd HH:mm:ssZ";
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date date = sdf.parse(dateStr);
PS To use TimeZone, you can use TimeZone.getDefault()
and add it to SimpleDateFormat.
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
sdf.setTimeZone(TimeZone.getDefault());
Date date = sdf.parse(dateStr);
精彩评论