Reputation: 3935
I am stuck while solving this naive scenario. Here is what I designed to convert UTC to local time.
public static DateTime timezoneAwareDate(Date date){
DateTime input = new DateTime(date,DateTimeZone.UTC);
DateTime output = input.withZone(DateTimeZone.getDefault());
Log.d(niftyFunctions.LOG_TAG,new SimpleDateFormat("yyyy-mmm-dd hh:mm").format(output.toDate()));
return output;
}
Here is what my input date looks like in UTC, coming from server:
2015-07-28 16:30
But here is what I am getting on my phone which is in IST from Log.d statement:
2015-030-28 07:30
I am going crazy about what's actually happening. Any help?
Upvotes: 0
Views: 589
Reputation: 3935
So I went with Joda-less solution using just the native libs. Here is the function
/**
* Returns localtime for UTC
*
* @param date
* @return
*/
public static Date timezoneAwareDate(String date){
// create simpledateformat for UTC dates in database
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date output;
// parse time
try{
output = simpleDateFormat.parse(date);
}catch (Exception e){
// return current time
output = new Date();
}
return output;
}
Upvotes: 1