I have a date string which is
"20120514045300.0Z"
I'm trying to figure out how to convert this to the following format
"2012-05-14-T045300.0Z"
How do you suggest that I should solve it?
Instead of diving into hard-to-read string manipulation code, you should parse the date into a decent representation and from that representation format the output. If possible, I would recommend you to stick with some existing API.
Here's a solution based on SimpleDateFormat
. (It hardcodes the 0Z
part though.)
String input = "20120514045300.0Z";
DateFormat inputDF = new SimpleDateFormat("yyyyMMddHHmmss'.0Z'");
DateFormat outputDF = new SimpleDateFormat("yyyy-MM-dd'-T'HHmmss.'0Z'");
Date date = inputDF.parse(input);
String output = outputDF.format(date);
System.out.println(output); // Prints 2012-05-14-T045300.0Z as expected.
(Adapted from here: How to get Date part from given String?.)
You need to parse the date and them format it to desirable form. Use SimpleDateFormat
to do it. You can also check out this question for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With