Groovy String to Date

后端 未结 7 1946
花落未央
花落未央 2020-12-02 11:45

I am coding this with Groovy

I am currently trying to convert a string that I have to a date without having to do anything too tedious.

String theD         


        
相关标签:
7条回答
  • 2020-12-02 12:22

    I think the best easy way in this case is to use parseToStringDate which is part of GDK (Groovy JDK enhancements):

    Parse a String matching the pattern EEE MMM dd HH:mm:ss zzz yyyy containing US-locale-constants only (e.g. Sat for Saturdays). Such a string is generated by the toString method of Date

    Example:

    println(Date.parseToStringDate("Tue Aug 10 16:02:43 PST 2010").format('MM-dd-yyyy'))
    
    0 讨论(0)
  • 2020-12-02 12:27

    Googling around for Groovy ways to "cast" a String to a Date, I came across this article: http://www.goodercode.com/wp/intercept-method-calls-groovy-type-conversion/

    The author uses Groovy metaMethods to allow dynamically extending the behavior of any class' asType method. Here is the code from the website.

    class Convert {
        private from
        private to
    
        private Convert(clazz) { from = clazz }
        static def from(clazz) {
            new Convert(clazz)
        }
    
        def to(clazz) {
            to = clazz
            return this
        }
    
        def using(closure) {
            def originalAsType = from.metaClass.getMetaMethod('asType', [] as Class[])
            from.metaClass.asType = { Class clazz ->
                if( clazz == to ) {
                    closure.setProperty('value', delegate)
                    closure(delegate)
                } else {
                    originalAsType.doMethodInvoke(delegate, clazz)
                }
            }
        }
    }
    

    They provide a Convert class that wraps the Groovy complexity, making it trivial to add custom as-based type conversion from any type to any other:

    Convert.from( String ).to( Date ).using { new java.text.SimpleDateFormat('MM-dd-yyyy').parse(value) }
    
    def christmas = '12-25-2010' as Date
    

    It's a convenient and powerful solution, but I wouldn't recommend it to someone who isn't familiar with the tradeoffs and pitfalls of tinkering with metaClasses.

    0 讨论(0)
  • 2020-12-02 12:31

    JChronic is your best choice. Here's an example that adds a .fromString() method to the Date class that parses just about anything you can throw at it:

    Date.metaClass.'static'.fromString = { str ->
        com.mdimension.jchronic.Chronic.parse(str).beginCalendar.time
    }
    

    You can call it like this:

    println Date.fromString("Tue Aug 10 16:02:43 PST 2010")
    println Date.fromString("july 1, 2012")
    println Date.fromString("next tuesday")
    
    0 讨论(0)
  • 2020-12-02 12:36

    Try this:

    def date = Date.parse("E MMM dd H:m:s z yyyy", dateStr)
    

    Here are the patterns to format the dates

    0 讨论(0)
  • 2020-12-02 12:39

    Below is the way we are going within our developing application.

    import java.text.SimpleDateFormat
    
    String newDateAdded = "2018-11-11T09:30:31"
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
    Date dateAdded = dateFormat.parse(newDateAdded)
    
    println(dateAdded)
    

    The output looks like

    Sun Nov 11 09:30:31 GMT 2018
    

    In your example, we could adjust a bit to meet your need. If I were you, I will do:

    String datePattern = "d/M/yyyy H:m:s"
    String theDate = "28/09/2010 16:02:43"
    SimpleDateFormat df = new SimpleDateFormat(datePattern)
    println df.parse(theDate)
    

    I hope this would help you much.

    0 讨论(0)
  • 2020-12-02 12:42

    Date#parse is deprecated . The alternative is :

    java.text.DateFormat#parse 
    

    thereFore :

     new SimpleDateFormat("E MMM dd H:m:s z yyyy", Locale.ARABIC).parse(testDate)
    

    Note that SimpleDateFormat is an implementation of DateFormat

    0 讨论(0)
提交回复
热议问题