简单日期格式的不正确结果



我想将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

使用大写字母 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

最新更新