import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class DateUtil {
private static final Logger logger = LoggerFactory.getLogger(DateUtil.class);
public static final String FORMAT_SHORT = "yyyy-MM-dd";
public static final String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";
public static final String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";
public static final String FORMAT_MINUTE = "yyyy-MM-dd HH:mm";
public static final String FORMAT_SHORT_CN = "yyyy年MM月dd日";
public static final String FORMAT_LONG_CN = "yyyy年MM月dd日 HH时mm分ss秒";
public static final String FORMAT_MEDIUM_CN = "yyyy年MM月dd日 HH时mm分";
public static final String FORMAT_NOYEAR_CN = "MM月dd日 HH:mm";
public static final String FORMAT_FULL_CN = "yyyy年MM月dd日 HH时mm分ss秒SSS毫秒";
private static final String FORMAT_ERP = "yyyy-MM-dd HH:mm:ss";
public static final String FORMAT_ERP_TIME = "HH:mm";
public static String getDatePattern() {
return FORMAT_LONG;
}
public static String getNow() {
return format(new Date());
}
public static String getNow(String format) {
return format(new Date(), format);
}
public static final String getTodayStr() {
Date now = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
return format.format(now);
}
public static String format(Date date) {
return format(date, getDatePattern());
}
public static String format(Date date, String pattern) {
String returnValue = "";
if (date != null) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
returnValue = df.format(date);
}
return (returnValue);
}
public static Date parse(String strDate) {
return parse(strDate, getDatePattern());
}
public static boolean isValidDate(String str) {
boolean convertSuccess=true;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
format.setLenient(false);
format.parse(str);
} catch (ParseException e) {
e.printStackTrace();
convertSuccess=false;
}
return convertSuccess;
}
public static Date parse(String strDate, String pattern) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
try {
return df.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
public static Date addMonth(Date date, int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, n);
return cal.getTime();
}
public static Date addDay(Date date, int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, n);
return cal.getTime();
}
public static Date addHour(Date date, int n){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.HOUR, n);
return cal.getTime();
}
public static String getTimeString() {
SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
Calendar calendar = Calendar.getInstance();
return df.format(calendar.getTime());
}
public static String getYear(Date date) {
return format(date).substring(0, 4);
}
public static int countDays(String date) {
long t = Calendar.getInstance().getTime().getTime();
Calendar c = Calendar.getInstance();
c.setTime(parse(date));
long t1 = c.getTime().getTime();
return (int) (t / 1000 - t1 / 1000) / 3600 / 24;
}
public static int countDays(String date, String format) {
long t = Calendar.getInstance().getTime().getTime();
Calendar c = Calendar.getInstance();
c.setTime(parse(date, format));
long t1 = c.getTime().getTime();
return (int) (t / 1000 - t1 / 1000) / 3600 / 24;
}
public static boolean isToday(Date date) {
Calendar c1 = Calendar.getInstance();
c1.setTime(date);
int year1 = c1.get(Calendar.YEAR);
int month1 = c1.get(Calendar.MONTH)+1;
int day1 = c1.get(Calendar.DAY_OF_MONTH);
Calendar c2 = Calendar.getInstance();
c2.setTime(new Date());
int year2 = c2.get(Calendar.YEAR);
int month2 = c2.get(Calendar.MONTH)+1;
int day2 = c2.get(Calendar.DAY_OF_MONTH);
if(year1 == year2 && month1 == month2 && day1 == day2){
return true;
}
return false;
}
public static final Date add(Date date, int field, int amount) {
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(date);
calendar.add(field, amount);
Date retDate = calendar.getTime();
return retDate;
}
public static Calendar addMinutes(Date date, int minuteNum) {
if(date == null){
date = new Date();
}
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.MINUTE, minuteNum);
return c;
}
public static int getCompareDate(Date date, int number){
Calendar c1= Calendar.getInstance();
Calendar c2= Calendar.getInstance();
c1.setTime(date1);
if(number!=0){
c1.add(Calendar.HOUR,number);
}
int result=c1.compareTo(c2);
if(result==0){
System.out.println("c1相等c2");
return 0;
}else if(result < 0){
System.out.println("c1小于c2");
return -1;
}else{
System.out.println("c1大于c2");
return 1;
}
}
public static int compare(String s1, String s2){
DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar c1= Calendar.getInstance();
Calendar c2= Calendar.getInstance();
try
{
c1.setTime(df.parse(s1));
c2.setTime(df.parse(s2));
}catch(ParseException e){
return 2;
}
int result=c1.compareTo(c2);
if(result==0)
{
return 0;
} else if(result<0)
{
return -1;
} else
{
return 1;
}
}
public static int getBetweenDays(String checkIn, String checkOut){
long between_days = 0;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date smdate= null;
Date bdate= null;
try {
smdate = sdf.parse(checkIn);
bdate = sdf.parse(checkOut);
smdate=sdf.parse(sdf.format(smdate));
bdate=sdf.parse(sdf.format(bdate));
Calendar cal = Calendar.getInstance();
cal.setTime(smdate);
long time1 = cal.getTimeInMillis();
cal.setTime(bdate);
long time2 = cal.getTimeInMillis();
between_days=(time2-time1)/(1000*3600*24);
} catch (ParseException e) {
logger.error("计算两个日期间天数出错", e);
}
return (int)between_days;
}
public static String getWeekOfDate(Date dt) {
String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0) {
w = 0;
}
return weekDays[w];
}
public static long date2TimeStamp(String dateStr, String format) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.parse(dateStr).getTime() / 1000;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
public static String getDateWeekByTimeStamp(long timeStamp){
String[] weekOfDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
Date date = new Date(timeStamp);
Calendar instance = Calendar.getInstance();
instance.setTime(date);
int week = instance.get(Calendar.DAY_OF_WEEK) - 1;
if(week < 0){
week = 0;
}
return weekOfDays[week];
}
public static int getBetweenHours(String startDateStr, String endDateStr) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long hours = 0;
try {
Date d1 = df.parse(startDateStr);
Date d2 = df.parse(endDateStr);
long diff = d2.getTime() - d1.getTime();
hours = diff / (1000 * 60 * 60);
} catch (Exception e) {
logger.error("计算两个日期间小时数出错", e);
}
return (int)hours;
}
public static String formatChinese(Date begin,Date end){
long between = (end.getTime() - begin.getTime());
long day = between / (24 * 60 * 60 * 1000);
long hour = (between / (60 * 60 * 1000) - day * 24);
long min = ((between / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (between / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
long ms = (between - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000 - s * 1000);
StringBuilder returnStr =new StringBuilder();
if( day > 0 ){
returnStr.append(day + "天");
}
if( hour > 0 ){
returnStr.append(hour + "小时");
}
if( min > 0 ){
returnStr.append(min + "分");
}
if( s > 0 ){
returnStr.append(s + "秒");
}
if( ms > 0 ){
returnStr.append(ms + "毫秒");
}
return returnStr.toString();
}
public static String formatTask(Date begin,Date end){
long between = (end.getTime() - begin.getTime()+24*3600000-1000);
long day = between / (24 * 60 * 60 * 1000);
long hour = (between / (60 * 60 * 1000) - day * 24);
long min = ((between / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (between / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
long ms = (between - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000 - s * 1000);
StringBuilder returnStr =new StringBuilder();
if( day > 0 ){
returnStr.append(day + "天");
}
if( hour > 0 ){
returnStr.append(hour + "小时");
}
if( min > 0 ){
returnStr.append(min + "分");
}
return returnStr.toString();
}
public static void main(String[] args) throws ParseException {
SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date begin = dfs.parse("2018-05-16 19:23:21.214");
Date end = dfs.parse("2018-05-16 19:23:21.545");
System.out.println(formatChinese(begin,end));
}
}