datedif函数计算年月,恋爱日期计算器

importjava.sql.Timestamp; 导入Java.text.date format; 导入Java.text.parse exception; 导入Java.text.simple date format; importjava.util.Calendar; importjava.util.Date; importorg.Apache.com mons.lang3. time.date format utils; /***类名: DateUtil

*班级说明:时间操作工具

*@version

*/

公共类日期实用工具{/* * *生成ISO-8601规范的时间格式

*@paramdate

*@return

*/

publicstaticstringformatiso 8601 datestring {

string pattern=’ yyyy-mm-DD ‘ t ‘ hh : mm : ss.sssz ‘; returndateformatutils.format (date,pattern );

}/***获取反向时间戳

*@return

*/

publicstaticlonggetcurrentreversetime ((longlong time=system.current time millis ) *1000000calculateutil.getnext ) ) 9000000

}/***获取原始时间戳

*@paramreverseTime

*@return

*/

publicstaticlongrecoverreversetime (longreversetime ) longlong time=long.max _ value-reverse time; 返回长时间/1000000;

(}/***生成页面的普通展示时间

*@paramdate

*@return

*/

publicstaticstringformatnormaldatestring {

string pattern=’ yyyy-mm-ddhh : mm : ss ‘; returndateformatutils.format (date,pattern );

}/***日期转换为字符串yyyy-MM-dd

*

*@paramdate

*@return

*/

publicstaticstringgetshotdate (datedate ) if ) date==null ) {return ‘ );

}返回数据(date,’ yyyy-MM-dd ‘;

}/***将日期转换为字符串格式进行自定义

*

*@paramdate

*@paramf

*@return

*/

publicstaticstringdatestr (datedate,String f ) if ) date==null ) {return ‘ );

}

简单数据格式=newsimpledateformatformat (f;

stringstr=format.format (日期); 返回str;

}/***获取当前日期

*@return

*/

公共静态数据获取新

calendar cal=calendar.getinstance (;

Date currDate=cal.getTime (; returncurrDate;

}/***准确获取当前日期至毫秒

*@return

*/

publicstatictimestampgetnowinmillis (

timestamptimestamep=new timestamp (get now ().getTime ) ); returntimeStamep;

} publicstaticdategetdatebystrtime (字符串数据,字符串格式) )

simpledateformatsdf=newsimpledateformat; try{returnSDF.parse(datestr );

}catch(parseexceptione ) {

e .打印堆栈跟踪(;

}返回空值;

(/**

* 计算两个日期之间相差的天数

*@paramdate1

*@paramdate2

*@returndate1>date2时结果<0, date1=date2时结果=0, date2>date1时结果>0*/

public static intdaysBetween(Date date1, Date date2, String format){

DateFormat sdf=newSimpleDateFormat(format);

Calendar cal=Calendar.getInstance();try{

Date d1=sdf.parse(DateUtil.dateStr(date1, format));

Date d2=sdf.parse(DateUtil.dateStr(date2, format));

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.dateStr(date1, “yyyyMMdd”));

Date d2= sdf.parse(DateUtil.dateStr(date2, “yyyyMMdd”));

cal.setTime(d1);

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;

}/*** 计算两个日期相差的时间*/

public static voidgetBetweenDate () {

String dateStart= “2017111520”;

String dateStop= “2017111620”;

SimpleDateFormat format= new SimpleDateFormat(“yyyyMMddHH”);

Date d1= null;

Date d2= null;try{

d1=format.parse(dateStart);

d2=format.parse(dateStop);//毫秒ms

long diff = d2.getTime() -d1.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.print(“两个时间相差:”);

System.out.print(diffDays+ ” 天, “);

System.out.print(diffHours+ ” 小时, “);

System.out.print(diffMinutes+ ” 分钟, “);

System.out.print(diffSeconds+ ” 秒.”);

}catch(Exception e) {

e.printStackTrace();

}

}/*** 计算两个日期之间相差的小时数

*@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.dateStr(date1,”yyyyMMddHH”));

Date d2= sdf.parse(DateUtil.dateStr(date2,”yyyyMMddHH”));

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.dateStr(date1,”yyyyMMddHHmm”));

Date d2= sdf.parse(DateUtil.dateStr(date2,”yyyyMMddHHmm”));

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.dateStr(date1, “yyyyMMddHHmmss”));

Date d2= sdf.parse(DateUtil.dateStr(date2, “yyyyMMddHHmmss”));

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;

}public static voidmain(String[] args) {

System.out.println(hoursBetweenByDateStr(getDateByStrTime(“2018010223”, “yyyyMMddHH”), getDateByStrTime(“2018010323”, “yyyyMMddHH”)));

System.out.println(minuteBetweenByDateStr(getDateByStrTime(“201801052359”, “yyyyMMddHHmm”), getDateByStrTime(“201801060000”, “yyyyMMddHHmm”)));//2.活动结束时间>0 ( 默认2018年1月5号 20:30结束)

if( DateUtil.minuteBetweenByDateStr (DateUtil.getDateByStrTime(“201801111348″,”yyyyMMddHHmm”), DateUtil.getNow()) > 0){

System.out.println(DateUtil.minuteBetweenByDateStr (DateUtil.getDateByStrTime(“201801111348″,”yyyyMMddHHmm”), DateUtil.getNow()));

}else{

System.out.println(“时间未结束”);

}

System.out.println(DateUtil.secondBetweenByDateStr(DateUtil.getDateByStrTime(“20180110150200″,”yyyyMMddHHmmss”), DateUtil.getNow()));

}

}

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注