olkoza
olkoza

Reputation: 725

Dateformat won't parse String

I am trying to parse a Date represented as a String. An example of the String would be: 20150724T104139.118+02

There is the parsing code:

DateFormat formatter = new SimpleDateFormat(timestampFormat);
return formatter.parse("yyyyMMdd'T'HHmmss'.'SX");

which throws error:

java.text.ParseException: Unparseable date: "20150724T104139.118+02"
at java.text.DateFormat.parse(DateFormat.java:366)
at 

The pattern is the same I use when transforming a Date into that string.

Upvotes: 2

Views: 1581

Answers (4)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79580

tl;dr

As long as you are using SimpleDateFormat, there is nothing wrong with your pattern, yyyyMMdd'T'HHmmss'.'SX.

Just as an aside, you do NOT need to enclose . within (single) quotes i.e. you can simply use yyyyMMdd'T'HHmmss.SX.

What's the problem then?

The problem is that you are passing the pattern, instead of the date-time string, to SimpleDateFormat#parse. Using your pattern, the following code runs successfully:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static void main(String[] args) throws ParseException {
        DateFormat formatter = new SimpleDateFormat("yyyyMMdd'T'HHmmss'.'SX");
        Date date = formatter.parse("20150724T104139.118+02");
        // ...
    }
}

More evidence to prove that there is nothing wrong with your pattern to be used with SimpleDateFormat for parsing:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    public static void main(String[] args) throws ParseException {
        DateFormat formatter = new SimpleDateFormat("yyyyMMdd'T'HHmmss'.'SX");
        Date date = formatter.parse("20150724T104139.118+02");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd'T'HHmmss'.'SSSX");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+02"));
        System.out.println(sdf.format(date));
    }
}

Output:

20150724T104139.118+02

java.time

Note that the legacy date-time API (java.util date-time types and their formatting API, SimpleDateFormat) is outdated and error-prone. It is recommended to stop using it completely and switch to java.time, the modern date-time API*.

Demo using modern date-time API:

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuuMMdd'T'HHmmss.SSSX", Locale.ENGLISH);
        OffsetDateTime odt = OffsetDateTime.parse("20150724T104139.118+02", dtf);
        System.out.println(odt);
    }
}

Output:

2015-07-24T10:41:39.118+02:00

In case you need an object of java.util.Date from this object of OffsetDateTime, you can do so as follows:

Date date = Date.from(odt.toInstant());

Learn more about the the modern date-time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Upvotes: 1

Rahul Tripathi
Rahul Tripathi

Reputation: 172618

You can try like this:

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd'T'HHmmss'.'sssX");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));  //Set the timezone as per your env if you want
sdf.parse("yourDate"); 

Upvotes: 0

Manos Nikolaidis
Manos Nikolaidis

Reputation: 22254

First of all, you have erroneously swapped the SimpleDateFormat string and the string you want to parse the date from. Also the format string doesn't match the example date. You need S three times for the milliseconds. Try the following :

String text = "20150724T104139.118+02";
DateFormat formatter = new SimpleDateFormat("yyyyMMdd'T'HHmmss'.'SSSX");
Date myTime = formatter.parse(text);

Upvotes: 2

sp00m
sp00m

Reputation: 48837

parse is expecting the date, not the pattern:

new SimpleDateFormat(datePattern).parse(dateToParse)

Upvotes: 2

Related Questions