package pub;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang.time.DateUtils;
public class GetDate {
// 获取上周一
public String getLastWeekMonday(Date date) {
Date a = DateUtils.addDays(date, -1);
Calendar cal = Calendar.getInstance();
cal.setTime(a);
cal.add(Calendar.WEEK_OF_YEAR, -1);// 一周
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
System.out.println("上周一" + df.format(cal.getTime()));
return df.format(cal.getTime());
}
// 获取上周日
public String getLastWeekSunday(Date date) {
Date a = DateUtils.addDays(date, -1);
Calendar cal = Calendar.getInstance();
cal.setTime(a);
cal.set(Calendar.DAY_OF_WEEK, 1);
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
System.out.println("上周日" + df.format(cal.getTime()));
return df.format(cal.getTime());
}
// 获取上月第一天
public static String getLastMonthDayOne(Date date) {
Calendar calendar1 = Calendar.getInstance();
calendar1.add(Calendar.MONTH, -1);
calendar1.set(Calendar.DAY_OF_MONTH, 1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
System.out.println("上月初:" + sdf.format(calendar1.getTime()));
return sdf.format(calendar1.getTime());
}
// 获取上月最后一天
public static String getLastMonthLastDay(Date date) {
Calendar calendar2 = Calendar.getInstance();
calendar2.set(Calendar.DAY_OF_MONTH, 0);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
System.out.println("上月末:" + sdf.format(calendar2.getTime()));
return sdf.format(calendar2.getTime());
}
public static void main(String[] args) {
/*Date date = new Date();
getLastMonthDayOne(date);
getLastMonthLastDay(date);*/
}
}
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang.time.DateUtils;
public class GetDate {
// 获取上周一
public String getLastWeekMonday(Date date) {
Date a = DateUtils.addDays(date, -1);
Calendar cal = Calendar.getInstance();
cal.setTime(a);
cal.add(Calendar.WEEK_OF_YEAR, -1);// 一周
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
System.out.println("上周一" + df.format(cal.getTime()));
return df.format(cal.getTime());
}
// 获取上周日
public String getLastWeekSunday(Date date) {
Date a = DateUtils.addDays(date, -1);
Calendar cal = Calendar.getInstance();
cal.setTime(a);
cal.set(Calendar.DAY_OF_WEEK, 1);
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
System.out.println("上周日" + df.format(cal.getTime()));
return df.format(cal.getTime());
}
// 获取上月第一天
public static String getLastMonthDayOne(Date date) {
Calendar calendar1 = Calendar.getInstance();
calendar1.add(Calendar.MONTH, -1);
calendar1.set(Calendar.DAY_OF_MONTH, 1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
System.out.println("上月初:" + sdf.format(calendar1.getTime()));
return sdf.format(calendar1.getTime());
}
// 获取上月最后一天
public static String getLastMonthLastDay(Date date) {
Calendar calendar2 = Calendar.getInstance();
calendar2.set(Calendar.DAY_OF_MONTH, 0);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
System.out.println("上月末:" + sdf.format(calendar2.getTime()));
return sdf.format(calendar2.getTime());
}
public static void main(String[] args) {
/*Date date = new Date();
getLastMonthDayOne(date);
getLastMonthLastDay(date);*/
}
}
日期操作工具
本文提供了一组实用的方法来获取特定的日期,例如上周一、上周日、上个月的第一天和最后一天。通过这些方法,可以方便地进行日期范围的确定,适用于报表统计、日志归档等场景。
660

被折叠的 条评论
为什么被折叠?



