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
    • 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

      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

        Dean Moses created issue -
        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 -
        Resolution Fixed [ 1 ]
        Status Open [ 1 ] Resolved [ 5 ]
        Assignee Cedric Champeau [ melix ]
        Fix Version/s 1.9-beta-5 [ 17925 ]
        Fix Version/s 1.8.4 [ 17852 ]
        Paul King made changes -
        Status Resolved [ 5 ] Closed [ 6 ]
        Mark Thomas made changes -
        Project Import Sun Apr 05 13:32:57 UTC 2015 [ 1428240777691 ]
        Mark Thomas made changes -
        Workflow jira [ 12733717 ] Default workflow, editable Closed status [ 12745495 ]
        Mark Thomas made changes -
        Project Import Mon Apr 06 02:11:23 UTC 2015 [ 1428286283443 ]
        Mark Thomas made changes -
        Workflow jira [ 12971200 ] Default workflow, editable Closed status [ 12978979 ]
        Transition Time In Source Status Execution Times Last Executer Last Execution Date
        Open Open Resolved Resolved
        200d 16h 1 Cédric Champeau 07/Nov/11 02:37
        Resolved Resolved Closed Closed
        47d 30m 1 Paul King 24/Dec/11 03:08

          People

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

            Dates

            • Created:
              Updated:
              Resolved:

              Development