简体   繁体   中英

Parsing string into date, Groovy

I have a date string stored in DB, as shown below

20180502160645

I want to parse it in this format:

yyyy-MM-dd'T'HH:mm:ssZ

Tried:

SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date date = dateParser.parse(dateString);
def parsedDate = dateParser.format(date);

And this:

Date dateTest = Date.parse("yyyy-MM-dd'T'HH:mm:ssZ",dateString );

Both failed to parse it.

Using the following format (below) will match the input:

import java.text.SimpleDateFormat

def dateString = '20180502160645'

SimpleDateFormat dateParser = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = dateParser.parse(dateString);
def parsedDate = dateParser.format(date);
assert dateString == parsedDate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM