java程序Date问题

来源:百度知道 编辑:UC知道 时间:2024/05/31 07:12:30
Calendar c1=Calendar.getInstance();
System.out.println(c1.get(c1.YEAR)+"year"+c1.get(c1.MONTH)+"month"+c1.get(c1.DAY_OF_MONTH)+"day"+c1.get(c1.HOUR_OF_DAY)+"hour"+c1.get(c1.MINUTE)+"minute"+c1.get(c1.SECOND)+"second");

System.out.println(c1.getTimeInMillis());
c1.add(c1.DAY_OF_YEAR, 360);
System.out.println(c1.getTime());

//Date Class
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-DD");
SimpleDateFormat sdf1=new SimpleDateFormat("yyyy年MM月DD日");
//Date d1=null;
try {
Date d1 = sdf.parse("2003-09-23");
System.out.println("SimpleDateFormate :"+sdf1.format(d1));
System.out.println(d1.getMonth());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
结果:2009year10month13day15hour2minute25second
1258095745078
Mo

格式化的时候出错了!DD要小写,大写的识别不了,这样不管你输的是几月,转换的都是一月。就像你YYYY用大写的会抛异常一样。所以应该这样改为:
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf1=new SimpleDateFormat("yyyy年MM月dd日");

这样就可以了。

首先,DD要小写
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf1=new SimpleDateFormat("yyyy年MM月dd日");
其次 c1.MONTH这种写法尽量不要用,应该写成Calender.MONTH
静态变量是属于类的,不是实例的

d1.getMonth() 这种也过时了,用Calendar.get(Calendar.MONTH)

不知道你注意没有你的时间显示有问题,月份不对,这是因为国外日期的问题,需要写成c1.get(Calendar.MONTH)+1

应该是你windows系统的日期格式不对吧。。
上面朋友提供的语句完全可以实现
去你的控制面板-区域设置-日期格式看看