groovy
  1. groovy
  2. GROOVY-4789

Add method on Date to parse strings created with date.toString()

    Details

    • Type: New Feature New Feature
    • Status: Closed Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.7.10
    • Fix Version/s: 1.8.4, 2.0-beta-1
    • Component/s: groovy-jdk
    • Labels:
      None
    • Number of attachments :
      1

      Description

      People often need to re-parse Dates out of strings created from date.toString().

      My particular system's default date.toString() format is "Thu Apr 20 00:27:00 CEST 2006" so I know I can turn a Date into a string and then back into an object using Date.parse('EEE MMM dd HH:mm:ss z yyyy', new Date().toString()).

      But breaks when date.toString() changes format, either in a future version of Groovy or when the code moves to another system where date.toString() produces a different format (I'm not clear if this can happen or not).

        Activity

        Hide
        Paul King added a comment -

        Looking at the source code for toString() on java.util.Date it looks like the following format is always used: "EEE MMM dd HH:mm:ss zzz yyyy". So, perhaps there is nothing to do here.

        Show
        Paul King added a comment - Looking at the source code for toString() on java.util.Date it looks like the following format is always used: " EEE MMM dd HH:mm:ss zzz yyyy ". So, perhaps there is nothing to do here.
        Hide
        Tim Yates added a comment - - edited

        It could be useful to add a default groovy method that can parse this format without needing to know the format string

        Something like

        "Thu Apr 20 00:27:00 CEST 2006".parseToStringDate()
        
        Show
        Tim Yates added a comment - - edited It could be useful to add a default groovy method that can parse this format without needing to know the format string Something like "Thu Apr 20 00:27:00 CEST 2006" .parseToStringDate()
        Hide
        Paul King added a comment -

        That could possibly be useful. Dean, you used the wording "But breaks ...". Do you have an actual example which you believe breaks with "EEE MMM dd HH:mm:ss zzz yyyy"?

        Show
        Paul King added a comment - That could possibly be useful. Dean, you used the wording "But breaks ...". Do you have an actual example which you believe breaks with "EEE MMM dd HH:mm:ss zzz yyyy"?
        Hide
        Dean Moses added a comment -

        Paul: no, I don't have an example where "EEE MMM dd HH:mm:ss zzz yyyy" breaks. I didn't know that the format was hard-coded into java.utilDate – I assumed it would change based on system locale or something, which is an assumption other people could easily make.

        It's not very Groovy to make people reverse-engineer the default date format like I had to, especially when they don't know whether that format will be stable across systems. It'd be very helpful to have a method for this.

        Show
        Dean Moses added a comment - Paul: no, I don't have an example where "EEE MMM dd HH:mm:ss zzz yyyy" breaks. I didn't know that the format was hard-coded into java.utilDate – I assumed it would change based on system locale or something, which is an assumption other people could easily make. It's not very Groovy to make people reverse-engineer the default date format like I had to, especially when they don't know whether that format will be stable across systems. It'd be very helpful to have a method for this.
        Hide
        Dirk Weber added a comment -

        I've implemented the static DGM for the Date class. It can be used to parse a date which previously has been created by Date.toString(). Also created a test-case.

        Show
        Dirk Weber added a comment - I've implemented the static DGM for the Date class. It can be used to parse a date which previously has been created by Date.toString(). Also created a test-case.
        Dirk Weber made changes -
        Field Original Value New Value
        Attachment groovy_4789.patch [ 57423 ]
        CÚdric Champeau made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Assignee Cedric Champeau [ melix ]
        Fix Version/s 1.9-beta-5 [ 17925 ]
        Fix Version/s 1.8.4 [ 17852 ]
        Resolution Fixed [ 1 ]
        Paul King made changes -
        Status Resolved [ 5 ] Closed [ 6 ]

          People

          • Assignee:
            CÚdric Champeau
            Reporter:
            Dean Moses
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: