Improper result of SimpleDateFormat

前端 未结 2 1022
别那么骄傲
别那么骄傲 2020-12-22 08:48

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

I used

 Calendar calendar = new GregorianCalendar(year, 6,          


        
相关标签:
2条回答
  • 2020-12-22 09:14

    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

    0 讨论(0)
  • 2020-12-22 09:17

    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

    • SimpleDateFormat API doc
    0 讨论(0)
提交回复
热议问题