You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
320 lines
9.2 KiB
320 lines
9.2 KiB
|
|
|
|
package com.xujie.sys.common.utils;
|
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.joda.time.DateTime;
|
|
import org.joda.time.LocalDate;
|
|
import org.joda.time.format.DateTimeFormat;
|
|
import org.joda.time.format.DateTimeFormatter;
|
|
|
|
import java.text.ParseException;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Calendar;
|
|
import java.util.Date;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
|
|
/**
|
|
* 日期处理
|
|
*
|
|
*
|
|
*/
|
|
public class DateUtils {
|
|
/** 时间格式(yyyy-MM-dd) */
|
|
public final static String DATE_PATTERN = "yyyy-MM-dd";
|
|
/** 时间格式(yyyy-MM-dd HH:mm:ss) */
|
|
public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
|
|
|
|
/**
|
|
* 日期格式化 日期格式为:yyyy-MM-dd
|
|
* @param date 日期
|
|
* @return 返回yyyy-MM-dd格式日期
|
|
*/
|
|
public static String format(Date date) {
|
|
return format(date, DATE_PATTERN);
|
|
}
|
|
|
|
/**
|
|
* 日期格式化 日期格式为:yyyy-MM-dd
|
|
* @param date 日期
|
|
* @param pattern 格式,如:DateUtils.DATE_TIME_PATTERN
|
|
* @return 返回yyyy-MM-dd格式日期
|
|
*/
|
|
public static String format(Date date, String pattern) {
|
|
if(date != null){
|
|
SimpleDateFormat df = new SimpleDateFormat(pattern);
|
|
return df.format(date);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* 字符串转换成日期
|
|
* @param strDate 日期字符串
|
|
* @param pattern 日期的格式,如:DateUtils.DATE_TIME_PATTERN
|
|
*/
|
|
public static Date stringToDate(String strDate, String pattern) {
|
|
if (StringUtils.isBlank(strDate)){
|
|
return null;
|
|
}
|
|
|
|
DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
|
|
return fmt.parseLocalDateTime(strDate).toDate();
|
|
}
|
|
|
|
/**
|
|
* 根据周数,获取开始日期、结束日期
|
|
* @param week 周期 0本周,-1上周,-2上上周,1下周,2下下周
|
|
* @return 返回date[0]开始日期、date[1]结束日期
|
|
*/
|
|
public static Date[] getWeekStartAndEnd(int week) {
|
|
DateTime dateTime = new DateTime();
|
|
LocalDate date = new LocalDate(dateTime.plusWeeks(week));
|
|
|
|
date = date.dayOfWeek().withMinimumValue();
|
|
Date beginDate = date.toDate();
|
|
Date endDate = date.plusDays(6).toDate();
|
|
return new Date[]{beginDate, endDate};
|
|
}
|
|
|
|
/**
|
|
* 对日期的【秒】进行加/减
|
|
*
|
|
* @param date 日期
|
|
* @param seconds 秒数,负数为减
|
|
* @return 加/减几秒后的日期
|
|
*/
|
|
public static Date addDateSeconds(Date date, int seconds) {
|
|
DateTime dateTime = new DateTime(date);
|
|
return dateTime.plusSeconds(seconds).toDate();
|
|
}
|
|
|
|
/**
|
|
* 对日期的【分钟】进行加/减
|
|
*
|
|
* @param date 日期
|
|
* @param minutes 分钟数,负数为减
|
|
* @return 加/减几分钟后的日期
|
|
*/
|
|
public static Date addDateMinutes(Date date, int minutes) {
|
|
DateTime dateTime = new DateTime(date);
|
|
return dateTime.plusMinutes(minutes).toDate();
|
|
}
|
|
|
|
/**
|
|
* 对日期的【小时】进行加/减
|
|
*
|
|
* @param date 日期
|
|
* @param hours 小时数,负数为减
|
|
* @return 加/减几小时后的日期
|
|
*/
|
|
public static Date addDateHours(Date date, int hours) {
|
|
DateTime dateTime = new DateTime(date);
|
|
return dateTime.plusHours(hours).toDate();
|
|
}
|
|
|
|
/**
|
|
* 对日期的【天】进行加/减
|
|
*
|
|
* @param date 日期
|
|
* @param days 天数,负数为减
|
|
* @return 加/减几天后的日期
|
|
*/
|
|
public static Date addDateDays(Date date, int days) {
|
|
DateTime dateTime = new DateTime(date);
|
|
return dateTime.plusDays(days).toDate();
|
|
}
|
|
|
|
/**
|
|
* 对日期的【周】进行加/减
|
|
*
|
|
* @param date 日期
|
|
* @param weeks 周数,负数为减
|
|
* @return 加/减几周后的日期
|
|
*/
|
|
public static Date addDateWeeks(Date date, int weeks) {
|
|
DateTime dateTime = new DateTime(date);
|
|
return dateTime.plusWeeks(weeks).toDate();
|
|
}
|
|
|
|
/**
|
|
* 对日期的【月】进行加/减
|
|
*
|
|
* @param date 日期
|
|
* @param months 月数,负数为减
|
|
* @return 加/减几月后的日期
|
|
*/
|
|
public static Date addDateMonths(Date date, int months) {
|
|
DateTime dateTime = new DateTime(date);
|
|
return dateTime.plusMonths(months).toDate();
|
|
}
|
|
|
|
/**
|
|
* 对日期的【年】进行加/减
|
|
*
|
|
* @param date 日期
|
|
* @param years 年数,负数为减
|
|
* @return 加/减几年后的日期
|
|
*/
|
|
public static Date addDateYears(Date date, int years) {
|
|
DateTime dateTime = new DateTime(date);
|
|
return dateTime.plusYears(years).toDate();
|
|
}
|
|
|
|
// /**
|
|
// * @Author sxm
|
|
// * @Description 两个日期相差年份
|
|
// * @Date 2022/4/12 14:47
|
|
// * @Param
|
|
// * @return
|
|
// **/
|
|
// public static int differentDays(Date date1,Date date2)
|
|
// {
|
|
// Calendar cal1 = Calendar.getInstance();
|
|
// cal1.setTime(date1);
|
|
//
|
|
// Calendar cal2 = Calendar.getInstance();
|
|
// cal2.setTime(date2);
|
|
// int day1= cal1.get(Calendar.DAY_OF_YEAR);
|
|
// int day2 = cal2.get(Calendar.DAY_OF_YEAR);
|
|
//
|
|
// int year1 = cal1.get(Calendar.YEAR);
|
|
// int year2 = cal2.get(Calendar.YEAR);
|
|
// if(year1 != year2) //同一年
|
|
// {
|
|
// int timeDistance = 0 ;
|
|
// for(int i = year1 ; i < year2 ; i ++)
|
|
// {
|
|
// if(i%4==0 && i%100!=0 || i%400==0) //闰年
|
|
// {
|
|
// timeDistance += 366;
|
|
// }
|
|
// else //不是闰年
|
|
// {
|
|
// timeDistance += 365;
|
|
// }
|
|
// }
|
|
//
|
|
// return timeDistance + (day2-day1) ;
|
|
// }
|
|
// else { // 不同年
|
|
// return day2-day1;
|
|
// }
|
|
// }
|
|
|
|
public static String getStringNow() {
|
|
SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_PATTERN);
|
|
return sdf.format(new Date());
|
|
}
|
|
|
|
public static Date getStringToDate(String time,String format) {
|
|
SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
try {
|
|
return sdf.parse(time);
|
|
} catch (ParseException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* date2比date1多的天数
|
|
* @param date1
|
|
* @param date2
|
|
* @return
|
|
*/
|
|
public static int differentDays (Date date1, Date date2) {
|
|
Calendar cal1 = Calendar.getInstance();
|
|
cal1.setTime(date1);
|
|
Calendar cal2 = Calendar.getInstance();
|
|
cal2.setTime(date2);
|
|
int day1= cal1.get(Calendar.DAY_OF_YEAR);
|
|
int day2 = cal2.get(Calendar.DAY_OF_YEAR);
|
|
|
|
int year1 = cal1.get(Calendar.YEAR);
|
|
int year2 = cal2.get(Calendar.YEAR);
|
|
if(year1 != year2) { // 同一年
|
|
int timeDistance = 0 ;
|
|
for(int i = year1; i < year2; i ++) {
|
|
if(i % 4 == 0 && i % 100 != 0 || i % 400 == 0) { // 闰年
|
|
timeDistance += 366;
|
|
} else { // 不是闰年
|
|
timeDistance += 365;
|
|
}
|
|
}
|
|
return timeDistance + (day2-day1);
|
|
} else { // 不同年
|
|
return day2 - day1;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 比较两个日期的大小:
|
|
* 2 发生异常
|
|
* 1 firstDate>secondDate
|
|
* 0 firstDate==secondDate
|
|
* -1 firstDate<secondDate
|
|
* @param format
|
|
* @param firstDate
|
|
* @param secondDate
|
|
* @return
|
|
*/
|
|
public static int compareDate(String format,String firstDate, String secondDate){
|
|
SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
int result = 2;
|
|
try {
|
|
Date fDate = sdf.parse(firstDate);
|
|
Date sDate = sdf.parse(secondDate);
|
|
|
|
if(fDate.getTime()>sDate.getTime()){
|
|
result = 1;
|
|
}else if(fDate.getTime()==sDate.getTime()){
|
|
result = 0;
|
|
}else{
|
|
result = -1;
|
|
}
|
|
} catch (ParseException ex) {
|
|
Logger.getLogger(DateUtil.class.getName()).log(Level.SEVERE, null, ex);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static String getStringDate(Date date) {
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
return sdf.format(date);
|
|
}
|
|
|
|
public static Date getDateDate(Date currentDate) throws ParseException {
|
|
|
|
|
|
// 使用 SimpleDateFormat 格式化日期
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
String formattedDate = sdf.format(currentDate);
|
|
|
|
// 将格式化的字符串再转换回 Date 对象
|
|
Date dateWithoutTime = sdf.parse(formattedDate);
|
|
return dateWithoutTime;
|
|
}
|
|
|
|
|
|
public static void main(String[] args) {
|
|
try {
|
|
try {
|
|
getDateByParten("777","yyyy-MM-dd");
|
|
}catch (ParseException e){
|
|
throw new RuntimeException("格式有误!");
|
|
}
|
|
}catch (Exception e){
|
|
System.out.println(e.getMessage()+"66666");
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public static Date getDateByParten(String date, String parten)
|
|
throws ParseException {
|
|
SimpleDateFormat sdf = new SimpleDateFormat(parten);
|
|
return sdf.parse(date);
|
|
}
|
|
}
|