-2

Can anyone tell me how to convert this dateString "2003Jan01" into another dateString which is in this format "01-JAN-03"?

Manrico Corazzi
  • 11,299
  • 10
  • 48
  • 62
Suresh Sa
  • 1
  • 1

1 Answers1

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
  • 2
    Please 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