I have a String
of yyyyMM
format and I need to convert it to a date in the format MM/yyyy
.
Example: String is 201710
and it needs to be converted to a date in the format 10/2017
.
Can someone tell me how to do this in groovy?
Thanks
I have a String
of yyyyMM
format and I need to convert it to a date in the format MM/yyyy
.
Example: String is 201710
and it needs to be converted to a date in the format 10/2017
.
Can someone tell me how to do this in groovy?
Thanks
There is no such thing as java.util.Date
object in specific format. Format of the date like yyyyMM
or MM/yyyy
is just a way it is represented as a String
. If your input is a String formatted as 201710
and you want to format it as 10/2017
then you have to create a java.util.Date
object from your input String (Date.parse()
method does that) and call .format('MM/yyyy')
method on that created object, e.g.
def formatted = Date.parse("yyyyMM", "201710").format("MM/yyyy")