标签:java
package com.datedaycha; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException; /* * java 判断两个时间相差的天数 1、实现目标 输入:两个日期 输出:两个日期相差的天数 2、代码实现 方法1: 通过Calendar类的日期比较。注意:这里需要考虑一下: 日期是跨年份的,如一个是2012年,一个是2015年的 年份是分闰年和平年的,各自的天数不同 */ public class date_day_cha1 { /**方法1 * date2比date1多的天数 * @param date1 * @param date2 * @return */ public static int differentDays_one(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); //加个时间日期大小 判断 管他谁大谁小 int diff ; if(day1<day2) { diff = day2 - day1; } else { diff = day1 - day2; } 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) ; return timeDistance + diff ; } else //同一年 { // System.out.println("判断day2 - day1 : " + (day2-day1)); // return day2-day1; System.out.println("判断day2 - day1 : " + diff); return diff; } } /* 直接通过计算两个日期的毫秒数,他们的差除以一天的毫秒数,即可得到我们想要的两个日期相差的天数。*/ /** * 通过时间秒毫秒数判断两个时间的间隔 * @param date1 * @param date2 * @return */ public static int differentDaysByMillisecond(Date date1,Date date2) { //加个时间日期大小 判断 管他谁大谁小 long time1 = date1.getTime(); long time2 = date2.getTime(); long diff ; if(time1<time2) { diff = time2 - time1; } else { diff = time1 - time2; } //int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24)); int days = (int) (diff /(1000*3600*24)); return days; } //测试 public static void main(String[] args) throws Exception { String dateStr = "2017-1-1 0:0:0"; String dateStr2 = "2017-9-14 10:57:01"; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date date2 = format.parse(dateStr2); Date date = format.parse(dateStr); System.out.println("两个日期的差距 方法1:" + differentDays_one(date,date2)); System.out.println("两个日期的差距 方法2:" + differentDaysByMillisecond(date,date2)); Date d=new Date(); System.out.println(d.getTime()); System.out.println("距离今天-两个日期的差距 方法1:" + differentDays_one(date,d)); System.out.println("距离今天-两个日期的差距 方法2:" + differentDaysByMillisecond(d,date)); } catch (ParseException e) { e.printStackTrace(); } } } 方法2:
package com.datedaycha; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException; /** * 时间相距 * @author LC * @version 1.0 * @date 2017-9-14 10:59:04 */ public class date_day_cha2 { /** * 两个时间之间相差距离多少天 * @param one 时间参数 1: * @param two 时间参数 2: * @return 相差天数 */ public static long getDistanceDays(String str1, String str2) throws Exception{ DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date one; Date two; long days=0; try { one = df.parse(str1); two = df.parse(str2); long time1 = one.getTime(); long time2 = two.getTime(); long diff ; if(time1<time2) { diff = time2 - time1; } else { diff = time1 - time2; } days = diff / (1000 * 60 * 60 * 24); } catch (ParseException e) { e.printStackTrace(); } return days; } /** * 两个时间相差距离多少天多少小时多少分多少秒 * @param str1 时间参数 1 格式:1990-01-01 12:00:00 * @param str2 时间参数 2 格式:2009-01-01 12:00:00 * @return long[] 返回值为:{天, 时, 分, 秒} * @throws Exception */ public static long[] getDistanceTimes(String str1, String str2) throws Exception { DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date one; Date two; long day = 0; long hour = 0; long min = 0; long sec = 0; try { one = df.parse(str1); two = df.parse(str2); long time1 = one.getTime(); long time2 = two.getTime(); long diff ; if(time1<time2) { diff = time2 - time1; } else { diff = time1 - time2; } day = diff / (24 * 60 * 60 * 1000); hour = (diff / (60 * 60 * 1000) - day * 24); min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60); sec = (diff/1000-day*24*60*60-hour*60*60-min*60); } catch (ParseException e) { e.printStackTrace(); } long[] times = {day, hour, min, sec}; return times; } /** * 两个时间相差距离多少天多少小时多少分多少秒 * @param str1 时间参数 1 格式:1990-01-01 12:00:00 * @param str2 时间参数 2 格式:2009-01-01 12:00:00 * @return String 返回值为:xx天xx小时xx分xx秒 * @throws Exception */ public static String getDistanceTime(String str1, String str2) throws Exception { DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date one; Date two; long day = 0; long hour = 0; long min = 0; long sec = 0; try { one = df.parse(str1); two = df.parse(str2); long time1 = one.getTime(); long time2 = two.getTime(); long diff ; if(time1<time2) { diff = time2 - time1; } else { diff = time1 - time2; } day = diff / (24 * 60 * 60 * 1000); hour = (diff / (60 * 60 * 1000) - day * 24); min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60); sec = (diff/1000-day*24*60*60-hour*60*60-min*60); } catch (ParseException e) { e.printStackTrace(); } return day + "天" + hour + "小时" + min + "分" + sec + "秒"; } //测试 public static void main(String[] args) throws Exception { String dateStr = "2008-1-1 1:21:28"; String dateStr2 = "2017-9-14 10:57:01"; Long days=getDistanceDays(dateStr, dateStr2); long[] longdays2; longdays2=getDistanceTimes(dateStr, dateStr2); String stringday3= getDistanceTime(dateStr, dateStr2); System.out.println("返回值return:相差多少天:"+days); System.out.println("返回值return:long[] 返回值为:{天, 时, 分, 秒}----:"+longdays2[0]+"天"+longdays2[1]+"小时"+longdays2[2]+"分"+longdays2[3]+"秒"); System.out.println("返回值return:String 返回值为:xx天xx小时xx分xx秒 -----:"+stringday3); } }
标签:java
原文地址:http://ahviplc.blog.51cto.com/9599731/1967074