5

I wanted to convert from string to java.util.Date. for the same purpose I used following code,

String timeStamp = "Mon Feb 14 18:15:39 IST 2011";
DateFormat formatter = new SimpleDateFormat("dow mon dd hh:mm:ss zzz yyyy");
Date ts = (Date)formatter.parse(timeStamp);

The format given to SimpleDateFormat() is format of java.util.Date. When you convert util's Date to string it comes in this format('dow mon dd hh:mm:ss zzz yyyy'). But when I execute code, It gives me Exception. I Don't know what exactly I needed to do to get rid of this problem. I am posting the part of StackTrace of exception. If anybody knows the solution,

java.lang.IllegalArgumentException: Illegal pattern character 'o' at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:769) at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:576) at java.text.SimpleDateFormat.(SimpleDateFormat.java:501) at java.text.SimpleDateFormat.(SimpleDateFormat.java:476) Thank you in advance.

3
  • Since Java 8 use java.time, the modern Java date and time API, for your date and time work. Date, DateFormat amd SimpleDateFormat were always badly designed and troublesome to work with, so don’t any more. Commented Jul 27 at 14:14
  • The IST in your string, is that for Icelandic Standard Time, Irish Standard Time (cannot be Irish summer time in February), Israel Standard Time, India Standard Time or something else? If we don’t know, we cannot make the correct conversion. Commented Jul 27 at 14:39
  • There must be a reason why you were converting your original java.util.Date to a String and now want to convert it back to a Date. Is this for serialization? You can and should avoid using an ambiguous time zone abbreviation for that purpose. The standard recommendation is to use ISO 8601 format. Commented Jul 27 at 15:02

3 Answers 3

16

Try this instead:

DateFormat formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy");

E is used for "Day in Week" as text, M is the month name.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, this format is correct. Actually from java docs I got that format for util's Date.toString() function. Thank you very much.
Actually, it should be HH (hours in 0..23) and not hh (hours in 1..12. The documentation of Date.toString is quite confusing, I'm just filing a bug report about this.
2

You've formatted it wrong I believe, if you look here http://download.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html its E for day of the week and M for month

2 Comments

Your answer is also correc, but same as Andreas's answer. Anyways thanks for the link.
Yes I am sorry, I wrote it as he was writing it. He did answer first though so I really don't mind, as long as you got your question answered!
0

Serialization and deserialization: TL;DR

    Instant parsedBack = Instant.parse(Instant.now().toString());
    System.out.println(parsedBack);

2025-07-27T18:11:47.276158411Z

Use ISO 8601 and java.time

  1. For serializing date and time (for example for data transfer or for persistence) use the standard ISO 8601 format.
  2. For all date and time work, since Java 8 use java.time, the modern Java date and time API. The class you need from it is probably Instant, depending on your more exact requirements. The classes Date, DateFormat amd SimpleDateFormat were always badly designed and troublesome to work with, so avoid them.

The two points go nicely hand in hand:

    Instant originalInstant = Instant.now();
    String serializedToIso8601 = originalInstant.toString();
    Instant theSameInstant = Instant.parse(serializedToIso8601);

Parsing the string from Date.toString() back

If you are receiving the string from Date.toString(), first of all be aware that generally you cannot parse it back into an Instant or date-time object equal to the original Date. The hindrances include:

  1. Milliseconds from the original Date are lost since they are not in the string, leading to an inaccuracy of up to 999 milliseconds.
  2. The era from the original Date is not in the string either. So if your original Date was in year 44 BCE, you will get the corresponding date in year 44 CE (AD).
  3. The time zone abbreviation in the string is often (most often?) ambiguous, so there is a great risk of getting the wrong time zone and hence a wrong time. To make matters worse, an ambiguous time zone abbreviation will be interpreted differently on different JVMs. I believe that CET can only be for Central European (Standard) Time, though, so with your example string you should be good.

With these limitations the format pattern string from the answer by Andreas Dolk also works with java.time. Declare:

private static final DateTimeFormatter dtf
        = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz y", Locale.ROOT);

I have changed yyyy to just y. The former obviously works with year 2012, For years outside the 1000 through 9999 range Date printed fewer or more digits. The single y parses 1 through 9 digits.

It’s essential to provide a locale. Otherwise the JVM’s default locale will be used, and if it’s not English, parsing will fail. In the worst case you will see your code running fine for many years, and suddenly it will break when one day someone runs it on a computer or device with a different locale setting. I use Locale.ROOT for “the locale neutral locale” or “don’t apply any locale specific processing”. It seems to be the correct approach here.

With this formatter parsing goes like:

    String s = "Sun Dec 12 13:45:12 CET 2010";
    ZonedDateTime zdt = ZonedDateTime.parse(s, dtf);
    System.out.println("As ZonedDateTime: " + zdt);
    Instant nearlyTheSameInstant = zdt.toInstant();
    System.out.println("As Instant: " + nearlyTheSameInstant);

Output:

As ZonedDateTime: 2010-12-12T13:45:12+01:00[CET]
As Instant: 2010-12-12T12:45:12Z

Links

  • Oracle tutorial: Trail: Date Time explaining how to use java.time.
  • ISO 8601 on Wikipedia.
  • Acknowledgement: This answer was heavily inspired by thi-s answer by Anonymous. I believe the answer belongs better here since this question is the original to that other one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.