根据当前时间获取当月开始和结束日期
)
1、根据当前时间获取当月开始和结束日期
import java.util.Calendar;
import java.util.Date;
/**
* @Author Mouoal
* @Description: 说明用途
* @Date 2023/10/19 9:41
* @Version 1.0
*/
public class DateUtils {
public static Date getCurrentDate() {
String ds = "202030913";
return new Date();
}
public static Date getFirstDayOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);
return calendar.getTime();
}
public static Date getLastDayOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return calendar.getTime();
}
public static void main(String[] args) {
Date currentDate = getCurrentDate();
System.out.println(currentDate);
Date firstDayOfMonth = getFirstDayOfMonth(currentDate);
Date lastDayOfMonth = getLastDayOfMonth(currentDate);
System.out.println("当前日期: " + currentDate);
System.out.println("本月第一天: " + firstDayOfMonth);
System.out.println("本月最后一天: " + lastDayOfMonth);
}
}
2、根据当前日期获取一周的开始和结束日期
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.function.Function;
/**
* @Author Mouoal
* @Description: 说明用途
* @Date 2023/10/13 15:44
* @Version 1.0
*/
public class ddd {
public static class DealWeek {
public static void main(String[] args) throws ParseException {
String date = "20230915";
Calendar c = getCalendarByDateStr(date);
System.out.println(c.get(Calendar.WEEK_OF_YEAR));
String[] result = getStartAndEndDayByDate(date);
System.out.println("周一日期为:" + result[0]);
System.out.println("周天日期为:" + result[1]);
}
//根据日期获取该天所在周的开始与结束日期(周跨年周,只计算本年内那部分天数)
public static String[] getStartAndEndDayByDate(String yyyyMMdd) throws ParseException{
String[] dateZone = new String[3];
Calendar cal = getCalendarByDateStr(yyyyMMdd);
int year = Integer.parseInt(yyyyMMdd.substring(0, 4));
Function<Integer, String> add0 = (i -> i < 10 ? "0" + i : String.valueOf(i));//补0
dateZone[0] = cal.get(Calendar.YEAR) + add0.apply(cal.get(Calendar.MONTH) + 1)+ add0.apply(cal.get(Calendar.DAY_OF_MONTH));
if(cal.get(Calendar.YEAR) < year)dateZone[0] = year + "0101";
cal.add(Calendar.DATE, 6);
dateZone[1] = cal.get(Calendar.YEAR) +add0.apply(cal.get(Calendar.MONTH) + 1) + add0.apply(cal.get(Calendar.DAY_OF_MONTH));
if(cal.get(Calendar.YEAR) > year)dateZone[1] = year + "1231";
dateZone[2] = String.valueOf(cal.get(Calendar.WEEK_OF_YEAR));
return dateZone;
}
private static Calendar getCalendarByDateStr(String yyyyMMdd) throws ParseException {
SimpleDateFormat sm = new SimpleDateFormat("yyyyMMdd");
Date d = sm.parse(yyyyMMdd);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
//判断要计算的日期是否是周日,如果是,则减一天计算周六的,否则会出问题,计算到下一周去了
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
if(1 == dayWeek) cal.add(Calendar.DAY_OF_MONTH, -1);
cal.setFirstDayOfWeek(Calendar.MONDAY);//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
int day = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);//根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
return cal;
}
}
}