package com.soft.mpms.gress.speed.zframe.common;
import com.github.pagehelper.util.StringUtil;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class calculationTimeFrame {
public static void main(String[] args) {
for(int i = 1; i <= 12; i++){
calculationTimeFrameByMonthFour("2025", i+"");
}
}
// 通过年和月计算时间范围(上周四到本周三)
public static List<String> calculationTimeFrameByMonthFour(String dataYear,String dataMonth) {
List<String> list = new ArrayList<String>();
int year = Integer.parseInt(dataYear);
int month = Integer.parseInt(dataMonth)-1;
Calendar c = new GregorianCalendar();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DATE, 1);
Date date = c.getTime();
//计算本月的第一个周四是几号
Date fridayByMonth = getFirstWeekByMonthFour(date);
if(daysBetween(date,fridayByMonth) < 7 && daysBetween(date,fridayByMonth) > 4){
fridayByMonth = cutdays(fridayByMonth,7);
}
getTimeFrameFour(fridayByMonth,30,list);
return list;
}
public static String getTimeFrameFour(Date date,int n,List<String> list){
String startDate = DateUtil.dateFormart(date, "yyyy/MM/dd");
Date enddate = getLastDayOfWeekFour(date);
String endDate = DateUtil.dateFormart(enddate, "yyyy/MM/dd");
String timeFrame = startDate + "~" + endDate;
list.add(timeFrame);
System.out.println(timeFrame);
Date lastDayByMonth = DateUtil.StringToDate(getLastDayByMonth(endDate,"yyyy/MM/dd"), "yyyy/MM/dd");
if(daysBetween(enddate, lastDayByMonth) >= 3 && n > daysBetween(enddate, lastDayByMonth)){
n = daysBetween(enddate, lastDayByMonth);
getTimeFrameFour(addOneday(endDate,"yyyy/MM/dd"),n,list);
}else{
n = 30;
}
return timeFrame;
}
// 获取当前时间所在周的结束日期
public static Date getLastDayOfWeekFour(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.THURSDAY);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // 星期三
return c.getTime();
}
//计算本月的第一个周四是几号
public static Date getFirstWeekByMonthFour(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int i = 1;
//DAY_OF_WEEK获取当前时间是一个星期的第几天,星期日是第一天 星期一是第二天,以此类推
//Calendar.FRIDAY判断是不是星期四
while(cal.get(Calendar.DAY_OF_WEEK) != Calendar.THURSDAY){
cal.set(Calendar.DAY_OF_MONTH, i++);//设置这个月的星期1 为几号
}
Date firstMonday = cal.getTime();//取得日期和时间
return firstMonday;
}
public static Date addOneday(String today,String sdf){
SimpleDateFormat f = new SimpleDateFormat(sdf);
try {
Date d = new Date(f.parse(today).getTime()+24*3600*1000);
return d;
}
catch(Exception ex) {
ex.printStackTrace();
return new Date();
}
}
//获取当前月的最后一天
private static String getLastDayByMonth(String dateStr,String sdf){
String endTimeStr = "";
try {
SimpleDateFormat datef=new SimpleDateFormat(sdf);
Date date = datef.parse(dateStr);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
//当前月的最后一天
cal.set( Calendar.DATE, 1 );
cal.roll(Calendar.DATE, - 1 );
Date endTime=cal.getTime();
endTimeStr = datef.format(endTime);
// System.out.println(endTimeStr);
} catch (Exception e) {
e.printStackTrace();
}
return endTimeStr;
}
/**
*
* @Title: StringToDate
* @Description: 字符串转换日期
* @param @param dateStr
* @param @param formatStr
* @param @return 设定文件
* @return Date 返回类型
* @throws
*/
public static Date StringToDate(String dateStr, String formatStr) {
DateFormat sdf = new SimpleDateFormat(formatStr);
Date date = null;
if (!StringUtil.isEmpty(dateStr)) {
try {
date = sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
}
return date;
}
public static String dateFormart(Date date, String formart) {
// 初始化返回值
String returnStr = "";
if (date != null) {
// 格式
SimpleDateFormat df = new SimpleDateFormat(formart);
// 格式化结果
returnStr = df.format(date);
}
// 返回
return returnStr;
}
private static Date cutdays(Date date, int i){
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
try {
Date d = new Date(date.getTime()-i *24*3600*1000);
return d;
}catch(Exception ex) {
return new Date();
}
}
private static int daysBetween(Date smdate,Date bdate) {
long between_days = 0L;
try {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
smdate=sdf.parse(sdf.format(smdate));
bdate=sdf.parse(sdf.format(bdate));
Calendar cal = Calendar.getInstance();
cal.setTime(smdate);
long time1 = cal.getTimeInMillis();
cal.setTime(bdate);
long time2 = cal.getTimeInMillis();
between_days=(time2-time1)/(1000*3600*24);
} catch (ParseException e) {
e.printStackTrace();
}
return Integer.parseInt(String.valueOf(between_days));
}
}