java获取当月第一天的零点零分和最后一天的23点59分

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 1);
//将小时至0
calendar.set(Calendar.HOUR_OF_DAY, 0);
//将分钟至0
calendar.set(Calendar.MINUTE, 0);
//将秒至0
calendar.set(Calendar.SECOND,0);
//将毫秒至0
calendar.set(Calendar.MILLISECOND, 0);
//获得当前月第一天
Date sdate = calendar.getTime();
//将当前月加1;
calendar.add(Calendar.MONTH, 1);
//在当前月的下一月基础上减去1毫秒
calendar.add(Calendar.MILLISECOND, -1);
//获得当前月最后一天
Date edate = calendar.getTime();

System.out.println("The first day of month=="+sdate.toString());
System.out.println("first day:"+sdate.getTime());
System.out.println("The last day of month=="+edate.toString());
System.out.println("last day:"+edate.getTime());

 作为备忘,也希望高人给出更优方法

你可能感兴趣的:(Java,Date)