1,当做查询条件时候 可以查询当天的信息 从当天00.00———— 23.59.59
startTime = DateUtil.beginOfDay((Date) startTime);
endTime = DateUtil.endOfDay((Date) endTime);
2.时间转为String 类型
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
protected String STR = sdf.format(new Date());
3.String转为时间类型
Date date = sdf.parse(str);
4.得到日历中的月初和月末
Calendar calendar = Calendar.getInstance();
//得到月初
calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
Date strDateFrom = calendar.getTime();
System.out.println(strDateFrom);
//得到月末
calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
Date strDateTo = calendar.getTime();
System.out.println(strDateTo);
5.判断是否是周末的方法
public boolean isWeekend(String date) throws ParseException {
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
Date bdate = format1.parse(date);
Calendar cal = Calendar.getInstance();
cal.setTime(bdate);
if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
return true;
} else return false;
}
6.月份相加
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
//cal.set(Calendar.MONTH, oldMonth);
cal.add(Calendar.MONTH,5);
System.out.println(cal.getTime());