Maison >Java >javaDidacticiel >关于时间,日期,星期,月份的算法(Java中Calendar的使用方法)
package cn.outofmemory.codes.Date; import java.util.Calendar; import java.util.Date; public class CalendarDemo { public static void main(String[] args) { Calendar calendar=Calendar.getInstance(); calendar.setTime(new Date()); System.out.println("现在时间是:"+new Date()); String year=String.valueOf(calendar.get(Calendar.YEAR)); String month=String.valueOf(calendar.get(Calendar.MONTH)+1); String day=String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)); String week=String.valueOf(calendar.get(Calendar.DAY_OF_WEEK)-1); System.out.println("现在时间是:"+year+"年"+month+"月"+day+"日,星期"+week); long year2009=calendar.getTimeInMillis(); calendar.set(1989,9,26);//这里与真实的月份之间相差1 long year1989=calendar.getTimeInMillis(); long days=(year2009-year1989)/(1000*60*60*24); System.out.println("今天和1989年10月26日相隔"+days+"天,"+"也就是说我在这个美丽的星球上已经幸福的生活了"+days+"天。"); } }Calendar类的静态方法getInstance()可以初始化一个日历对象:
set(int year ,int month,int date) set(int year ,int month,int date,int hour,int minute) set(int year ,int month,int date,int hour,int minute,int second)
Calendar time=Calendar.getInstance(); time.clear(); time.set(Calendar.YEAR,year); time.set(Calendar.MONTH,i-1);//注意,Calendar对象默认一月为0 int day=time.getActualMaximum(Calendar.DAY_OF_MONTH);//本月份的天数
Calendar cal=Calendar.getInstance(); Date date=cal.getTime();
Date date=new Date(); Calendar cal=Calendar.getInstance(); cal.setTime(date);
Date date=new Date(); SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); System.out.println(df.format(date));
Calendar cal=Calendar.getInstance(); cal.set(Calendar.YEAR, 2006); cal.set(Calendar.MONTH, 8); cal.set(Calendar.DAY_OF_MONTH, 3); int weekno=cal.get(Calendar.WEEK_OF_YEAR);(2)计算一年中的第几星期是几号
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd"); Calendar cal=Calendar.getInstance(); cal.set(Calendar.YEAR, 2006); cal.set(Calendar.WEEK_OF_YEAR, 1); cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); System.out.println(df.format(cal.getTime()));
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd"); Calendar cal=Calendar.getInstance(); cal.set(Calendar.YEAR, 2006); cal.set(Calendar.MONTH, 8); cal.set(Calendar.DAY_OF_MONTH, 3); cal.add(Calendar.DATE, -4); Date date=cal.getTime(); System.out.println(df.format(date)); cal.add(Calendar.DATE, 4); date=cal.getTime(); System.out.println(df.format(date));
cal.set(Calendar.YEAR, 2006); cal.set(Calendar.MONTH, 8); cal.set(Calendar.DAY_OF_MONTH, 3); cal.roll(Calendar.DATE, -4); date=cal.getTime(); System.out.println(df.format(date)); cal.roll(Calendar.DATE, 4); date=cal.getTime(); System.out.println(df.format(date));
/ **计算两个时间之间相隔天数 * @param startday 开始时间 * @param endday 结束时间 * @return */ public int getIntervalDays(Calendar startday,Calendar endday){ //确保startday在endday之前 if(startday.after(endday)){ Calendar cal=startday; startday=endday; endday=cal; } //分别得到两个时间的毫秒数 long sl=startday.getTimeInMillis(); long el=endday.getTimeInMillis(); long ei=el-sl; //根据毫秒数计算间隔天数 return (int)(ei/(1000*60*60*24)); }
/**计算两个时间之间相隔天数 * @param startday 开始时间 * @param endday 结束时间 * @return */ public int getIntervalDays(Date startday,Date endday){ //确保startday在endday之前 if(startday.after(endday)){ Date cal=startday; startday=endday; endday=cal; } //分别得到两个时间的毫秒数 long sl=startday.getTime(); long el=endday.getTime(); long ei=el-sl; //根据毫秒数计算间隔天数 return (int)(ei/(1000*60*60*24)); }
endday.set(Calendar.HOUR_OF_DAY, 23); endday.set(Calendar.MINUTE, 59); endday.set(Calendar.SECOND, 59); endday.set(Calendar.MILLISECOND, 59);这样再传进去startday,endday,则结果就如我们所愿了。不过,如果嫌以上方法麻烦,可以参考以下方法:
public int getDaysBetween (Calendar d1, Calendar d2) { if (d1.after(d2)) { // swap dates so that d1 is start and d2 is end java.util.Calendar swap = d1; d1 = d2; d2 = swap; } int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR); int y2 = d2.get(Calendar.YEAR); if (d1.get(Calendar.YEAR) != y2) { d1 = (Calendar) d1.clone(); do { days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);//得到当年的实际天数 d1.add(Calendar.YEAR, 1); } while (d1.get(Calendar.YEAR) != y2); } return days; }