0

I'm trying to convert dates generated by Java's toString() function back into date objects. However the behaviour doesn't match what I'm expecting.

I've tried the following code:

public static void main(String[] args) {
    SimpleDateFormat sdfDate = new SimpleDateFormat("E MMM dd HH:mm:ss zzz YYYY");
    Date now = new Date();
    String strDate = sdfDate.format(now);

    try {
        System.out.println(strDate+"\n"+sdfDate.parse(strDate).toString());
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

However when I run the above I'm getting the following output:

Mon Jul 25 16:58:04 BST 2016
Mon Jan 04 15:58:04 GMT 2016

I understand why the timezone is reverting to GMT, however I'm confused its interpretation of the month and day. I'm assuming I've messed something up in the Date format string, but I can't tell what.

I've also tried it with "EEE MMM dd HH:mm:ss zzz YYYY" but I get the same behaviour. Can anyone tell me what I'm doing wrong please?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
elaverick
  • 314
  • 1
  • 2
  • 13
  • Why do you want to parse toString of date? Is this just an exercise or a solution to some other problem? – Krzysztof Krasoń Jul 25 '16 at 16:04
  • 1
    Notice that the `toString` method javadoc explains the exact pattern it uses. – Sotirios Delimanolis Jul 25 '16 at 16:07
  • @SotiriosDelimanolis Note that toString() says it is using a pattern other than the one used here. I know toString() is mentioned in the question, but it isn't really getting applied here. – arcy Jul 25 '16 at 16:21
  • 1
    @elaverick I think you mean "yyyy" instead of "YYYY". It is not clear to me what a "week year" is, or why the parser cannot translate it back from its output, but with "yyyy" your code works. – arcy Jul 25 '16 at 16:22
  • @arcy `sdfDate.parse(strDate).toString()`. `parse` returns a `Date` object. `Date#toString` is well documented. – Sotirios Delimanolis Jul 25 '16 at 17:02
  • @SotiriosDelimanolis It certainly is. Says it returns "a String in yyyy-mm-dd format". It isn't helpful here. – arcy Jul 25 '16 at 18:04
  • @arcy Which javadoc are you looking at? I'm looking at [`Date#toString()`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#toString--), which states _Converts this Date object to a String of the form_ and then proceeds to describe all the parts, which you can use to construct a pattern with `SimpleDateFormat`. That's what they are trying to do, that's why it's helpful. They would not have needed to ask this question if they simply referred to the documentation of the components they were using. – Sotirios Delimanolis Jul 25 '16 at 18:08
  • Interesting. Yours is new in Java 8. Was looking at https://docs.oracle.com/javase/7/docs/api/java/sql/Date.html#toString() – arcy Jul 25 '16 at 18:16
  • @arcy That's `java.sql.Date`. I assumed they were talking about [`java.util.Date`](https://docs.oracle.com/javase/7/docs/api/java/util/Date.html#toString()). – Sotirios Delimanolis Jul 25 '16 at 18:29
  • Good assumption. My mistake. – arcy Jul 25 '16 at 18:42
  • Thank you for the replies, arcy's comment regarding the capitalisation of the year marker sorted it for me (if you'd like to make that an answer I will mark it as accepted). I'd originally been going by http://www.java2s.com/Tutorial/Java/0040__Data-Type/SimpleDateFormat.htm that source, which didn't mention the y/Y alternatives and I mistakenly assumed that I'd get a parser error if I'd selected the wrong case for my tokens. – elaverick Jul 26 '16 at 07:52

0 Answers0