26

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"

user617966
  • 4,515
  • 4
  • 23
  • 24

6 Answers6

31

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));
    }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Hi there, I was wondering what does the "T" and "Z" represent? – Kevin Zhao Jun 29 '15 at 05:12
  • 2
    @KevinZhao: 'T' is just the way of separating the time from the date; 'Z' is the way of indicating UTC as the time zone - "Zulu time". – Jon Skeet Jun 29 '15 at 05:46
  • thank you . this one really helped me final DateTimeFormatter parser = ISODateTimeFormat.dateTime(); parsedDate = parser.parseDateTime(stringDate).toDate(); – Lyju I Edwinson Sep 21 '16 at 00:09
31

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

user617966
  • 4,515
  • 4
  • 23
  • 24
0

Hope this Helps:

public String getSystemTimeInBelowFormat() {
    String timestamp = new SimpleDateFormat("yyyy-mm-dd 'T' HH:MM:SS.mmm-HH:SS").format(new Date());
    return timestamp;
}
0

Enter the original date into a Date object and then print out the result with a DateFormat. You may have to split up the string into smaller pieces to create the initial Date object, if the automatic parse method does not accept your format.

Pseudocode:

Date inputDate = convertYourInputIntoADateInWhateverWayYouPrefer(inputString);
DateFormat outputFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss.SSS");
String outputString = outputFormat.format(inputDate);
Pops
  • 30,199
  • 37
  • 136
  • 151
  • 1
    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" – user617966 Mar 22 '11 at 16:15
  • 1
    As far i know SimpleDateFormat is NOT thread safe, if you need a thread safe solution go with joda-time and build a formatter for example with the [PeriodFormatterBuilder](http://joda-time.sourceforge.net/apidocs/org/joda/time/format/PeriodFormatterBuilder.html) and enjoy easy date handling as it should be. – moritz Mar 22 '11 at 16:21
  • Well, the automatic parser is lenient, but not yet sentient. I guess you'll have to write that `convertYourInputIntoADateInWhateverWayYouPrefer()` method after all. – Pops Mar 22 '11 at 16:21
  • +1 @Moritz, I was trying to go for a simple-to-understand solution (as I usually do) but there are definitely advantages to joda time if this is for production. – Pops Mar 22 '11 at 16:22
0

You might want to have a look at joda time, which is a little easier to use than the java native date tools, and provides many common date patterns pre-built.

In response to comments, more detail:

To do this using Joda time, you need two DateTimeFormatters - one for your input format to parse your input and one for your output format to print your output. Your input format is an ISO standard format, so Joda time's ISODateTimeFormat class has a static method with a parser for it already: dateHourMinuteSecondMillis. Your output format isn't one they have a pre-built formatter for, so you'll have to make one yourself using DateTimeFormat. I think DateTimeFormat.forPattern("mm/dd/yyyy kk:mm:ss.SSS"); should do the trick. Once you have your two formatters, call the parseDateTime() method on the input format and the print method on the output format to get your result, as a string.

Putting it together should look something like this (warning, untested):

DateTimeFormatter input = ISODateTimeFormat.dateHourMinuteSecondMillis();
DateTimeFormatter output = DateTimeFormat.forPattern("mm/dd/yyyy kk:mm:ss.SSS");
String outputFormat = output.print( input.parseDate(inputFormat) );
Mark Tozzi
  • 10,353
  • 5
  • 22
  • 30
-3

Use DateFormat. (Sorry, but the brevity of the question does not warrant a longer or more detailed answer.)

Bombe
  • 81,643
  • 20
  • 123
  • 127
  • Sorry for any inconvenience. I am getting Exception :java.text.ParseException: Unparseable date: "2011-03-10T11:54:30.207Z" error see my question i have edited. – user617966 Mar 22 '11 at 16:20