java组件:获取查询月份的第一天和最后一天,默认取当前月份
public DateSegment GetStartTimeEndTimeOfMonth(String dateStr) {
DateSegment dateSegment = new DateSegment();
// 设置查询月第一天
Calendar firstCa = Calendar.getInstance();
// 设置查询月最后
Calendar lastCa = Calendar.getInstance();
// 当前时间
Date userDate = new Date();
if (StringHelper.hasContent(dateStr)) {
try {
userDate = new SimpleDateFormat("yyyy-MM").parse(dateStr);
} catch (ParseException e1) {
}
}
// 查询的第一个开始时间,该月份的上一个月份1号
firstCa.setTime(userDate);
firstCa.set(Calendar.DAY_OF_MONTH, 1);
firstCa.set(Calendar.HOUR_OF_DAY, 0);
firstCa.set(Calendar.MINUTE, 0);
firstCa.set(Calendar.SECOND, 0);
firstCa.set(Calendar.MILLISECOND, 1);
String firststr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(firstCa.getTime());
DateSegment.setBeginDate(firststr);
// 设置查询月最后一天
lastCa.setTime(firstCa.getTime());
lastCa.set(Calendar.DAY_OF_MONTH, lastCa.getActualMaximum(Calendar.DAY_OF_MONTH));
lastCa.set(Calendar.HOUR_OF_DAY, 23);
lastCa.set(Calendar.MINUTE, 59);
lastCa.set(Calendar.SECOND, 59);
String laststr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(lastCa.getTime());
DateSegment.setStopDate(laststr);
return DateSegment;
}
/**
* <ul>
* <li>beginDate:开始时间</li>
* <li>stopDate:结束时间</li>
* </ul>
*/
public class DateSegment {
private String beginDate;
private String stopDate;
@Override
public String toString() {
return StringHelper.toJsonString(this);
}
}
每天努力一点,每天都在进步。
原文链接:https://blog.csdn.net/dingjianmin/article/details/94408884
public void testSelect2() throws ParseException {
String dateStr="2021-02";
DateSegment dateSegment = new DateSegment();
Calendar firstCa = Calendar.getInstance();
Calendar lastCa = Calendar.getInstance();
Date userDate = new SimpleDateFormat("yyyy-MM").parse(dateStr);
firstCa.setTime(userDate);
firstCa.set(Calendar.DAY_OF_MONTH, 1);
firstCa.set(Calendar.HOUR_OF_DAY, 0);
firstCa.set(Calendar.MINUTE, 0);
firstCa.set(Calendar.SECOND, 0);
firstCa.set(Calendar.MILLISECOND, 1);
String firstStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(firstCa.getTime());
System.out.println(firstStr);
lastCa.setTime(firstCa.getTime());
lastCa.set(Calendar.DAY_OF_MONTH, lastCa.getActualMaximum(Calendar.DAY_OF_MONTH));
lastCa.set(Calendar.HOUR_OF_DAY, 23);
lastCa.set(Calendar.MINUTE, 59);
lastCa.set(Calendar.SECOND, 59);
String lastStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(lastCa.getTime());
System.out.println(lastStr);