java语言编程实现两个时间相差多少天、多少小时、多少分、多少秒-阿里云开发者社区

开发者社区> 开发与运维> 正文
登录阅读全文

java语言编程实现两个时间相差多少天、多少小时、多少分、多少秒

简介:

 

 

 

 

DateDistance.java

复制代码
package zhouls.bigdata.DataFeatureSelection.test;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 两个时间差计算
 */
public class DateDistance {
    private static String distanceTime;
    public static void main(String[] args) throws Exception {
        String startday =  "2017-09-20";     
        String endday ="2017-09-28";    
        long DistanceDays = getDistanceDays(startday,endday);//两个时间之间相差距离多少天 
        System.out.println(DistanceDays);
        
        String starttimes =  "2017-01-17 00:10:20";     
        String endtimes ="2017-01-18 00:10:21";
        long[] DistanceTimes = getDistanceTimes(starttimes,endtimes);//两个时间相差距离多少天多少小时多少分多少秒 ,以long[]形式返回
        for (int i = 0; i < DistanceTimes.length; i++) {
            System.out.println(DistanceTimes[i]);
        }
        
        String DistanceTime = getDistanceTime(starttimes,endtimes);//两个时间相差距离多少天多少小时多少分多少秒 ,以String形式返回
        System.out.println(DistanceTime);
    }
        
    
    
    /** 
     * 两个时间之间相差距离多少天 
     * @param one 时间参数 1: 
     * @param two 时间参数 2: 
     * @return 相差天数 
     */ 
        public static long getDistanceDays(String starttime, String endtime) throws Exception{  
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");  
            Date one;  
            Date two;  
            long days=0;  
            try {  
                one = df.parse(starttime);  
                two = df.parse(endtime);  
                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[] 返回值为:{天, 时, 分, 秒} 
         */  
        public static long[] getDistanceTimes(String starttime, String endtime) {  
            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(starttime);  
                two = df.parse(endtime);  
                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秒 
         */  
        public static String getDistanceTime(String starttime, String endtime) {  
            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(starttime);  
                two = df.parse(endtime);  
                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 + "";  
        }  
}
复制代码

 

 

 

 

 

 

  当然,我们可以进一步,将其放到一个时间的工具类去。

 

java编程如何实现多条2017-08-08 22:10:00.0这样的时间数据,相差多少天?(隔24小时为相差1天,否则为0天)

 

java编程如何实现多条2017-01-16 22:28:11.0这样的时间数据,转换成Date类型Mon Jan 16 22:28:11 CST 2017这样的时间数据

 

java编程如何实现2017-01-16 22:28:26.0这样的时间数据,转换成2017:01:16:22:28:26这样的时间数据

 

 

 

 

 

 

  同时,这里大家也可以更改格式

 

 

DateDistance.java

复制代码
package zhouls.bigdata.DataFeatureSelection.test;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 两个时间差计算
 */
public class DateDistance {
    private static String distanceTime;
    public static void main(String[] args) throws Exception {
        String startday =  "2017:09:20";     
        String endday ="2017:09:28";    
        long DistanceDays = getDistanceDays(startday,endday);//两个时间之间相差距离多少天 
        System.out.println(DistanceDays);
        
        String starttimes =  "2017:01:17 00:10:20";     
        String endtimes ="2017:01:18 00:10:21";
        long[] DistanceTimes = getDistanceTimes(starttimes,endtimes);//两个时间相差距离多少天多少小时多少分多少秒 ,以long[]形式返回
        for (int i = 0; i < DistanceTimes.length; i++) {
            System.out.println(DistanceTimes[i]);
        }
        
        String DistanceTime = getDistanceTime(starttimes,endtimes);//两个时间相差距离多少天多少小时多少分多少秒 ,以String形式返回
        System.out.println(DistanceTime);
    }
        
    
    
    /** 
     * 两个时间之间相差距离多少天 
     * @param one 时间参数 1: 
     * @param two 时间参数 2: 
     * @return 相差天数 
     */ 
        public static long getDistanceDays(String starttime, String endtime) throws Exception{  
            DateFormat df = new SimpleDateFormat("yyyy:MM:dd");  
            Date one;  
            Date two;  
            long days=0;  
            try {  
                one = df.parse(starttime);  
                two = df.parse(endtime);  
                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[] 返回值为:{天, 时, 分, 秒} 
         */  
        public static long[] getDistanceTimes(String starttime, String endtime) {  
            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(starttime);  
                two = df.parse(endtime);  
                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秒 
         */  
        public static String getDistanceTime(String starttime, String endtime) {  
            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(starttime);  
                two = df.parse(endtime);  
                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 + "";  
        }  
}



版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章
最新文章
相关文章