I want to convert the timestamp 2011-03-10T11:54:30.207Z to 10/03/2011 11:54:30.207. How can I do this? I want to convert ISO8601 format to UTC and then that UTC should be location aware. Please help
String str_date="2011-03-10T11:54:30.207Z"; DateFormat formatter ; Date date ; formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS"); date = (Date)formatter.parse(str_date); System.out.println("output: " +date );
Exception :java.text.ParseException: Unparseable date: "2011-03-10T11:54:30.207Z"
Dates are formatted using the following format: "yyyy-MM-dd'T'hh:mm:ss'Z'" if in UTC or "yyyy-MM-dd'T'hh:mm:ss[+|-]hh:mm" otherwise. On the contrary to the time zone, by default the number of milliseconds is not displayed. However, when displayed, the format is: "yyyy-MM-dd'T'hh:mm:ss.
Below is my coding: Object d = getMappedObjectValue(reportBeamDataMap, ReportBeamConstant. DAT_STOPDATE); Date stopDate1 = (Date)d; SimpleDateFormat printFormat = new SimpleDateFormat("hh:mm"); String timeString = printFormat. format(stopDate1);
Firstly, you need to be aware that UTC isn't a format, it's a time zone, effectively. So "converting from ISO8601 to UTC" doesn't really make sense as a concept.
However, here's a sample program using Joda Time which parses the text into a DateTime
and then formats it. I've guessed at a format you may want to use - you haven't really provided enough information about what you're trying to do to say more than that. You may also want to consider time zones... do you want to display the local time at the specified instant? If so, you'll need to work out the user's time zone and convert appropriately.
import org.joda.time.*; import org.joda.time.format.*; public class Test { public static void main(String[] args) { String text = "2011-03-10T11:54:30.207Z"; DateTimeFormatter parser = ISODateTimeFormat.dateTime(); DateTime dt = parser.parseDateTime(text); DateTimeFormatter formatter = DateTimeFormat.mediumDateTime(); System.out.println(formatter.print(dt)); } }
Yes. you can use SimpleDateFormat like this.
SimpleDateFormat formatter, FORMATTER; formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); String oldDate = "2011-03-10T11:54:30.207Z"; Date date = formatter.parse(oldDate.substring(0, 24)); FORMATTER = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss.SSS"); System.out.println("OldDate-->"+oldDate); System.out.println("NewDate-->"+FORMATTER.format(date));
Output OldDate-->2011-03-10T11:54:30.207Z NewDate-->10-Mar-2011 11:54:30.207
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With