SimpleDateFormat不正确的结果 [英] Improper result of SimpleDateFormat

查看:131
本文介绍了SimpleDateFormat不正确的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转换 Tue Jun 01 00:00:00 IST 112 into 01-Jun-2012 / p>

我使用

 日历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为月,

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

查看




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

解决方案

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不正确的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆