import org.apache.commons.collections.CollectionUtils;
import org.yaml.snakeyaml.Yaml;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class DateUtils {
public static final String PATTERN_SPECIA = "MM/dd/yyyy";
public static final String PATTERN_COMMONLY_All = "yyyy-MM-dd HH:mm:ss";
public static final String PATTERN_COMMONLY_NONE = "yyyy-MM-dd";
private static final String CRON_DATE_FORMAT = "ss mm HH dd MM ? yyyy";
public static final String PATTERN_COMMONLY_NUMBER = "yyyyMMdd";
public static final String PATTERN_COMMONLY_NUMBER_YU = "yyyyMMddHHmmss";
public static final String PATTERN_COMMONLY_NUMBER_POS = "YYYYMMDDHHMMSS";
public static String getCron(final Date date) {
SimpleDateFormat sdf = new SimpleDateFormat(CRON_DATE_FORMAT);
String formatTimeStr = "";
if (date != null) {
formatTimeStr = sdf.format(date);
}
return formatTimeStr;
}
public static String format(Long times){
StringBuilder builder = new StringBuilder();
if(times!=null){
long hours = times/(60*60*1000);
if(hours>0){
builder.append(hours).append("h");
}
long min = (times%(60*60*1000))/(60*1000);
long sec = ((times%(60*60*1000))%(60*1000))/1000;
if(min>0){
builder.append(min).append("mins");
}else{
if(sec>0 && hours>0){
builder.append(min).append("mins");
}
}
if(sec>0){
builder.append(sec).append("s");
}
}
return builder.toString();
}
public static String format(Date date, String pattern) {
if (date == null || pattern == null) {
return null;
}
SimpleDateFormat dt = new SimpleDateFormat(pattern);
return dt.format(date);
}
public static Date parse(String date, String pattern) {
if (date == null || pattern == null) {
return null;
}
Date resultDate = null;
try {
resultDate = new SimpleDateFormat(pattern).parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return resultDate;
}
public static Date parse(String date) {
String[] datas = date.split("/");
String newData = datas[2]+"-"+datas[0]+"-"+datas[1]+" 00:00:00";
Date d = null;
try {
d = new SimpleDateFormat(PATTERN_COMMONLY_All).parse(newData);
} catch (ParseException e) {
e.printStackTrace();
}
return d;
}
public static Date change(Date date){
SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_COMMONLY_NONE);
String s=sdf.format(date);
try {
date = sdf.parse(s);
} catch (ParseException e) {
date = getToday();
}
return date;
}
public static Date getYesterday(){
Date yesterday = getToday();
Calendar cal = Calendar.getInstance();
cal.setTime(yesterday);
cal.add(Calendar.DATE,-1);
yesterday = cal.getTime();
return yesterday;
}
public static Date getYesterday(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE,-1);
date = cal.getTime();
return date;
}
public static Date getTomorrow(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE,1);
date = cal.getTime();
return date;
}
public static List<Date> getThisWeek() {
List<Date> listDate = new ArrayList<Date>();
Calendar cal = Calendar.getInstance();
int weekday = cal.get(8);
cal.add(5,-weekday);
listDate.add(change(cal.getTime()));
cal.add(5,6);
listDate.add(change(cal.getTime()));
return listDate;
}
public static List<Date> getLastWeek() {
List<Date> listDate = new ArrayList<Date>();
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.add(Calendar.DATE, -1*7);
cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
listDate.add(change(cal.getTime()));
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
listDate.add(change(cal.getTime()));
return listDate;
}
public static List<Date> getThisMonth() {
List<Date> listDate = new ArrayList<Date>();
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, 0);
cal.set(Calendar.DAY_OF_MONTH,1);
listDate.add(change(cal.getTime()));
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
listDate.add(change(cal.getTime()));
return listDate;
}
public static List<Date> getLastMonth() {
List<Date> listDate = new ArrayList<Date>();
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1);
cal.set(Calendar.DAY_OF_MONTH, 1);
listDate.add(change(cal.getTime()));
cal.add(Calendar.MONTH, 1);
cal.add(Calendar.DATE, -1);
listDate.add(change(cal.getTime()));
return listDate;
}
public static List<Date> getTargetMonth(int month) {
List<Date> listDate = new ArrayList<Date>();
Calendar cal = Calendar.getInstance();
if(month!=0){
month--;
}
cal.set(Calendar.MONTH,month);
cal.set(Calendar.DAY_OF_MONTH, 1);
listDate.add(change(cal.getTime()));
cal.add(Calendar.MONTH, 1);
cal.add(Calendar.DATE, -1);
listDate.add(change(cal.getTime()));
return listDate;
}
public static Date getSevenDay() {
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, - 6);
Date monday = c.getTime();
return change(monday);
}
public static Date getExpiredHour(Date givenDate, int offset) {
return getExpiredDate(givenDate, offset, Calendar.HOUR);
}
public static Date addOneSecond(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.SECOND, 1);
return calendar.getTime();
}
public static Date getExpiredDate(Date givenDate, int offset, int type) {
Calendar cal = Calendar.getInstance();
cal.setTime(givenDate);
cal.add(type, offset);
return cal.getTime();
}
public static Date getExpiredDay(Date givenDate, int offset) {
return getExpiredDate(givenDate, offset, Calendar.DAY_OF_MONTH);
}
public static Date getToday(){
TimeZone timeZoneSH = TimeZone.getTimeZone("Asia/Shanghai");
SimpleDateFormat outputFormat = new SimpleDateFormat(PATTERN_COMMONLY_All, Locale.CHINA);
outputFormat.setTimeZone(timeZoneSH);
Date date = new Date(System.currentTimeMillis());
String time = outputFormat.format(date);
Date date1 = parse(time, PATTERN_COMMONLY_All);
return date1;
}
public static Date parseToDate(String source) {
if (source == null) {
return null;
}
SimpleDateFormat format = new SimpleDateFormat(PATTERN_COMMONLY_All);
try {
return format.parse(source);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
public static Date parseTodateByTimeStamp(String timeStamp){
SimpleDateFormat sdf=new SimpleDateFormat(PATTERN_COMMONLY_All);
String sd = sdf.format(new Date(Long.parseLong(timeStamp+"000")));
return parseToDate(sd);
}
public static Long parseTimeStampByDate(Date dateTime){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String format = simpleDateFormat.format(dateTime);
try {
Date parse = simpleDateFormat.parse(format);
return parse.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static String getStringToday() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
String dateString = formatter.format(currentTime);
return dateString;
}
public static String getWeekOfDate(Date date) {
String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0){
w = 0;
}
return weekDays[w];
}
public static int betweenDays(Long start,Long end){
Long times=end-start;
Long days = times/(1000*60*60*24)+1;
return days.intValue();
}
public static int betweenHours(Long start,Long end){
Long times=end-start;
Long days = times/(1000*60*60);
Long less = times%(1000*60*60);
if(less>0){
return days.intValue()+1;
}
return days.intValue();
}
public static List<String> getBetweenDate(String begin,String end){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
List<String> betweenList = new ArrayList<String>();
try{
if(format.parse(begin).getTime()>format.parse(end).getTime()){
return null;
}
end = format.format(format.parse(end));
Calendar startDay = Calendar.getInstance();
startDay.setTime(format.parse(begin));
startDay.add(Calendar.DATE, -1);
while(true){
startDay.add(Calendar.DATE, 1);
Date newDate = startDay.getTime();
String newend=format.format(newDate);
betweenList.add(newend);
if(end.equals(newend)){
break;
}
}
}catch (Exception e) {
e.printStackTrace();
}
return betweenList;
}
public static List<String> getBetweenDate(Date begin,Date end){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String time1 = format.format(begin);
String time2 = format.format(end);
return getBetweenDate(time1,time2);
}
public static int betweenDays(Date start,Date end) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date time1 = format.parse(format.format(start));
Date time2 = format.parse(format.format(end));
return betweenDays(time1.getTime(), time2.getTime());
}
public static boolean betweenDays(String[] time1,String[] time2) throws ParseException {
if(time1.length>0 && time1.length==time2.length){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
for (int i = 0; i < time1.length; i++) {
List<String> betweenDate = getBetweenDate(time1[i], time2[i]);
if(CollectionUtils.isNotEmpty(betweenDate)){
for (int j = i+1; j < time1.length; j++) {
String startTime = time1[j];
String endTime = time2[j];
for (String s : betweenDate) {
if(format.parse(s).getTime()==format.parse(startTime).getTime()){
return true;
}
if(format.parse(s).getTime()==format.parse(endTime).getTime()){
return true;
}
}
}
}else{
return false;
}
}
}
return false;
}
public static String dayComparePrecise(Date fromDate,Date toDate){
if(fromDate!=null && toDate!=null){
StringBuilder builder = new StringBuilder();
Calendar from = Calendar.getInstance();
from.setTime(fromDate);
Calendar to = Calendar.getInstance();
to.setTime(toDate);
int fromYear = from.get(Calendar.YEAR);
int fromMonth = from.get(Calendar.MONTH);
int fromDay = from.get(Calendar.DAY_OF_MONTH);
int toYear = to.get(Calendar.YEAR);
int toMonth = to.get(Calendar.MONTH);
int toDay = to.get(Calendar.DAY_OF_MONTH);
int year = toYear - fromYear;
int month = toMonth - fromMonth;
int day = toDay - fromDay;
if(day<0){
month = month-1;
}
if(year>0){
if(month<0){
year = year-1;
month = 12+month;
}
builder.append(year).append("年").append(month).append("个月");
}else{
builder.append(month).append("个月");
}
return builder.toString();
}
return null;
}
public static String dayComparePrecise(String time){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
if(time==null){
return null;
}
try {
Date parse = format.parse(time);
return dayComparePrecise(parse,new Date());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static Integer yearComparePrecise(Date fromDate,Date toDate){
if(fromDate!=null && toDate!=null){
Calendar from = Calendar.getInstance();
from.setTime(fromDate);
Calendar to = Calendar.getInstance();
to.setTime(toDate);
int fromYear = from.get(Calendar.YEAR);
int fromMonth = from.get(Calendar.MONTH);
int fromDay = from.get(Calendar.DAY_OF_MONTH);
int toYear = to.get(Calendar.YEAR);
int toMonth = to.get(Calendar.MONTH);
int toDay = to.get(Calendar.DAY_OF_MONTH);
int year = toYear - fromYear;
int month = toMonth - fromMonth;
int day = toDay - fromDay;
if(day<0){
month = month-1;
}
if(month<0){
year = year-1;
}
return year;
}
return null;
}
public static Integer yearComparePrecise(String time){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
if(time==null){
return null;
}
try {
Date parse = format.parse(time);
return yearComparePrecise(parse,new Date());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}