写一个日期的代码经常看看 这个容易忘啊~~!
日期类型
Calendar calendar = Calendar.getInstance();
calendar.clear();
//计算某一月份的最大天数
calendar.set(Calendar.YEAR, 2000);
calendar.set(Calendar.MONTH, 1);
int ds = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println("天数:"+ds);
//Date与Calendar类之间的转换
Date date = new Date();
calendar.setTime(date);//将Date转化城calendar
System.out.println(calendar.get(Calendar.YEAR));
System.out.println(calendar.get(Calendar.MONTH)+1);
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
//将Calendar转化成Date
Calendar calendar1 = Calendar.getInstance();
calendar1.clear();
//计算某一月份的最大天数
calendar1.set(Calendar.YEAR, 2000);
calendar1.set(Calendar.MONTH, 1);
calendar1.set(Calendar.DAY_OF_MONTH, 12);
Date date1 = calendar1.getTime();
System.out.println(date1);
//Date的格式化
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(date1));//将Date转化成String
//将String转化成Date
try {
System.out.println(sdf.parse("2000-09-10"));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//给日期加天数
Calendar c6 = Calendar.getInstance();
c6.clear();
c6.set(Calendar.YEAR, 2008);
c6.set(Calendar.MONTH, 1);
c6.set(Calendar.DAY_OF_MONTH, 26);
//c6.add(Calendar.DAY_OF_YEAR, 20);
c6.add(Calendar.DAY_OF_YEAR, -6);
Date d6 = c6.getTime();
System.out.println("-->"+sdf.format(d6));