happy
happy

Reputation: 2628

Improper result of SimpleDateFormat

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

Upvotes: 0

Views: 1646

Answers (2)

Pramod Kumar
Pramod Kumar

Reputation: 8014

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

Upvotes: 3

Jigar Joshi
Jigar Joshi

Reputation: 240860

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

Upvotes: 5

Related Questions