I'm getting time stamp from the server like- "2014-03-05T13:00:07.341Z"
I want to set this to android calendar and convert the time stamp to am/pm. I have done the following code-
TimeZone tz = TimeZone.getTimeZone("Asia/Calcutta");
Calendar cal1 = Calendar.getInstance(tz);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
dateFormat.setCalendar(cal1);
try{
cal1.setTime(dateFormat.parse("2014/03/05T13:00:07.341Z"));
}catch(Exception e){
e.printStackTrace();
}
I am not able to convert to am/pm . How to do that? Any help will be appreciated.
If you want to convert to a specific format, you should use a separate DateFormat
for that. Currently you've only shown code to parse, and that just returns a Date
- an instant in time.
You'll need to work out everything you need about your output format - how do you want to represent the date, for example, and which locale do you want to use?
Some sample code to be adapted:
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
// Explicitly set it to UTC...
inputFormat.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
Date date = outputFormat.parse("2014/03/05T13:00:07.341Z", Locale.US);
// TODO: What format do you want, and what locale?
DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd h:mm:ss a");
outputFormat.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
String text = outputFormat.format(date);
It's not immediately clear what you should do if there's a ParseException
by the way - but you almost certainly shouldn't just log it and continue as if nothing had happened.
Note that the Calcutta time zone is only relevant on output - the input already specifies that it's in UTC.
See more on this question at Stackoverflow