-5

Is there any other easy way to convert "Aug/2016" to "08/2016" in java?

My Logic is working fine, but seems too vague.

String myDate = "Aug/2016"; 
String str = myDate.split("/")[0];

Date date;
            try {
                date = new SimpleDateFormat("MMMM").parse(str);

                Calendar cal = Calendar.getInstance();
                cal.setTime(date);
                System.out.println("##### ----- month in number  :   "  +cal.get(Calendar.MONTH+1));

                int year = Calendar.getInstance().get(Calendar.YEAR);
                int mnth = cal.get(Calendar.MONTH) + 1; 

                str = String.valueOf(mnth+"/"+year);
                System.out.println("##### ----- new selected  date  :   "  +str);

            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }           

suggestions please!...

prabu
  • 717
  • 3
  • 12
  • 30
  • 3
    Possible duplicate of [java.util.Date format conversion yyyy-mm-dd to mm-dd-yyyy](http://stackoverflow.com/questions/18480633/java-util-date-format-conversion-yyyy-mm-dd-to-mm-dd-yyyy) – Jekin Kalariya Sep 13 '16 at 07:32

1 Answers1

6

Simply use SimpleDateFormat:

    String myDate = "Aug/2016"; 
    SimpleDateFormat parser = new SimpleDateFormat("MMM/yyyy");
    SimpleDateFormat formatter = new SimpleDateFormat("MM/yyyy");
    System.out.println(formatter.format(parser.parse(myDate)));
Jens
  • 67,715
  • 15
  • 98
  • 113