0

I am getting date in two different formats.
1) 2012-01-05
2) 05/01/2012
But I want this to be in the below format. "5 Jan 2011"

Now I'm having String d1="2012-01-05" and String d2="2012-01-05".

Cœur
  • 34,719
  • 24
  • 185
  • 251
yshak
  • 1,893
  • 2
  • 16
  • 14

4 Answers4

1
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy");  
String now = formatter.format(new Date());

this u want right...

or

String oldString = "2009-12 Dec";
Calendar calendar = Calendar.getInstance();
calendar.setTime(new SimpleDateFormat("yyyy-MM").parse(oldString)); // Yes, month name is ignored but we don't need this.
calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
String newString = new SimpleDateFormat("dd-MMM-yyyy").format(calendar.getTime()).toUpperCase();
System.out.println(newString); // 31-DEC-2009
NikhilReddy
  • 6,824
  • 11
  • 35
  • 57
0

Take a look here: How to parse a date?

You have to determine, which format is used and than create a appropriate pattern.

After parsing the date, you can format it with another pattern, if you need a String:

SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy");  
String now = formatter.format(new Date());
Community
  • 1
  • 1
Christian Kuetbach
  • 15,550
  • 4
  • 42
  • 78
0

This should retrieve the date in the format you want. Simple few lines.

            String pattern = "dd MMM yyyy";
            SimpleDateFormat format = new SimpleDateFormat(pattern);

            System.out.println(format.format(new Date()));
Neeta
  • 3,466
  • 7
  • 33
  • 54
0
String d1 = "2012-01-05";
String d2 = "05/01/2012";
SimpleDateFormat curFormater1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat curFormater2 = new SimpleDateFormat("dd/MM/yyyy");
Date dateObj1 = null;

Date dateObj2 = null;
try {
    dateObj1 = curFormater.parse(d1);
    dateObj2 = curFormater.parse(d2);
} catch (ParseException e) {
    e.printStackTrace();
}

SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy");
String date_new1 = formatter.format(d1);
String date_new2 = formatter.format(d2);

try this code it will help you so solve your problem

Harsh Trivedi
  • 992
  • 7
  • 24