zoukankan      html  css  js  c++  java
  • JAVA 时间差距,两个时间相差多少天,时,分,秒

    方法一:获取天

     /*
        判读时间差距,两个时间相差多少天,时,分,秒
         */
        public static Long getDay(String date) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Long days = null;
            try {
                Date currentTime = dateFormat.parse(dateFormat.format(new Date()));//现在系统当前时间
                Date pastTime = dateFormat.parse(date);//过去时间
                long diff = currentTime.getTime() - pastTime.getTime();
                days = diff / (1000 * 60 * 60 * 24);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return days;
        }

    方法二:获取  天,小时,分,秒

     public void test() {
            try {
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date now = df.parse("2019-03-26 13:31:40");//当前时间
                Date date = df.parse("2004-01-02 11:30:24");//过去
                long l = now.getTime() - date.getTime();
                long day = l / (24 * 60 * 60 * 1000);
                long hour = (l / (60 * 60 * 1000) - day * 24);
                long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
                long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
                System.out.println("" + day + "天" + hour + "小时" + min + "分" + s + "秒");
            } catch (Exception e) {
    
            }
    
        }

    方法三:同上

     public void test() {
            try {
                SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date begin = dfs.parse("2004-01-02 11:30:24");
                Date end = dfs.parse("2004-03-26 13:31:40");
                long between = (end.getTime() - begin.getTime()) / 1000;//除以1000是为了转换成秒
                long day1 = between / (24 * 3600);
                long hour1 = between % (24 * 3600) / 3600;
                long minute1 = between % 3600 / 60;
                long second1 = between % 60 / 60;
                System.out.println("" + day1 + "天" + hour1 + "小时" + minute1 + "分" + second1 + "秒");
            } catch (Exception e) {
    
            }
    
        }

    方法四:比较时间大小

     public void test() {
            String s1 = "2008-01-25 09:12:09";
            String s2 = "2008-01-29 09:12:11";
            java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            java.util.Calendar c1 = java.util.Calendar.getInstance();
            java.util.Calendar c2 = java.util.Calendar.getInstance();
            try {
                c1.setTime(df.parse(s1));
                c2.setTime(df.parse(s2));
            } catch (java.text.ParseException e) {
                System.err.println("格式不正确");
            }
            int 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");
        }
  • 相关阅读:
    Gym 101194L / UVALive 7908
    POJ 2259
    POJ 2559
    Gym 101194E / UVALive 7901
    Gym 101194D / UVALive 7900
    一种整数集上二分的正确写法
    日常训练记录
    Gym 101194C / UVALive 7899
    Gym 101194A / UVALive 7897
    HDU 5542
  • 原文地址:https://www.cnblogs.com/nongzihong/p/11368058.html
Copyright © 2011-2022 走看看