package com.funi.demoprojact.utils;
import com.google.common.base.Strings;
import org.springframework.util.StringUtils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* Created by Administrator on 2017/12/2 0002.
*/
public abstract class DateUtils {
private static final String DATE_FORMAT = "yyyy-MM-dd";
private static final String DATE_FORMAT_All = "yyyyMMddHHmmss";
private static ThreadLocal<DateFormat> threadLocal = new ThreadLocal<DateFormat>();
/**
* 年月日
* @return
*/
public static DateFormat getDateFormat() {
DateFormat df = threadLocal.get();
if(df==null){
df = new SimpleDateFormat(DATE_FORMAT);
threadLocal.set(df);
}
return df;
}
/**
* date转string
* @param date
* @return
*/
public static String formatDate(Date date) {
if(null==date){
return "";
}
return getDateFormat().format(date);
}
/**
* string转date
* @param strDate
* @return
*/
public static Date parse(String strDate) {
if(Strings.isNullOrEmpty(strDate)){
return null;
}
try{
return getDateFormat().parse(strDate);
}catch (ParseException e){
e.printStackTrace();
return null;
}
}
/**
* 年月日时分秒
* @return
*/
public static DateFormat getDateFormatAll() {
DateFormat df = threadLocal.get();
if(df==null){
df = new SimpleDateFormat(DATE_FORMAT_All);
threadLocal.set(df);
}
return df;
}
/**
* 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+1) ;
}
else //同一年
{
System.out.println("判断day2 - day1 : " + (day2-day1+1));
return day2-day1+1;
}
}
public static String formatDateAll(Date date) {
if(null==date){
return "";
}
return getDateFormatAll().format(date);
}
public static Date parseAll(String strDate) {
if(Strings.isNullOrEmpty(strDate)){
return null;
}
try{
return getDateFormatAll().parse(strDate);
}catch (ParseException e){
e.printStackTrace();
return null;
}
}
public static boolean dateBetweenSE(Date start,Date current,Date end){
if(null==start || null==current || null==end){
return false;
}
if(start.compareTo(current)<=0 && end.compareTo(current)>=0){
return true;
}
return false;
}
public static void main(String[] args)throws Exception{
/*Date nowDate = new Date();
String startDate = "2017-07-31 00:00:00";
String endDate = "2017-08-06 12:00:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date start = sdf.parse(startDate);
Date end = sdf.parse(endDate);
boolean isBetween = dateBetweenSE(start,nowDate,end);
System.err.println(isBetween);*/
String lotteryTime = "2017-07-31";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
if (!StringUtils.isEmpty(lotteryTime)) {
try {
long now = new Date().getTime();
long date = sdf.parse(lotteryTime).getTime()+ 24*60*60*1000;
if (date < now) {
throw new Exception("已过摇号日期,不允许再进行核验、审核、注销操作!");
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
public static String getStringDate() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
return dateString;
}
}
日期工具类
最新推荐文章于 2022-07-12 18:29:20 发布