Groovy String to Date

后端 未结 7 1973
花落未央
花落未央 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: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")
    

提交回复
热议问题