How can groovy convert a string to a date?

In Groovy, you can convert a string to a date using the Date.parse() method.

Below is the code sample:

def dateString = "2021-01-01"
def format = "yyyy-MM-dd"
def date = Date.parse(format, dateString)
println date

The output is “Fri Jan 01 00:00:00 CST 2021”.

In the above example, the Date.parse() method takes in two parameters: the date format and the date string. It converts the date string into a date object based on the specified date format.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds