2023-03-11 16:48:52 +08:00
|
|
|
|
package cn.trans88.taxiappkotlin.util;
|
|
|
|
|
|
2023-11-17 15:09:43 +08:00
|
|
|
|
import android.os.Build;
|
2023-03-11 16:48:52 +08:00
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
2023-11-17 15:09:43 +08:00
|
|
|
|
import androidx.annotation.RequiresApi;
|
|
|
|
|
|
2023-03-11 16:48:52 +08:00
|
|
|
|
import java.text.SimpleDateFormat;
|
2023-11-17 15:09:43 +08:00
|
|
|
|
import java.time.Instant;
|
|
|
|
|
import java.time.LocalDate;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.time.ZoneId;
|
2023-03-11 16:48:52 +08:00
|
|
|
|
import java.util.Calendar;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Author: LJH
|
|
|
|
|
* @Time: 2023/2/15
|
|
|
|
|
* @description:
|
|
|
|
|
*/
|
|
|
|
|
public class DateUtil {
|
|
|
|
|
|
|
|
|
|
public static SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取任务第一次执行的开始或者结束日期
|
|
|
|
|
* @param weekDay
|
|
|
|
|
* @param startOrEndTime
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static Calendar getStartOrEndCalendar(Integer weekDay,Long startOrEndTime){
|
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
|
calendar.setTime(new Date(startOrEndTime));
|
|
|
|
|
int hourTime = calendar.get(Calendar.HOUR_OF_DAY);
|
|
|
|
|
int minuteTime = calendar.get(Calendar.MINUTE);
|
|
|
|
|
int secondTime = calendar.get(Calendar.SECOND);
|
|
|
|
|
// 获取当前时间
|
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
|
|
|
|
|
|
// 计算距离下一个周一的天数,SUNDAY = 1,Calendar.MONDAY == 2,依此类推SATURDAY == 7
|
|
|
|
|
int day = weekDay - cal.get(Calendar.DAY_OF_WEEK);
|
|
|
|
|
if (day <= 0) {
|
|
|
|
|
day += 7;
|
|
|
|
|
}
|
|
|
|
|
if (!(cal.get(Calendar.DAY_OF_WEEK) == weekDay &&
|
|
|
|
|
(cal.get(Calendar.HOUR_OF_DAY) < hourTime || (cal.get(Calendar.HOUR_OF_DAY) == hourTime &&
|
|
|
|
|
cal.get(Calendar.MINUTE) < minuteTime) || (cal.get(Calendar.HOUR_OF_DAY) == hourTime &&
|
|
|
|
|
cal.get(Calendar.MINUTE) == minuteTime && cal.get(Calendar.SECOND) < secondTime)))
|
|
|
|
|
){
|
|
|
|
|
// 设置日期为下一个weekDay
|
|
|
|
|
cal.add(Calendar.DAY_OF_MONTH, day);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置时间
|
|
|
|
|
cal.set(Calendar.HOUR_OF_DAY, hourTime);
|
|
|
|
|
cal.set(Calendar.MINUTE, minuteTime);
|
|
|
|
|
cal.set(Calendar.SECOND, secondTime);
|
|
|
|
|
|
|
|
|
|
// 获取日期
|
|
|
|
|
Date date = cal.getTime();
|
|
|
|
|
return cal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取第一次执行任务的开始或者结束日期至下一次开始或结束日期的时间差
|
|
|
|
|
* @param weekDay
|
|
|
|
|
* @param startOrEndTime
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static long getTimeDiff(Calendar calendar,Integer weekDay,Long startOrEndTime){
|
|
|
|
|
|
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
|
cal.setTime(new Date(startOrEndTime));
|
|
|
|
|
int hourTime = cal.get(Calendar.HOUR_OF_DAY);
|
|
|
|
|
int minuteTime = cal.get(Calendar.MINUTE);
|
|
|
|
|
int secondTime = cal.get(Calendar.SECOND);
|
|
|
|
|
|
|
|
|
|
Date firstDate = calendar.getTime();
|
|
|
|
|
// System.out.println("firstDate:"+dateTimeFormat.format(firstDate));
|
|
|
|
|
// 计算距离下一个weekDay的天数,SUNDAY = 1,Calendar.MONDAY == 2,依此类推SATURDAY == 7
|
|
|
|
|
int day = weekDay - calendar.get(Calendar.DAY_OF_WEEK);
|
|
|
|
|
if (day <= 0) {
|
|
|
|
|
day += 7;
|
|
|
|
|
}
|
|
|
|
|
//设置日期为下一个weekDay
|
|
|
|
|
calendar.add(Calendar.DAY_OF_MONTH,day);
|
|
|
|
|
// 设置时间为10点
|
|
|
|
|
calendar.set(Calendar.HOUR_OF_DAY, hourTime);
|
|
|
|
|
calendar.set(Calendar.MINUTE, minuteTime);
|
|
|
|
|
calendar.set(Calendar.SECOND, secondTime);
|
|
|
|
|
//获取下一个周一的日期
|
|
|
|
|
Date secondDate = calendar.getTime();
|
|
|
|
|
// System.out.println("secondDate:"+dateTimeFormat.format(secondDate));
|
|
|
|
|
return secondDate.getTime() - firstDate.getTime();
|
|
|
|
|
}
|
2023-09-01 16:13:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取当前时间
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String nowTime(){
|
|
|
|
|
String now = dateTimeFormat.format(new Date());
|
|
|
|
|
return now;
|
|
|
|
|
}
|
2023-11-17 15:09:43 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 判断当前系统时间跟缓存的 应用开始运行时间对比,是不是第二天的时间
|
|
|
|
|
* 获取时间差,当前时间与缓存时间相差的天数
|
|
|
|
|
* @param currentTime
|
|
|
|
|
* @param cacheTime
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static int getDaysDiff(long currentTime, long cacheTime){
|
2023-12-12 16:29:08 +08:00
|
|
|
|
Log.d("yzd_t", "currentTime: " + currentTime + ", cacheStartTime: " + cacheTime);
|
2023-11-17 15:09:43 +08:00
|
|
|
|
int dayType = DayType.IS_OTHER_DAY.ordinal();
|
|
|
|
|
// long currentTimeStamp = 1699723270L; // 替换为currentTimeStamp的值 2023/11/12
|
|
|
|
|
// long lastTimeStamp = 1699690270L; // 替换为lastTimeStamp的值 2023/11/11
|
|
|
|
|
|
|
|
|
|
// 将时间戳转换为Date对象
|
|
|
|
|
// Date currentDate = new Date(currentTimeStamp * 1000L);
|
|
|
|
|
// Date lastDate = new Date(lastTimeStamp * 1000L);
|
|
|
|
|
Date currentDate = new Date(currentTime);
|
|
|
|
|
Date lastDate = new Date(cacheTime);
|
|
|
|
|
|
|
|
|
|
// 创建Calendar对象并设置时间
|
|
|
|
|
Calendar currentCalendar = Calendar.getInstance();
|
|
|
|
|
currentCalendar.setTime(currentDate);
|
|
|
|
|
Calendar lastCalendar = Calendar.getInstance();
|
|
|
|
|
lastCalendar.setTime(lastDate);
|
|
|
|
|
|
|
|
|
|
// 设置时间为午夜
|
|
|
|
|
currentCalendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
|
|
currentCalendar.set(Calendar.MINUTE, 0);
|
|
|
|
|
currentCalendar.set(Calendar.SECOND, 0);
|
|
|
|
|
currentCalendar.set(Calendar.MILLISECOND, 0);
|
|
|
|
|
|
|
|
|
|
lastCalendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
|
|
lastCalendar.set(Calendar.MINUTE, 0);
|
|
|
|
|
lastCalendar.set(Calendar.SECOND, 0);
|
|
|
|
|
lastCalendar.set(Calendar.MILLISECOND, 0);
|
|
|
|
|
|
|
|
|
|
// 判断日期差异
|
|
|
|
|
long diff = currentCalendar.getTimeInMillis() - lastCalendar.getTimeInMillis();
|
|
|
|
|
int daysDiff = (int) (diff / (24 * 60 * 60 * 1000));
|
|
|
|
|
Log.d("yzd_t", "daysDiff: " + daysDiff);
|
|
|
|
|
|
|
|
|
|
if (daysDiff == 1) {
|
|
|
|
|
Log.d("yzd_t","currentTimeStamp对应的时间是lastTimeStamp的后一天的时间。");
|
|
|
|
|
dayType = DayType.IS_SECOND_DAY.ordinal();
|
|
|
|
|
} else if (daysDiff == 0) {
|
|
|
|
|
Log.d("yzd_t","currentTimeStamp对应的时间不是lastTimeStamp的后一天的时间。");
|
|
|
|
|
dayType = DayType.IS_SAME_DAY.ordinal();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dayType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-03-11 16:48:52 +08:00
|
|
|
|
}
|