Reputation: 1371
I've got this string 21-04-2016
for instance, that I'm trying to convert to a date. After that's done I need to flip it around, remove all the dashes and then convert it back to a string so that it looks like 20160421
. I have found some code that I'll post below but I want to know if there is a simple way of doing this, maybe without having to convert the string to a date? Anyhow, here's my (broken) code:
String from = region.startDate //I get this string from a controller
Date fromDate = Date.parse('yyyy-MM-dd', from) //.parse is depricated apparently
println(from)
println(fromDate)
Here's what I get back (from the println):
Wed Jul 08 00:04:00 SAST 16
11-04-2016
Upvotes: 0
Views: 249
Reputation: 36987
No need to convert from String to Date in that case (you could use a SimpleDateFormat
(or rather two) to do that, though); just use String and List operations from Java and Groovy:
from.split('-').reverse().join()
Upvotes: 3