Search code examples
javastringdatestruts

Convert String to date format with contains characters month


Good day,

Please correct me if my title and question is not correct.

Normally I just convert a String to a date format all in numeric, example :

mm/dd/yyyy to yyyy-mm-dd as 01/02/2000 to 2000-02-01

or any other style/pattern.

I am thinking can I convert it to a date format with contains of characters month or not, example :

mm/dd/yyyy to dd-MMM-yyyy as 10/09/2013 to 09-Oct-2013

Which is the month no longer display in numeric, but in alpha.

I am work in Java, Struts framework.

Kindly advise.


Solution

  • Hope this helps. You can choose multiple patterns which can be found at link below. Here is an example how to use it

    Multiple patterns can be found here

     final String OLD_FORMAT = "dd/MM/yyyy";
    
     final String NEW_FORMAT = "yyyy/MMM/dd";
    
     // August 12, 2010
     String oldDateString = "12/08/2010";
     String newDateString;
    
     SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT);
     Date d = sdf.parse(oldDateString);
     sdf.applyPattern(NEW_FORMAT);
     newDateString = sdf.format(d);
    

    `