/**
* 日期工具类
* <p>
* 使用LocalDateTime存取或判断
* </p>
*/
@Component
public class DateUtils {
/**
* 判断两个时间段是否重叠
* @param slot1
* @param slot2
* @return
*/
public static boolean overlapped(TimeSlot slot1, TimeSlot slot2) {
TimeSlot previous, next;
previous = slot1.startTime.isBefore(slot2.startTime) ? slot1 : slot2;
next = slot2.startTime.isAfter(slot1.startTime) ? slot2 : slot1;
// 这里业务需要,允许时间点的重叠
// 例如某个时间段的起始时间:2020-06-29 00:00:00
// 和另一个时间段的终止时间:2020-06-29 00:00:00
// 它们俩可以有交点。如果不需要这种逻辑只把le改成lt
// ,ge改成gt就可
return !(lt(previous, next) || gt(previous, next));
}
/**
* 上个月的最后一天
*/
public static String afterMonthLastDay() {
Calendar c = Calendar.getInstance();
//当前日期设置为指定日期
c.setTime(new Date());
//指定日期月份减去一
c.add(Calendar.MONTH, -1);
//指定日期月份减去一后的 最大天数
c.set(Calendar.DATE, c.getActualMaximum(Calendar.DATE));
//获取上给月最后一天的日期
Date lastDateOfPrevMonth = c.getTime();
java.text.DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
return formater.format(lastDateOfPrevMonth);
}
/**
* 构造一个时间段
* @param startTime
* @param endTime
* @return
*/
public static TimeSlot buildSlot(LocalDateTime startTime, LocalDateTime endTime) {
return new TimeSlot(startTime, endTime);
}
/**
* less equal
* 小于等于
* @param prev
* @param next
* @return
*/
private static boolean le(TimeSlot prev, TimeSlot next) {
return lt(prev, next) || next.endTime.isEqual(prev.startTime);
}
/**
* greater equal
* 大于等于
* @param prev
* @param next
* @return
*/
private static boolean ge(TimeSlot prev, TimeSlot next) {
return gt(prev, next) || prev.endTime.isEqual(next.startTime);
}
/**
* greater than
* 大于
* @param prev
* @param next
* @return
*/
private static boolean gt(TimeSlot prev, TimeSlot next) {
return prev.endTime.isBefore(next.startTime);
}
/**
* less than
* 小于
* @param prev
* @param next
* @return
*/
private static boolean lt(TimeSlot prev, TimeSlot next) {
return next.endTime.isBefore(prev.startTime);
}
/**
* 将单元格数据转换为日期样式的字符串
*
* @param cell
* @return
*/
public static String convertDate(Cell cell) {
String value = "";
if (cell.getCellType() == CellType.NUMERIC) {
if (HSSFDateUtil.isCellDateFormatted(cell)) {
//用于转化为日期格式
Date d = cell.getDateCellValue();
java.text.DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
value = formater.format(d);
}
}
return value;
}
/**
* 将单元格数据转换为日期样式的字符串
*
* @param cell
* @return
*/
public static String convertString(Cell cell) {
String value = "";
if (cell.getCellType() == CellType.STRING) {
value = cell.getStringCellValue();
}
return value;
}
/**
* 将单元格数据转换为金额样式的字符串
* @author:xhmaca
* @param cell
* @return
*/
public static String convertBigDecimal(Cell cell) {
String value = "";
if (cell.getCellType() == CellType.NUMERIC) {
if (HSSFDateUtil.isCellDateFormatted(cell)) {
} else {
BigDecimal bigDecimal = new BigDecimal(cell.getNumericCellValue());
value = bigDecimal.toString();
}
}
return value;
}
/**
* 计算字符型日期天数差
* @param dateStart
* @param dateStop
*/
public static Integer dateDifferent(String dateStart, String dateStop) {
long diff,diffSeconds,diffMinutes,diffHours,diffDays = 0;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
Date d1 = format.parse(dateStart);//毫秒ms
Date d2 = format.parse(dateStop);//毫秒ms
diff = d2.getTime() - d1.getTime();
diffSeconds = diff / 1000 % 60;
diffMinutes = diff / (60 * 1000) % 60;
diffHours = diff / (60 * 60 * 1000) % 24;
diffDays = diff / (24 * 60 * 60 * 1000);
System.out.print("两个时间相差:");
System.out.print(diffDays + "天,");
System.out.print(diffHours + "小时,");
System.out.print(diffMinutes + "分钟,");
System.out.print(diffSeconds + "秒.");
} catch (ParseException e) {
e.printStackTrace();
}
int days = (int) diffDays;
return days;
}
/**
* 计算字符型日期天数差
* @param dateStart
* @param dateStop
*/
public static long datelongDifferent(String dateStart, String dateStop) {
long diff,diffSeconds,diffMinutes,diffHours,diffDays = 0;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
Date d1 = format.parse(dateStart);//毫秒ms
Date d2 = format.parse(dateStop);//毫秒ms
diff = d2.getTime() - d1.getTime();
diffSeconds = diff / 1000 % 60;
diffMinutes = diff / (60 * 1000) % 60;
diffHours = diff / (60 * 60 * 1000) % 24;
diffDays = diff / (24 * 60 * 60 * 1000);
System.out.print("两个时间相差:");
System.out.print(diffDays + "天,");
System.out.print(diffHours + "小时,");
System.out.print(diffMinutes + "分钟,");
System.out.print(diffSeconds + "秒.");
} catch (ParseException e) {
e.printStackTrace();
}
return diffDays;
}
/**
* 获取当前日期
*/
public static String getCurrectDate()
{
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();//当前日期
return df.format(date);
}
/**
* 获取当前日期
*/
public static String getLongFormatDate(String date)
{
SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df1.format(date);
}
/**
* 获取当前日期
*/
public static String getShortFormatDate(String date)
{
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return df.format(date);
}
/**
* 计算年份
* @param dateStart
*
*/
public static Integer getYear(String dateStart) {
long diff,diffSeconds,diffMinutes,diffHours,diffDays = 0;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
Date d1 = format.parse(dateStart);//毫秒ms
return d1.getYear()+1900;
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
/**
* 时间段类
*/
static class TimeSlot{
private LocalDateTime startTime;
private LocalDateTime endTime;
public TimeSlot(LocalDateTime startTime, LocalDateTime endTime) {
if (startTime.isAfter(endTime)) {
this.startTime = endTime;
this.endTime = startTime;
} else {
this.startTime = startTime;
this.endTime = endTime;
}
}
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。