Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a timestamp from yyyy-MM-ddThh:mm:ss:SSSZ format to MM/dd/yyyy hh:mm:ss.SSS format? From ISO8601 to UTC

Tags:

java

datetime

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"

like image 320
user617966 Avatar asked Mar 22 '11 15:03

user617966


People also ask

What is SSSZ in date format?

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.

How do you convert HH MM to timestamp?

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);


2 Answers

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));     } } 
like image 71
Jon Skeet Avatar answered Oct 21 '22 03:10

Jon Skeet


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

like image 43
user617966 Avatar answered Oct 21 '22 03:10

user617966