SimpleDateFormat的结果不正确

我想将Tue Jun 01 00:00:00 IST 112转换为01-JUN-2012

我用了

  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); 

但我得到30-000-0112

使用CAPITAL M一个月,

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

你也是先设置日期,然后你重置日历,我想你不想做什么,所以你可能需要将它更改为如下

 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); 

看到

  • SimpleDateFormat API文档

使用日期格式的资本MMM,如下所示 –

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

其他一切都还可以

  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)); 

输出将是 – 06-Jul-2012