Can anyone tell me how to convert this dateString "2003Jan01" into another dateString which is in this format "01-JAN-03"?
Asked
Active
Viewed 152 times
-2
-
2what have you already tried? – Manrico Corazzi Jun 30 '17 at 14:08
-
1Also answered here https://stackoverflow.com/questions/6637469/converting-date-string-to-a-different-format and probably other places. – rghome Jun 30 '17 at 14:09
1 Answers
0
One way is using two SimpleDateFormat: one to parse the input, one to format the output:
import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class SDF { public static void main(String args[]) { SimpleDateFormat source = new SimpleDateFormat("yyyyMMMdd", Locale.ENGLISH); try { Date date = source.parse("2003Jan01"); SimpleDateFormat destination = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH); System.out.println(destination.format(date)); } catch(Exception e) { e.printStackTrace(); } } }

Manrico Corazzi
- 11,299
- 10
- 48
- 62
-
2Please don’t teach the young ones to use the long outdated classes `SimpleDateFormat` and `Date`. Today we have so much better. – Ole V.V. Jun 30 '17 at 15:29