package com.sport.sportcloudmarathonh5.utils;
import cn.hutool.core.convert.Convert;
import org.springframework.util.ObjectUtils;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;
/**
* @author zdj
* @version 1.0
* @date 2022/6/22 15:41
*/
public class DateUtils {
/**
* 指定日期加上天数后的日期
*/
public static Date plusDay(int num, Date currdate) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(currdate);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Date zero = calendar.getTime();
Calendar ca = Calendar.getInstance();
ca.setTime(zero);
ca.add(Calendar.DATE, num);// num为增加的天数,可以改变的
currdate = ca.getTime();
return currdate;
}
/**
* 获取当前时间到晚上12点有多少秒
*/
public static long getCurTimeToNightSecond() {
long epochMilli = LocalDateTime.of(LocalDate.now(), LocalTime.MAX).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
long curTimeMilli = LocalDateTime.now().toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
return (epochMilli - curTimeMilli) / 1000;
}
/**
* 获取当前时间到指定时间的秒数
* @param localDateTime
* @return
*/
public static long getSecondByDatetime(LocalDateTime localDateTime){
Duration between = Duration.between(LocalDateTime.now(), localDateTime);
return between.toMinutes() * 60;
}
/**
* 将秒转为时分秒格式【01:01:01】
* @param second 需要转化的秒数
* @return
*/
public static String secondConvertHourMinSecond(Integer second) {
if(!ObjectUtils.isEmpty(second)){
second = Math.abs(second);
}
String str = "00:00:00";
if (second == null || second <= 0) {
return str;
}
// 得到小时
long h = second / 3600;
str = h > 0 ? ((h < 10 ? ("0" + h) : h) + ":") : "00:";
// 得到分钟
long m = (second % 3600) / 60;
str += (m < 10 ? ("0" + m) : m) + ":";
//得到剩余秒
long s = second % 60;
str += (s < 10 ? ("0" + s) : s);
return str;
}
/**
* 将【01:01:01】格式转为秒
* @param sfmStr 需要转化的时间字符串,格式为[01:01:01]
* @return
*/
public static Integer sfmStrConvertSecond(String sfmStr) {
Integer convertEdSecond = 0;
// 格式验证,格式不满足的直接返回0
if(ObjectUtils.isEmpty(sfmStr) || !sfmStr.contains(":") || sfmStr.split(":").length != 3){
return convertEdSecond;
}
// 获取时分秒
String [] sfmArray = sfmStr.split(":");
Integer hour = Convert.toInt(sfmArray[0]);
Integer minute = Convert.toInt(sfmArray[1]);
Integer second = Convert.toInt(sfmArray[2]);
return (hour * 3600) + (minute * 60) + second;
}
/**
* 获取指定类型的开始时间
* @param type 1天,2周,3月,4年
* @return
*/
public static LocalDateTime getStartTimeByType(int type) {
if(type == 1){
return LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
} else if(type == 2){
return LocalDateTime.of(LocalDate.now().with(DayOfWeek.MONDAY), LocalTime.MIN);
} else if(type == 3){
return LocalDateTime.of(LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN);
} else if(type == 4){
return LocalDateTime.of(LocalDate.now().with(TemporalAdjusters.firstDayOfYear()), LocalTime.MIN);
}
return null;
}
/**
* 获取指定类型的结束时间
* @param type 1天,2周,3月,4年
* @return
*/
public static LocalDateTime getEndTimeByType(int type) {
if(type == 1){
return LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
} else if(type == 2){
return LocalDateTime.of(LocalDate.now().with(DayOfWeek.SUNDAY), LocalTime.MAX);
} else if(type == 3){
return LocalDateTime.of(LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX);
} else if(type == 4){
return LocalDateTime.of(LocalDate.now().with(TemporalAdjusters.lastDayOfYear()), LocalTime.MAX);
}
return null;
}
/**
* 将指定时间,转为指定格式
* @param localDateTime 需要处理的时间
* @param type 展示格式(1横杠格式,2文字格式)
* @param model 展示类型(1年月日,2年月日 时分,3年月日 时分秒,4年月,5年,6月,7日,8时分秒,9时分)
* @return 处理后的时间
*/
public static String convertTimeToStringByPattern(LocalDateTime localDateTime, int type, int model) {
String pattern = "";
if(type == 2){
switch(model){
case 1: pattern = "yyyy年MM月dd日"; break;
case 2: pattern = "yyyy年MM月dd日 HH时mm分"; break;
case 3: pattern = "yyyy年MM月dd日 HH时mm分ss秒"; break;
case 4: pattern = "yyyy年MM月"; break;
case 5: pattern = "yyyy年"; break;
case 6: pattern = "MM月"; break;
case 7: pattern = "dd日"; break;
case 8: pattern = "HH时mm分ss秒"; break;
case 9: pattern = "HH时mm分"; break;
}
} else {
switch(model){
case 1: pattern = "yyyy-MM-dd"; break;
case 2: pattern = "yyyy-MM-dd HH:mm"; break;
case 3: pattern = "yyyy-MM-dd HH:mm:ss"; break;
case 4: pattern = "yyyy-MM"; break;
case 5: pattern = "yyyy"; break;
case 6: pattern = "MM"; break;
case 7: pattern = "dd"; break;
case 8: pattern = "HH:mm:ss"; break;
case 9: pattern = "HH:mm"; break;
}
}
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
return dtf.format(localDateTime);
}
/**
* 将小时转为时分(格式:00‘30’‘)
* @param hour 需要转化的小时
* @return
*/
public static String hourConvertHourMinSecond(BigDecimal hour) {
String str = "0`00``";
if (hour.compareTo(new BigDecimal(0)) <= 0) {
return str;
}
// 得到秒
BigDecimal second = hour.multiply(new BigDecimal(3600));
// 得到分钟
long m = (second.intValue() / 60);
str = (m < 10 ? ("0" + m + "`") : m+"``");
//得到剩余秒
long s = (second.intValue() % 60);
str += (s < 10 ? ("0" + s + "`") : s +"``");
return str;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/137462.html