first commit
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
package lingtao.net.util;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
public class DateFormatUtils {
|
||||
|
||||
// 开始日期
|
||||
public String formatBeginTime(String add_time_begin) {
|
||||
// 按日期范围统计
|
||||
if (add_time_begin.length() > 20 && !add_time_begin.contains("~")) {
|
||||
return add_time_begin.substring(0, 10);
|
||||
}
|
||||
// 按周统计
|
||||
if (add_time_begin.contains("~")) {
|
||||
String[] dates = add_time_begin.split("~");
|
||||
return dates[0];
|
||||
} // 按月统计
|
||||
else if (add_time_begin.length() == 7) {
|
||||
return add_time_begin + "-01";
|
||||
} // 按季度统计
|
||||
else if (add_time_begin.length() == 6) {
|
||||
String subYear = add_time_begin.substring(0, 4);
|
||||
if (add_time_begin.contains("-1")) {
|
||||
return subYear + "-01-01";
|
||||
} else if (add_time_begin.contains("-2")) {
|
||||
return subYear + "-04-01";
|
||||
} else if (add_time_begin.contains("-3")) {
|
||||
return subYear + "-07-01";
|
||||
} else {
|
||||
return subYear + "-10-01";
|
||||
}
|
||||
} // 按年统计
|
||||
else if (add_time_begin.length() == 4) {
|
||||
return add_time_begin + "-01-01";
|
||||
} else { // 按日统计
|
||||
return add_time_begin;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 结束日期
|
||||
public String formatEndTime(String add_time_begin) {
|
||||
// 按日期范围统计
|
||||
if (add_time_begin.length() > 20 && !add_time_begin.contains("~")) {
|
||||
return add_time_begin.substring(13) + " 23:59:59";
|
||||
}
|
||||
// 按周统计
|
||||
if (add_time_begin.contains("~")) {
|
||||
String[] dates = add_time_begin.split("~");
|
||||
return dates[1];
|
||||
}
|
||||
// 按月统计
|
||||
else if (add_time_begin.length() == 7) {
|
||||
return add_time_begin + "-31";
|
||||
}
|
||||
// 按季度统计
|
||||
else if (add_time_begin.length() == 6) {
|
||||
String subYear = add_time_begin.substring(0, 4);
|
||||
if (add_time_begin.contains("-1")) {
|
||||
return subYear + "-03-31";
|
||||
} else if (add_time_begin.contains("-2")) {
|
||||
return subYear + "-06-30";
|
||||
} else if (add_time_begin.contains("-3")) {
|
||||
return subYear + "-09-30";
|
||||
} else {
|
||||
return subYear + "-12-31";
|
||||
}
|
||||
}
|
||||
// 按年统计
|
||||
else if (add_time_begin.length() == 4) {
|
||||
return add_time_begin + "-12-31";
|
||||
} else {// 按日统计
|
||||
if (StringUtils.isEmpty(add_time_begin))
|
||||
return add_time_begin;
|
||||
return add_time_begin + " 23:59:59";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算俩个日期间隔的时间
|
||||
*
|
||||
* @param date1
|
||||
* @param date2
|
||||
* @return
|
||||
*/
|
||||
public static long getSubtractiveDays(Date date1, Date date2) {
|
||||
long nano = (Date
|
||||
.from(LocalDateTime.ofInstant(date2.toInstant(), ZoneId.systemDefault()).atZone(ZoneId.systemDefault())
|
||||
.withHour(0).withMinute(0).withSecond(0).withNano(0).toInstant())
|
||||
.getTime()
|
||||
- Date.from(LocalDateTime.ofInstant(date1.toInstant(), ZoneId.systemDefault())
|
||||
.atZone(ZoneId.systemDefault()).withHour(0).withMinute(0).withSecond(0).withNano(0).toInstant())
|
||||
.getTime());
|
||||
return nano / (1000 * 3600 * 24);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算俩个日期间隔的时间
|
||||
*
|
||||
* @param date1
|
||||
* @param date2
|
||||
* @return
|
||||
*/
|
||||
public static int getDay(Date date1, Date date2) {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(date1);
|
||||
long time1 = cal.getTimeInMillis();
|
||||
cal.setTime(date2);
|
||||
long time2 = cal.getTimeInMillis();
|
||||
long between_days = (time2 - time1) / (1000 * 3600 * 24);
|
||||
return Integer.parseInt(String.valueOf(between_days));
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算俩个日期间隔的时间
|
||||
*
|
||||
* @param date1
|
||||
* @param date2
|
||||
* @return
|
||||
*/
|
||||
public static String getDatePoor(Date endDate, Date nowDate) {
|
||||
|
||||
long nd = 1000 * 24 * 60 * 60;// 每天毫秒数
|
||||
|
||||
long nh = 1000 * 60 * 60;// 每小时毫秒数
|
||||
|
||||
long nm = 1000 * 60;// 每分钟毫秒数
|
||||
|
||||
long diff = endDate.getTime() - nowDate.getTime(); // 获得两个时间的毫秒时间差异
|
||||
|
||||
long day = diff / nd; // 计算差多少天
|
||||
|
||||
long hour = diff % nd / nh; // 计算差多少小时
|
||||
|
||||
long min = diff % nd % nh / nm; // 计算差多少分钟
|
||||
|
||||
return day + "天" + hour + "小时" + min + "分钟";
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算俩个日期间隔的时间
|
||||
*
|
||||
* @param date1 字符串日期
|
||||
* @param date2 字符串日期
|
||||
* @return
|
||||
* @throws ParseException
|
||||
*/
|
||||
public static String getDay(String date1, String date2) throws ParseException {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||
Date d1 = sdf.parse(date1);
|
||||
Date d2 = sdf.parse(date2);
|
||||
|
||||
long daysBetween = (long) ((d1.getTime() - d2.getTime()) / (60 * 60 * 24 * 1000));
|
||||
System.out.println(date1 + " 与 " + date2 + "间隔 " + daysBetween + " 天");
|
||||
return date1 + " 与 " + date2 + "间隔 " + daysBetween + " 天";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user