Java/Groovy - simple date reformatting
Asked Answered
I

4

59

I'm new to Java/Groovy development and I have a simple string that I would like to reformat, however I get an 'Unparseable date' error when I attempt to run the following:

import java.text.SimpleDateFormat 
import java.util.Date

String oldDate
Date date
String newDate 

oldDate = '04-DEC-2012'
date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(oldDate)
newDate = new SimpleDateFormat("M-d-yyyy").format(date) 

println newDate

I'm sure it's something simple, but the solution eludes me. Can anyone help?

Imphal answered 17/1, 2013 at 16:18 Comment(3)
Your date is given in "DD-MMM-YYYY" pattern, and you are trying to parse something else...Ostrander
duplicate https://mcmap.net/q/331103/-unparseable-date-quot-30-jun-12-quotKumasi
Not knowing Grrovy for new readers to this question I strongly recommend you don’t use SimpleDateFormat and Date. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use LocalDate, DateTimeFormatterBuilder and DateTimeFormatter, all from java.time, the modern Java date and time API. See this answer by user7605325.Stercoricolous
T
98

With Groovy, you don't need the includes, and can just do:

String oldDate = '04-DEC-2012'
Date date = Date.parse( 'dd-MMM-yyyy', oldDate )
String newDate = date.format( 'M-d-yyyy' )

println newDate

To print:

12-4-2012
Tacet answered 17/1, 2013 at 16:20 Comment(1)
@Andreas 7 years later, the link is docs.groovy-lang.org/docs/groovy-2.4.4/html/groovy-jdk/java/…Tacet
R
4

Your DateFormat pattern does not match you input date String. You could use

new SimpleDateFormat("dd-MMM-yyyy")
Ralphralston answered 17/1, 2013 at 16:20 Comment(0)
K
1

oldDate is not in the format of the SimpleDateFormat you are using to parse it.

Try this format: dd-MMM-yyyy - It matches what you're trying to parse.

Kacikacie answered 17/1, 2013 at 16:21 Comment(0)
T
1
//Groovy Script

import java.util.Date   
import java.util.TimeZone   
tz = TimeZone.getTimeZone("America/Sao_Paulo")   
def date = new Date()   
def dt = date.format("yyyy-MM-dd HH:mm:ss", timezone=tz)   
println dt //formatado   
println date // formatado porém, horario do Sistema em GMT.
Triclinic answered 16/5, 2022 at 16:25 Comment(4)
Thanks for wanting to contribute. I strongly recommend you don’t use Date and TimeZone. Those classes are poorly designed and long outdated. Instead use classes from java.time, the modern Java date and time API. Also and quite importantly, in what way does your answer answer the question? I believe the question was about reformatting from a format like 04-DEC-2012 into 12-4-2012.Stercoricolous
@OleV.V. How would the OPs problem be solved with java.time? Do you have an example?Sugared
In Java: Parse oldDate from the qusetion using my answer here, only the pattern needs to be dd-MMM-uuuu (four digit year). For formatting the LocalDate from that answer into 12-4-2012, use date.format(DateTimeFormatter.ofPattern("M-d-u")).Stercoricolous
Olá @OleV.V., realmente descrevi minha resposta de maneira errada, agradeço as suas dicas.Triclinic

© 2022 - 2024 — McMap. All rights reserved.