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.
228 lines
5.3 KiB
228 lines
5.3 KiB
3 years ago
|
package com.fr.plugin.ztsso.utils;
|
||
|
|
||
|
import java.sql.Timestamp;
|
||
|
import java.text.SimpleDateFormat;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.Calendar;
|
||
|
import java.util.Date;
|
||
|
import java.util.List;
|
||
|
|
||
|
public class DateUtilSelf {
|
||
|
|
||
|
/**
|
||
|
* 日期转换为日期字符串
|
||
|
* @param date
|
||
|
* @param formatStr
|
||
|
* @return String
|
||
|
*/
|
||
|
public static String DateToString(Date date,String formatStr) {
|
||
|
SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
|
||
|
String dateStr = sdf.format(date).toString();
|
||
|
|
||
|
return dateStr;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 日期字符串转换日期
|
||
|
* @param dateStr
|
||
|
* @param formatStr
|
||
|
* @return Date
|
||
|
*/
|
||
|
public static Date strToDate(String dateStr,String formatStr){
|
||
|
SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
|
||
|
Date date = null;
|
||
|
|
||
|
try {
|
||
|
date = sdf.parse(dateStr);
|
||
|
}
|
||
|
catch(Exception e) {
|
||
|
}
|
||
|
|
||
|
return date;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Date转Timestamp
|
||
|
* @param date
|
||
|
* @return
|
||
|
*/
|
||
|
public static Timestamp dateToTimestamp(Date date) {
|
||
|
Date transDate = DateUtilSelf.strToDate(DateUtilSelf.DateToString(date, "yyyy-MM-dd hh:mm:ss"),"yyyy-MM-dd hh:mm:ss");
|
||
|
|
||
|
Timestamp timestamp = new Timestamp(transDate.getTime());
|
||
|
return timestamp;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Date字符串转Timestamp
|
||
|
* @param dateStr
|
||
|
* @param format
|
||
|
* @return
|
||
|
*/
|
||
|
public static Timestamp strToTimestamp(String dateStr,String format) {
|
||
|
Date date = strToDate(dateStr,format);
|
||
|
Timestamp timestamp = new Timestamp(date.getTime());
|
||
|
return timestamp;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取两个日期字符串之间的天数
|
||
|
* @param startDateStr
|
||
|
* @param endDateStr
|
||
|
* @param formatStr
|
||
|
* @return
|
||
|
*/
|
||
|
public static int getDays(String startDateStr,String endDateStr,String formatStr) {
|
||
|
|
||
|
Date startDate = strToDate(startDateStr,formatStr);
|
||
|
Date endDate = strToDate(endDateStr,formatStr);
|
||
|
|
||
|
long startTime = startDate.getTime();
|
||
|
long endTime = endDate.getTime();
|
||
|
|
||
|
int days = (int) ((endTime - startTime)/(60*60*24*1000));
|
||
|
|
||
|
return days;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取给定时间之前之后的时间
|
||
|
* @param type
|
||
|
* @param dateStr
|
||
|
* @param count
|
||
|
* @param formatStr
|
||
|
* @return
|
||
|
*/
|
||
|
public static String getAfterDateStr(int type,String dateStr,int count,String formatStr) {
|
||
|
Date startDate = strToDate(dateStr,formatStr);
|
||
|
|
||
|
Calendar calendar = Calendar.getInstance();
|
||
|
calendar.setTime(startDate);
|
||
|
|
||
|
calendar.add(type, count);
|
||
|
|
||
|
String endDateStr = DateToString(calendar.getTime(),formatStr);
|
||
|
|
||
|
return endDateStr;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取给定时间之前之后的时间
|
||
|
* @param type
|
||
|
* @param date
|
||
|
* @param count
|
||
|
* @param formatStr
|
||
|
* @return
|
||
|
*/
|
||
|
public static String getAfterDateStr(int type,Date date,int count,String formatStr) {
|
||
|
|
||
|
Calendar calendar = Calendar.getInstance();
|
||
|
calendar.setTime(date);
|
||
|
|
||
|
calendar.add(type, count);
|
||
|
|
||
|
String endDateStr = DateToString(calendar.getTime(),formatStr);
|
||
|
|
||
|
return endDateStr;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取给定时间之前之后的时间
|
||
|
* @param type
|
||
|
* @param date
|
||
|
* @param count
|
||
|
* @return
|
||
|
*/
|
||
|
public static Date getAfterDateStr(int type,Date date,int count) {
|
||
|
Calendar dateResult = Calendar.getInstance();
|
||
|
dateResult.setTime(date);
|
||
|
dateResult.add(type, count);
|
||
|
return dateResult.getTime();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 时间戳转日期
|
||
|
* @param timestamp
|
||
|
* @return
|
||
|
*/
|
||
|
public static Date timestampToDate(Timestamp timestamp) {
|
||
|
Date date = new Date(timestamp.getTime());
|
||
|
|
||
|
return date;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 时间戳转时间字符串
|
||
|
* @param timestamp
|
||
|
* @param format 日期格式
|
||
|
* @return
|
||
|
*/
|
||
|
public static String timestampToStr(Timestamp timestamp,String format) {
|
||
|
Date date = timestampToDate(timestamp);
|
||
|
|
||
|
String timeStr = DateToString(date, format);
|
||
|
|
||
|
return timeStr;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取所给日期length天内每一天的日期
|
||
|
* @param date 所给日期(yyyy-MM-dd)
|
||
|
* @param length 长度
|
||
|
* @return
|
||
|
*/
|
||
|
public static List<String> getDateList(String date,int length){
|
||
|
List<String> dateList = new ArrayList<String>();
|
||
|
String format = "yyyy-MM-dd";
|
||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||
|
//获取length天后的日期
|
||
|
|
||
|
String targetDate = getAfterDateStr(Calendar.DATE,date,length,format);
|
||
|
|
||
|
Date start = null;
|
||
|
Date end = null;
|
||
|
|
||
|
if(length >= 0) {
|
||
|
start = strToDate(date,format);
|
||
|
end = strToDate(targetDate,format);
|
||
|
}else {
|
||
|
start = strToDate(targetDate,format);
|
||
|
end = strToDate(date,format);
|
||
|
}
|
||
|
|
||
|
Calendar calBegin = Calendar.getInstance();
|
||
|
calBegin.setTime(start);
|
||
|
Calendar calEnd = Calendar.getInstance();
|
||
|
calEnd.setTime(end);
|
||
|
|
||
|
while (end.after(calBegin.getTime())) {
|
||
|
calBegin.add(Calendar.DATE, 1);
|
||
|
String dayStr = sdf.format(calBegin.getTime());
|
||
|
dateList.add(dayStr);
|
||
|
}
|
||
|
|
||
|
return dateList;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 比较startDate是否在endDate之前
|
||
|
* @param startDate
|
||
|
* @param endDate
|
||
|
* @param format
|
||
|
* @return 0 两个日期相等 <0 开始日期在结束日期之前 >0 开始日期在结束日期之后
|
||
|
*/
|
||
|
public static int comparisonDate(String startDate,String endDate,String format) {
|
||
|
Date start = strToDate(startDate,format);
|
||
|
Date end = strToDate(endDate,format);
|
||
|
|
||
|
return start.compareTo(end);
|
||
|
}
|
||
|
|
||
|
//获取当前日期年、月、日、时、分、秒
|
||
|
public static int getCount(int type){
|
||
|
Calendar calendar = Calendar.getInstance();
|
||
|
|
||
|
return calendar.get(type);
|
||
|
}
|
||
|
}
|