-3

I have string in 'MM/dd/yyyy' format and want to convert it into 'dd-MM-yy'.

e.g. '04/01/2012' should be converted into '01-Apr-12'

Can anyone please suggest how to get this?

harriyott
  • 10,505
  • 10
  • 64
  • 103
Vallaru
  • 3
  • 2
  • 8
  • 1
    possible duplicate of [String to Date in Different Format in Java](http://stackoverflow.com/questions/882420/string-to-date-in-different-format-in-java) – Brian Roach Feb 13 '12 at 04:40
  • 2
    Welcome to Stack Overflow. Please use the search before asking new questions. Many have already been asked and answered. – Brian Roach Feb 13 '12 at 04:40

3 Answers3

1
SimpleDateFormat currentFormat = new SimpleDateFormat("mm/dd/yyyy");
SimpleDateFormat newFormat = new SimpleDateFormat("dd-mm-yy");

String dateInOldFormat="04/01/2012";
Date temp = currentFormat.parse(dateInOldFormat);
String dateInNewFormat= newFormat.format(temp);

i think things are pretty simple from here on...

Anantha Sharma
  • 9,920
  • 4
  • 33
  • 35
0

You can use the Date class in Java. Parse a string with your format, and then output using a different format.

Oleksi
  • 12,947
  • 4
  • 56
  • 80
0

Below is example of date conversion... For your program, do changes accordingly...

    String dateStr = "Thu Jan 19 2012 01:00 PM";
    DateFormat readFormat = new SimpleDateFormat( "EEE MMM dd yyyy hh:mm aaa");

    DateFormat writeFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
    Date date = null;
    try {
       date = readFormat.parse( dateStr );
    } catch ( ParseException e ) {
        e.printStackTrace();
    }

    String formattedDate = "";
    if( date != null ) {
    formattedDate = writeFormat.format( date );
    }

    System.out.println(formattedDate);

Output is 2012-01-19 13:00:00

Good Luck

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276