查询某月第一天
private String getFirstDateByMonth(String dateyyyyMM) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM");
Date date = format.parseDateTime(dateyyyyMM).toDate();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);
return sdf.format(calendar.getTime());
}
查询某月最后一天
private String getLastDateByMonth(String dateyyyyMM) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM");
Date date = format.parseDateTime(dateyyyyMM).toDate();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return sdf.format(calendar.getTime());
}
比较两个日期大小
-
date1.getTime()>date2.getTime()
-
date1.compareTo(date2)
相等返回0,date1>date2返回1,date1<date2返回-1
字符串转Date
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = df.parse(dateStr);
Date转字符串
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = sdf.format(date)
日期加一天
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DATE, 1);
date = c.getTime();