importjava.text.DateFormat;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.Date;importjava.util.GregorianCalendar;importjava.util.HashMap;importjava.util.Map;/*** 工具类-日期处理
**/
public classDateUtil {/*** 获得当前日期
*@return
*/
public staticDate getNow() {
Calendar cal=Calendar.getInstance();
Date currDate=cal.getTime();returncurrDate;
}/*** 日期转换为字符串 格式自定义
*
*@paramdate
*@paramf
*@return
*/
public staticString dateStr(Date date, String f) {
SimpleDateFormat format= newSimpleDateFormat(f);
String str=format.format(date);returnstr;
}/*** 日期转换为字符串 MM月dd日 hh:mm
*
*@paramdate
*@return
*/
public staticString dateStr(Date date) {return dateStr(date, "MM月dd日 hh:mm");
}/*** 日期转换为字符串 yyyy-MM-dd
*
*@paramdate
*@return
*/
public staticString dateStr2(Date date) {return dateStr(date, "yyyy-MM-dd");
}/*** yyyy年MM月dd日HH时mm分ss秒
*
*@paramdate
*@return
*/
public staticString dateStr5(Date date) {return dateStr(date, "yyyy年MM月dd日 HH时mm分ss秒");
}/*** yyyyMMddHHmmss
*
*@paramdate
*@return
*/
public staticString dateStr3(Date date) {return dateStr(date, "yyyyMMddHHmmss");
}/*** yyyy-MM-dd HH:mm:ss
*
*@paramdate
*@return
*/
public staticString dateStr4(Date date) {return dateStr(date, "yyyy-MM-dd HH:mm:ss");
}/*** yyyy年MM月dd日
*
*@paramdate
*@return
*/
public staticString dateStr6(Date date) {return dateStr(date, "yyyy年MM月dd日");
}/*** yyyyMMdd
*
*@paramdate
*@return
*/
public staticString dateStr7(Date date) {return dateStr(date, "yyyyMMdd");
}/*** MM-dd
*
*@paramdate
*@return
*/
public staticString dateStr8(Date date) {return dateStr(date, "MM-dd");
}/*** MMdd
*
*@paramdate
*@return
*/
public staticString dateStr10(Date date) {return dateStr(date, "MMdd");
}/*** yyyyMM
*
*@paramdate
*@return
*/
public staticString dateStr11(Date date) {return dateStr(date, "yyyyMM");
}/*** yyyyMM
*
*@paramdate
*@return
*/
public staticString dateStr12(Date date) {return dateStr(date, "yyyy-MM-dd HH:mm:ss:SSS");
}/*** yyyyMMddHH
*
*@paramdate
*@return
*/
public staticString dateStr13(Date date) {return dateStr(date, "yyyyMMddHH");
}/*** yyyyMMddHHmm
*
*@paramdate
*@return
*/
public staticString dateStr14(Date date) {return dateStr(date, "yyyyMMddHHmm");
}/*** 将时间戳转换为Date
*
*@paramtimes
*@return
*/
public staticDate getDate(String times) {long time =Long.parseLong(times);return new Date(time * 1000);
}/*** 将时间戳转换为Date
* 10位13位时间戳都可以
*@paramtimes
*@return
*/
public staticDate getCommonDate(String times) {if(StringUtil.isNotBlank(times)&×.length()==10){
times=times+"000";
}long time =Long.parseLong(times);return newDate(time);
}public staticString dateStr(String times) {returndateStr(getDate(times));
}public staticString dateStr2(String times) {returndateStr2(getDate(times));
}public staticString dateStr3(String times) {returndateStr3(getDate(times));
}public staticString dateStr4(String times) {returndateStr4(getDate(times));
}public staticString dateStr5(String times) {returndateStr5(getDate(times));
}/*** 将Date转换为时间戳
*
*@paramdate
*@return
*/
public static longgetTime(Date date) {return date.getTime() / 1000;
}public static intgetDay(Date d) {
Calendar cal=Calendar.getInstance();
cal.setTime(d);returncal.get(Calendar.DAY_OF_MONTH);
}/*** s - 表示 "yyyy-mm-dd" 形式的日期的 String 对象
*
*@paramf
*@return
*/
public staticDate valueOf(String s) {final int YEAR_LENGTH = 4;final int MONTH_LENGTH = 2;final int DAY_LENGTH = 2;final int MAX_MONTH = 12;final int MAX_DAY = 31;intfirstDash;intsecondDash;int threeDash = 0;int fourDash = 0;
Date d= null;if (s == null) {throw newjava.lang.IllegalArgumentException();
}
firstDash= s.indexOf('-');
secondDash= s.indexOf('-', firstDash + 1);if (s.contains(":")) {
threeDash= s.indexOf(':');
fourDash= s.indexOf(':', threeDash + 1);
}if ((firstDash > 0) && (secondDash > 0) && (secondDash < s.length() - 1)) {
String yyyy= s.substring(0, firstDash);
String mm= s.substring(firstDash + 1, secondDash);
String dd= "";
String hh= "";
String MM= "";
String ss= "";if (s.contains(":")) {
dd= s.substring(secondDash + 1, threeDash - 3);
hh= s.substring(threeDash - 2, threeDash);
MM= s.substring(threeDash + 1, fourDash);
ss= s.substring(fourDash + 1);
}else{
dd= s.substring(secondDash + 1);
}if (yyyy.length() == YEAR_LENGTH && mm.length() == MONTH_LENGTH && dd.length() ==DAY_LENGTH) {int year =Integer.parseInt(yyyy);int month =Integer.parseInt(mm);int day =Integer.parseInt(dd);int hour = 0;int minute = 0;int second = 0;if (s.contains(":")) {
hour=Integer.parseInt(hh);
minute=Integer.parseInt(MM);
second=Integer.parseInt(ss);
}if (month >= 1 && month <=MAX_MONTH) {int maxDays =MAX_DAY;switch(month) {//February determine if a leap year or not
case 2:if ((year % 4 == 0 && !(year % 100 == 0)) || (year % 400 == 0)) {
maxDays= MAX_DAY - 2; //leap year so 29 days in//February
} else{
maxDays= MAX_DAY - 3; //not a leap year so 28 days//in February
}break;//April, June, Sept, Nov 30 day months
case 4:case 6:case 9:case 11:
maxDays= MAX_DAY - 1;break;
}if (day >= 1 && day <=maxDays) {
Calendar cal=Calendar.getInstance();
cal.set(year, month- 1, day, hour, minute, second);
cal.set(Calendar.MILLISECOND,0);
d=cal.getTime();
}
}
}
}if (d == null) {throw newjava.lang.IllegalArgumentException();
}returnd;
}/***@authorlijie
*@paramBegin
*@paramend 传入开始时间 和 结束时间 格式如:2012-09-07
*@return返回Map 获取相隔多少年 get("YEAR")及为俩个时间年只差,月 天,类推 Key : YEAR MONTH DAY 如果开始时间 晚于 结束时间 return null;*/@SuppressWarnings({"rawtypes", "unchecked"})public staticMap getApartTime(String Begin, String end) {
String[] temp= Begin.split("-");
String[] temp2= end.split("-");if (temp.length > 1 && temp2.length > 1) {
Calendar ends=Calendar.getInstance();
Calendar begin=Calendar.getInstance();
begin.set(StringUtil.toInt(temp[0]), StringUtil.toInt(temp[1]), StringUtil.toInt(temp[2]));
ends.set(StringUtil.toInt(temp2[0]), StringUtil.toInt(temp2[1]), StringUtil.toInt(temp2[2]));if (begin.compareTo(ends) < 0) {
Map map= newHashMap();
ends.add(Calendar.YEAR,-StringUtil.toInt(temp[0]));
ends.add(Calendar.MONTH,-StringUtil.toInt(temp[1]));
ends.add(Calendar.DATE,-StringUtil.toInt(temp[2]));
map.put("YEAR", ends.get(Calendar.YEAR));
map.put("MONTH", ends.get(Calendar.MONTH) + 1);
map.put("DAY", ends.get(Calendar.DATE));returnmap;
}
}return null;
}/*** 前/后?分钟
*
*@paramd
*@paramminute
*@return
*/
public static Date rollMinute(Date d, intminute) {return new Date(d.getTime() + minute * 60 * 1000);
}/*** 前/后?天
*
*@paramd
*@paramday
*@return
*/
public static Date rollDay(Date d, intday) {
Calendar cal=Calendar.getInstance();
cal.setTime(d);
cal.add(Calendar.DAY_OF_MONTH, day);returncal.getTime();
}/*** 前/后?月
*
*@paramd
*@parammon
*@return
*/
public static Date rollMon(Date d, intmon) {
Calendar cal=Calendar.getInstance();
cal.setTime(d);
cal.add(Calendar.MONTH, mon);returncal.getTime();
}/*** 前/后?年
*
*@paramd
*@paramyear
*@return
*/
public static Date rollYear(Date d, intyear) {
Calendar cal=Calendar.getInstance();
cal.setTime(d);
cal.add(Calendar.YEAR, year);returncal.getTime();
}public static Date rollDate(Date d, int year, int mon, intday) {
Calendar cal=Calendar.getInstance();
cal.setTime(d);
cal.add(Calendar.YEAR, year);
cal.add(Calendar.MONTH, mon);
cal.add(Calendar.DAY_OF_MONTH, day);returncal.getTime();
}/*** 获取当前时间-时间戳字符串
*
*@return
*/
public staticString getNowTimeStr() {
String str= Long.toString(System.currentTimeMillis() / 1000);returnstr;
}/*** 获取当前时间-时间戳
*
*@return
*/
public static intgetNowTime() {return Integer.parseInt(StringUtil.isNull(System.currentTimeMillis() / 1000));
}/*** 将Date转换为时间戳
*
*@paramtime
*@return
*/
public staticString getTimeStr(Date time) {long date =time.getTime();
String str= Long.toString(date / 1000);returnstr;
}public staticString getTimeStr(String dateStr, String format) {
SimpleDateFormat sdf= newSimpleDateFormat(format);
Date date;try{
date=sdf.parse(dateStr);
}catch(ParseException e) {
e.printStackTrace();return "";
}
String str=DateUtil.getTimeStr(date);returnstr;
}public staticString rollMonth(Date addTime, String time_limit) {
Date t= DateUtil.rollDate(addTime, 0, StringUtil.toInt(time_limit), 0);return t.getTime() / 1000 + "";
}public staticString rollDay(Date addTime, String time_limit_day) {
Date t= DateUtil.rollDate(addTime, 0, 0, StringUtil.toInt(time_limit_day));return t.getTime() / 1000 + "";
}public staticDate getIntegralTime() {
Calendar cal=Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY,0);
cal.set(Calendar.SECOND,0);
cal.set(Calendar.MINUTE,0);
cal.set(Calendar.MILLISECOND,0);returncal.getTime();
}public staticDate getLastIntegralTime() {
Calendar cal=Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY,23);
cal.set(Calendar.SECOND,59);
cal.set(Calendar.MINUTE,59);
cal.set(Calendar.MILLISECOND,0);returncal.getTime();
}public staticDate getLastSecIntegralTime(Date d) {
Calendar cal=Calendar.getInstance();
cal.setTimeInMillis(d.getTime());
cal.set(Calendar.HOUR_OF_DAY,23);
cal.set(Calendar.SECOND,59);
cal.set(Calendar.MINUTE,59);
cal.set(Calendar.MILLISECOND,0);returncal.getTime();
}public static longgetTime(String format) {long t = 0;if(StringUtil.isBlank(format))returnt;
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date;try{
date=sdf.parse(format);
t= date.getTime() / 1000;
}catch(ParseException e) {
e.printStackTrace();
}returnt;
}//获取本周日的日期
@SuppressWarnings("unused")public staticString getCurrentWeekday() {int weeks = 0;int mondayPlus =DateUtil.getMondayPlus();
GregorianCalendar currentDate= newGregorianCalendar();
currentDate.add(GregorianCalendar.DATE, mondayPlus+ 6);
Date monday=currentDate.getTime();
DateFormat df=DateFormat.getDateInstance();
String preMonday=df.format(monday);returnpreMonday;
}//获得当前日期与本周日相差的天数
private static intgetMondayPlus() {
Calendar cd=Calendar.getInstance();//获得今天是一周的第几天,星期日是第一天,星期二是第二天......
int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; //因为按中国礼拜一作为第一天所以这里减1
if (dayOfWeek == 1) {return 0;
}else{return 1 -dayOfWeek;
}
}//获得本周一的日期
@SuppressWarnings("unused")public staticString getMondayOFWeek() {int weeks = 0;int mondayPlus =DateUtil.getMondayPlus();
GregorianCalendar currentDate= newGregorianCalendar();
currentDate.add(GregorianCalendar.DATE, mondayPlus);
Date monday=currentDate.getTime();
DateFormat df=DateFormat.getDateInstance();
String preMonday=df.format(monday);returnpreMonday;
}//获取当前月第一天:
public staticString getFirstDayOfMonth() {
SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd");
Calendar c=Calendar.getInstance();
c.add(Calendar.MONTH,0);
c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
String first =format.format(c.getTime());returnfirst;
}//获取当月最后一天
public staticString getLastDayOfMonth() {
SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd");
Calendar ca=Calendar.getInstance();
ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
String last=format.format(ca.getTime());returnlast;
}//获取下月第一天:
public staticString getFirstDayOfNextMonth() {
SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd");
Calendar c=Calendar.getInstance();
c.add(Calendar.MONTH,1);
c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
String first =format.format(c.getTime());returnfirst;
}/*** 日期月份处理
*
*@paramd 时间
*@parammonth 相加的月份,正数则加,负数则减
*@return
*/
public static Date timeMonthManage(Date d, intmonth) {
Calendar rightNow=Calendar.getInstance();
rightNow.setTime(d);
rightNow.add(Calendar.MONTH, month);returnrightNow.getTime();
}/*** 获取指定年月的最后一天
*
*@paramyear_time 指定年
*@parammonth_time 指定月
*@return
*/
public static Date monthLastDay(int year_time, intmonth_time) {
Calendar cal=Calendar.getInstance();
cal.set(year_time, month_time,0, 23, 59, 59);returncal.getTime();
}/*** 获取指定年月的第一天
*
*@paramyear_time 指定年
*@parammonth_time 指定月
*@return
*/
public static Date monthFirstDay(int year_time, intmonth_time) {
Calendar cal=Calendar.getInstance();
cal.set(year_time, month_time- 1, 1, 0, 0, 0);returncal.getTime();
}/*** 获取指定时间月的第一天
*
*@paramd 指定时间,为空代表当前时间
*@return
*/
public staticDate currMonthFirstDay(Date d) {
Calendar cal=Calendar.getInstance();if (d != null)
cal.setTime(d);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE),0, 0, 0);returncal.getTime();
}/*** 获取指定时间月的最后一天
*
*@paramd 指定时间,为空代表当前时间
*@return
*/
public staticDate currMonthLastDay(Date d) {
Calendar cal=Calendar.getInstance();if (d != null)
cal.setTime(d);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE),23, 59, 59);returncal.getTime();
}/*** 获取指定时间的年
*
*@paramdate 指定时间
*@return
*/
public static intgetTimeYear(Date date) {if (date == null)
date= newDate();
Calendar cal=Calendar.getInstance();
cal.setTime(date);returncal.get(Calendar.YEAR);
}/*** 获取指定时间的月
*
*@paramdate 指定时间
*@return
*/
public static intgetTimeMonth(Date date) {if (date == null)
date= newDate();
Calendar cal=Calendar.getInstance();
cal.setTime(date);return cal.get(Calendar.MONTH) + 1;
}/*** 获取指定时间的天
*
*@paramdate 指定时间
*@return
*/
public static intgetTimeDay(Date date) {if (date == null)
date= newDate();
Calendar cal=Calendar.getInstance();
cal.setTime(date);returncal.get(Calendar.DATE);
}public staticDate getFirstSecIntegralTime(Date d) {
Calendar cal=Calendar.getInstance();
cal.setTimeInMillis(d.getTime());
cal.set(Calendar.HOUR_OF_DAY,0);
cal.set(Calendar.SECOND,0);
cal.set(Calendar.MINUTE,0);
cal.set(Calendar.MILLISECOND,0);
cal.set(Calendar.DATE,0);returncal.getTime();
}/*** 获取指定时间天的结束时间
*
*@paramd
*@return
*/
public static Date getDayEndTime(longd) {
Date day= null;if (d <= 0){
day= newDate();
}else{
day= new Date(d * 1000);
}
Calendar cal=Calendar.getInstance();if (day != null){
cal.setTimeInMillis(day.getTime());
}
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE),23, 59, 59);returncal.getTime();
}/*** 获取指定时间天的开始时间
*
*@paramd
*@return
*/
public static Date getDayStartTime(longd) {
Date day= null;if (d <= 0){
day= newDate();
}else{
day= new Date(d * 1000);
}
Calendar cal=Calendar.getInstance();if (day != null) {
cal.setTimeInMillis(day.getTime());
}
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE),0, 0, 0);returncal.getTime();
}/*** 获取19位的格式时间
*
*@paramdateStr
*@return*@throwsParseException*/
public staticDate getDateByFullDateStr(String dateStr) {if(StringUtil.isBlank(dateStr)) {return null;
}try{
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");returnsdf.parse(dateStr);
}catch(Exception e) {
e.printStackTrace();return null;
}
}/*** 计算两个日期之间相差的天数
*@paramdate1
*@paramdate2
*@returndate1>date2时结果<0, date1=date2时结果=0, date2>date1时结果>0*/
public static intdaysBetween(Date date1, Date date2){
DateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Calendar cal=Calendar.getInstance();try{
Date d1=sdf.parse(DateUtil.dateStr7(date1));
Date d2=sdf.parse(DateUtil.dateStr7(date2));
cal.setTime(d1);long time1 =cal.getTimeInMillis();
cal.setTime(d2);long time2 =cal.getTimeInMillis();return Integer.parseInt(String.valueOf((time2 - time1) / 86400000L));
}catch(ParseException e) {
e.printStackTrace();
}return 0;
}/*** 计算两个日期之间相差的小时数
*@paramdate1
*@paramdate2
*@return
*/
public static inthoursBetween(Date date1, Date date2) {
DateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Calendar cal=Calendar.getInstance();try{
Date d1=sdf.parse(DateUtil.dateStr7(date1));
Date d2=sdf.parse(DateUtil.dateStr7(date2));
cal.setTime(d1);long time1 =cal.getTimeInMillis();
cal.setTime(d2);long time2 =cal.getTimeInMillis();return Integer.parseInt(String.valueOf((time2 - time1) / 3600000L));
}catch(ParseException e) {
e.printStackTrace();
}return 0;
}/*** 计算两个日期之间相差的小时数
*@paramdate1
*@paramdate2
*@returndate1>date2时结果<0, date1=date2时结果=0, date2>date1时结果>0*/
public static inthoursBetweenByDateStr(Date date1, Date date2) {
DateFormat sdf=new SimpleDateFormat("yyyyMMddHH");
Calendar cal=Calendar.getInstance();try{
Date d1=sdf.parse(DateUtil.dateStr13(date1));
Date d2=sdf.parse(DateUtil.dateStr13(date2));
cal.setTime(d1);long time1 =cal.getTimeInMillis();
cal.setTime(d2);long time2 =cal.getTimeInMillis();return Integer.parseInt(String.valueOf(((time2 - time1) / 3600000L)));
}catch(ParseException e) {
e.printStackTrace();
}return 0;
}/*** 计算两个日期之间相差的分钟数
*@paramdate1
*@paramdate2
*@returndate1>date2时结果<0, date1=date2时结果=0, date2>date1时结果>0*/
public static intminuteBetweenByDateStr(Date date1, Date date2) {
DateFormat sdf=new SimpleDateFormat("yyyyMMddHHmm");
Calendar cal=Calendar.getInstance();try{
Date d1=sdf.parse(DateUtil.dateStr14(date1));
Date d2=sdf.parse(DateUtil.dateStr14(date2));
cal.setTime(d1);long time1 =cal.getTimeInMillis();
cal.setTime(d2);long time2 =cal.getTimeInMillis();return Integer.parseInt(String.valueOf(((time2 - time1) / 60000L)));
}catch(ParseException e) {
e.printStackTrace();
}return 0;
}/*** 计算两个日期之间相差的秒数
*@paramdate1
*@paramdate2
*@returndate1>date2时结果<0, date1=date2时结果=0, date2>date1时结果>0*/
public static intsecondBetweenByDateStr(Date date1, Date date2) {
DateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
Calendar cal=Calendar.getInstance();try{
Date d1=sdf.parse(DateUtil.dateStr3(date1));
Date d2=sdf.parse(DateUtil.dateStr3(date2));
cal.setTime(d1);long time1 =cal.getTimeInMillis();
cal.setTime(d2);long time2 =cal.getTimeInMillis();return Integer.parseInt(String.valueOf(((time2 - time1) / 1000L)));
}catch(ParseException e) {
e.printStackTrace();
}return 0;
}/*** 得到两个时间相差的 天,小时,分钟,秒
*
*@paramstartTime
*@paramendTime
*@return
*/
public static MapgetTimeDifference(Date startTime,
Date endTime) {if (startTime == null || endTime == null) {return null;
}long nd = 1000 * 24 * 60 * 60;//一天的毫秒数
long nh = 1000 * 60 * 60; //一小时的毫秒数
long nm = 1000 * 60; //一分钟的毫秒数
long ns = 1000; //一秒钟的毫秒数
longdiff;
diff= endTime.getTime() -startTime.getTime();long day = diff / nd; //天
long hour = diff % nd / nh; //小时
long min = diff % nd % nh / nm; //分钟
long sec = diff % nd % nh % nm / ns;//秒
Map map = new HashMap();
map.put("day", day);
map.put("hour", hour);
map.put("min", min);
map.put("sec", sec);returnmap;
}public staticDate getStartTimeToday(){
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:00:00");try{returnsdf.parse(sdf.format(DateUtil.getNow()));
}catch(ParseException e) {
e.printStackTrace();
}return null;
}/*** yyMMddHHmm
*
*@paramdate
*@return
*/
public staticString dateStr9(Date date) {return dateStr(date, "yyMMddHHmm");
}public staticString getStartTimeOneDayStr(Date date){
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd 00:00:00");returnsdf.format(date);
}public staticDate getStartTimeByDate(Date date){
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd 00:00:00");try{returnsdf.parse(sdf.format(date));
}catch(ParseException e) {
e.printStackTrace();
}return null;
}public staticDate getDateByStrTime(String dateStr,String format) {
SimpleDateFormat sdf= newSimpleDateFormat(format);try{returnsdf.parse(dateStr);
}catch(ParseException e) {
e.printStackTrace();
}return null;
}public static intsecondsBetween(Date startTime,Date endTime){
System.out.println("当前时间:"+dateStr4(startTime));
System.out.println("当天结束时间:"+dateStr4(endTime));long diff=endTime.getTime()-startTime.getTime();long diffSeconds = diff / 1000 % 60;long diffMinutes = diff / (60 * 1000) % 60;long diffHours = diff / (60 * 60 * 1000) % 24;long diffDays = diff / (24 * 60 * 60 * 1000);
System.out.println("两个时间相差:");
System.out.println(diffDays+ " 天, ");
System.out.println(diffHours+ " 小时, ");
System.out.println(diffMinutes+ " 分钟, ");
System.out.println(diffSeconds+ " 秒.");int totalSecons= Integer.valueOf(String.valueOf(diff/1000));
System.out.println("两个时间相差总秒数:"+totalSecons);returntotalSecons;
}public static voidmain(String[] args) {//Double rewardFee=BigDecimalUtil.sub(1, 0.2);//
//System.out.println("正常比列:" +rewardFee);//
//Double taxMoney=BigDecimalUtil.roundup(BigDecimalUtil.div((36800-1000.00),rewardFee ),0);//
//System.out.println("扣税部分的总额:" +taxMoney);//
//System.out.println("总额:" +BigDecimalUtil.add(taxMoney,1000));//
//if(secondBetweenByDateStr(DateUtil.getDateByStrTime("20180110135900","yyyyMMddHHmmss"),DateUtil.getNow())>=0){//System.out.println(secondBetweenByDateStr(DateUtil.getDateByStrTime("20180110135900","yyyyMMddHHmmss"),DateUtil.getNow()));//System.out.println("活动已开始" );//}else{//System.out.println("活动未开始:" );//}//
//
//Date time=DateUtil.getNow();//if(DateUtil.daysBetween(DateUtil.getDateByStrTime("20170606","yyyyMMdd"), time) <0){//System.out.println("6号之前" );//}else{//System.out.println("6号之后" );//}//Date date=getXinLiCaiAutoInvestTime(DateUtil.getDateByStrTime("20160229","yyyyMMdd"));//
//System.out.println("返回日期:"+dateStr(date, "yyyyMMdd") );//System.out.println(dateStr9(new Date()));//int value=Integer.valueOf(1608221702);//System.out.println("intValue: "+value);//System.out.println(DateUtil.getStartTimeOneDayStr(DateUtil.rollDay(DateUtil.getNow(), -1)));//
//System.out.println(DateUtil.getStartTimeOneDayStr(DateUtil.rollDay(DateUtil.getNow(), 1)));//
//System.out.println(DateUtil.dateStr2(DateUtil.rollDay(DateUtil.getNow(), -1)));//
//System.out.println(DateUtil.dateStr2(DateUtil.getNow()));//System.out.println(DateUtil.getStartTimeOneDayStr(DateUtil.rollDay(DateUtil.getNow(), 0)));//
//
//System.out.print(DateUtil.getTime(DateUtil.getNow())+"\n");//System.out.print(System.currentTimeMillis());//System.out.println("当前时间相差"+DateUtil.daysBetween(DateUtil.getDateByStrTime("20161008","yyyyMMdd"), DateUtil.getDateByStrTime("20161031","yyyyMMdd")));//Date closeTime1 = DateUtil.getDateByFullDateStr(DateUtil.dateStr2(new Date())+" "+"00:06:00");//Date closeTime2 = DateUtil.getDateByFullDateStr(DateUtil.dateStr2(new Date())+" "+"00:06:00");//
//System.out.println("closeTime1 : "+DateUtil.dateStr4(closeTime1));//System.out.println("closeTime2 : "+DateUtil.dateStr4(closeTime2));//
//if(DateUtil.getNow().compareTo(closeTime1)>=0&&DateUtil.getNow().compareTo(closeTime2)<=0){//System.out.println("当前时间为冻结时间不接受任何交易!");//}else{//System.out.println(DateUtil.dateStr4(DateUtil.getNow()));//}//
//
//
//int days=DateUtil.daysBetween(DateUtil.getDateByStrTime("20161117000000","yyyyMMddHHmmss"), DateUtil.getNow());//
//System.out.println("当前相差天数为 :"+days);//
//
//System.out.println("当前时间比较方法1为 : " + compare_date_1(DateUtil.dateStr2(new Date())+" "+"00:06:00", "2016-11-17 00:00:19"));//
//System.out.println("当前时间比较方法1为 : " + compare_date_2(DateUtil.dateStr2(new Date())+" "+"00:06:00", "2016-11-17 00:00:19"));//
//System.out.println(DateUtil.rollDay(DateUtil.getNow(), -1));//
//System.out.println(DateUtil.getDateByStrTime("20161123", "yyyyMMdd").compareTo(DateUtil.getDateByStrTime("20161123", "yyyyMMdd")));//
//
//int abc=DateUtil.daysBetween(DateUtil.getDateByStrTime("20170429","yyyyMMdd"), DateUtil.getDateByStrTime("20170429","yyyyMMdd"));//System.out.println("abc:"+abc);//
//System.out.println("明天起始时间 : "+DateUtil.getStartTimeByDate(DateUtil.rollDay(DateUtil.getNow(), 1)));//
//System.out.println("明天起始时间Str:"+DateUtil.getStartTimeOneDayStr(DateUtil.rollDay(DateUtil.getNow(), 1)));//System.out.println(getLastMonthLastDay());//
//System.out.println(dateStr4(new Date(1506575450093L)));//
//System.out.println(DateUtil.rollMon(DateUtil.getNow(), -6).getTime());//
//int abc=DateUtil.daysBetween(DateUtil.getDateByStrTime("2017-09-07 04:30:06","yyyy-MM-dd HH:mm:ss"), DateUtil.getDateByStrTime("2017-09-28 18:00:00","yyyy-MM-dd HH:mm:ss"));//System.out.println("abc:"+abc);//
//
//int bcd = DateUtil.compare_date_2("2017-09-07 04:30:06","2017-09-28 18:00:00");//
//System.out.println("bcd"+bcd);//
//System.out.println("时间 : "+System.currentTimeMillis());
}/*** 给当前时间加一天
*@paramdate1
*@return
*/
public staticDate getAddOneDay(Date d) {
Calendar cal= newGregorianCalendar();
cal.setTime(d);
cal.add(Calendar.DATE,1);returncal.getTime();
}/*** 给当前时间加一年
*@paramdate1
*@return
*/
public staticDate getAddOneYear(Date d) {
Calendar cal= newGregorianCalendar();
cal.setTime(d);
cal.add(Calendar.YEAR,1);returncal.getTime();
}/*** 比较时间
*@paramstrDate1 时间1
*@paramstrDate2 时间2
*@return0:相等, <0:strDate1小于strDate2, >0:strDate1大于strDate2*/
public static intcompare_date_1(String strDate1, String strDate2) {int result=0;
DateFormat df= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");try{
java.util.Calendar c1=java.util.Calendar.getInstance();
java.util.Calendar c2=java.util.Calendar.getInstance();
c1.setTime(df.parse(strDate1));
c2.setTime(df.parse(strDate2));
result=c1.compareTo(c2);if(result==0){
System.out.println("c1相等c2");
}else if(result<0){
System.out.println("c1小于c2");
}else{
System.out.println("c1大于c2");
}
}catch(Exception exception) {
exception.printStackTrace();
}returnresult;
}/*** 比较时间
*@paramstrDate1 时间1
*@paramstrDate2 时间2
*@return0:相等, <0:strDate1小于strDate2, >0:strDate1大于strDate2*/
public static intcompare_date_2(String strDate1, String strDate2) {int result=0;
DateFormat df= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");try{
Date dt1=df.parse(strDate1);
Date dt2=df.parse(strDate2);if(dt1.getTime()>dt2.getTime()){
result=1;
}else if(dt1.getTime()
result=-1;
}else{
result=0;
}
}catch(Exception exception) {
exception.printStackTrace();
}returnresult;
}/*** 获取薪理财自动转入的时间
*@paramdate 最后一期转入时间
*@return
*/
public staticDate getXinLiCaiAutoInvestTime(Date date) {
Calendar cal= newGregorianCalendar();
cal.setTime(date);int year=Calendar.YEAR; //年
int month=Calendar.MONTH; //月
int day=Calendar.DATE; //天
Date returnDate=null;if(month==1 || month==3 || month==5 || month==8 || month==10){if(month==1){if(day>=28){if((year%4==0&&year%100!=0)||year%400==0){
returnDate=getDateByStrTime(dateStr(date,"yyyyMM")+29, "yyyyMMdd") ;
}else{
returnDate=getDateByStrTime(dateStr(date,"yyyyMM")+28, "yyyyMMdd") ;
}
}
}else{if(day>30){
returnDate=getDateByStrTime(dateStr(date,"yyyyMM")+30, "yyyyMMdd") ;
}else{
returnDate= rollMon(date, 1);
}
}
}else{
returnDate= rollMon(date, 1);
}returnreturnDate;
}/*** 获取上一个月的第一天
*@return
*/
public staticString getLastMonthFirstDay(){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.MONTH,-1);
calendar.set(Calendar.DAY_OF_MONTH,1);return sdf.format(calendar.getTime())+" 00:00:00";
}/*** 获取上一个月的最后一天
*@return
*/
public staticString getLastMonthLastDay(){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar=Calendar.getInstance();int month=calendar.get(Calendar.MONTH);
calendar.set(Calendar.MONTH, month-1);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));return sdf.format(calendar.getTime())+" 23:59:59";
}/*** 比较两个时间是否为同一天
*@paramdate1
*@paramdate2
*@return
*/
public static booleanisSameDay(Date date1, Date date2) {
Calendar calendar=Calendar.getInstance();
calendar.setTime(date1);int year1 =calendar.get(Calendar.YEAR);int day1 =calendar.get(Calendar.DAY_OF_YEAR);
calendar.setTime(date2);int year2 =calendar.get(Calendar.YEAR);int day2 =calendar.get(Calendar.DAY_OF_YEAR);if ((year1 == year2) && (day1 ==day2)) {return true;
}return false;
}public staticDate getDateByDateAndDateFormate(Date date,String dateFormat){
SimpleDateFormat sdf= newSimpleDateFormat(dateFormat);try{returnsdf.parse(sdf.format(date));
}catch(ParseException e) {
e.printStackTrace();
}return null;
}public staticDate MongoDbTimeToDate(Object mongodbTime) {
SimpleDateFormat format1= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Date returnD=null;try{if (mongodbTime instanceofDate) {
Date d=(Date) mongodbTime;
returnD=d;
}else{
returnD=format1.parse(mongodbTime.toString());
}
}catch(ParseException e) {try{
returnD=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(mongodbTime.toString());
}catch(ParseException e1) {try{
returnD= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(mongodbTime.toString());
}catch(ParseException e2) {
e2.printStackTrace();
}
}
}returnreturnD;
}/*** 字符串日期转日期格式
*@paramdateStr
*@return
*/
public staticDate dateStrToDate(String dateStr, String dateFormat) {if(StringUtil.isBlank(dateStr)) {return null;
}try{
SimpleDateFormat sdf= newSimpleDateFormat(dateFormat);returnsdf.parse(dateStr);
}catch(Exception e) {
e.printStackTrace();return null;
}
}/*** 获取指定日期的结束时间
*@paramdate
*@return
*/
public staticString getEndTimeOneDayStr(Date date){
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd 23:59:59");returnsdf.format(date);
}/*** 获取19位的格式时间
*
*@paramdateStr
*@return*@throwsParseException*/
public staticDate getDateByYMDDateStr(String dateStr) {if(StringUtil.isBlank(dateStr)) {return null;
}try{
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");returnsdf.parse(dateStr);
}catch(Exception e) {
e.printStackTrace();return null;
}
}public static longgetMilliseconds(String format) {long t = 0;if(StringUtil.isBlank(format))returnt;
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date;try{
date=sdf.parse(format);
t=date.getTime();
}catch(ParseException e) {
e.printStackTrace();
}returnt;
}
}