0

I want to convert Tue Jun 01 00:00:00 IST 112 into 01-JUN-2012

I used

 Calendar calendar = new GregorianCalendar(year, 6, Calendar.DAY_OF_MONTH); 
     Date maxDate=new Date();
     maxDate=calendar.getTime();  
     calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); 
      SimpleDateFormat s=new SimpleDateFormat("dd-mmm-yyyy");
       s.format(maxDate);

But I get 30-000-0112

jmj
  • 237,923
  • 42
  • 401
  • 438
happy
  • 2,550
  • 17
  • 64
  • 109

2 Answers2

5

Use CAPITAL M for month,

 SimpleDateFormat s=new SimpleDateFormat("dd-MMM-yyyy");

Also you are setting date first and then you are resetting calendar which I guess not what you want to do so may be you need to change it to as follows

Date maxDate=new Date();
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); 
maxDate=calendar.getTime();  
SimpleDateFormat s=new SimpleDateFormat("dd-MMM-yyyy");
s.format(maxDate);

See

jmj
  • 237,923
  • 42
  • 401
  • 438
3

Use capital MMM in date format as shown below -

  SimpleDateFormat s=new SimpleDateFormat("dd-MMM-yyyy");

Everything else is ok

    Calendar calendar = Calendar.getInstance();
     Date maxDate=new Date();
     maxDate=calendar.getTime();  
     calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); 
     SimpleDateFormat s=new SimpleDateFormat("dd-MMM-yyyy");
     System.out.println(s.format(maxDate));

output would be - 06-Jul-2012

Pramod Kumar
  • 7,914
  • 5
  • 28
  • 37