zoukankan      html  css  js  c++  java
  • 两个时间计算【十分秒】

    两个时间计算【十分秒】

    1. 用于计算接口的执行时长
    package com.chinalife.maced_bdsdk;
    
    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    public class HaoShiDemo {
        public static void main(String[] args) throws ParseException, InterruptedException {
    
            Date start = new Date();
            Thread.sleep(1000);
            Date end = new Date();
    
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
            String start1 = df.format(start);
            String end2 = df.format(end);
    
            System.out.println("HaoShi--->:" + "Group:" + MainDate(end2, start1));
        }
    
        public static String MainDate(String StartDate, String EndDate) throws ParseException {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
            Date currentTime = df.parse(StartDate);   //当前系统时间
            Date firstTime = df.parse(EndDate);     //查询的数据时间
            String str = getTime(currentTime, firstTime);
    //        System.out.println("获取的年月日时分秒时间差为:" + str);
            return str;
        }
    
        //获取时间差方法
        public static String getTime(Date currentTime, Date firstTime) {
            long diff = currentTime.getTime() - firstTime.getTime();//这样得到的差值是微秒级别
            Calendar currentTimes = dataToCalendar(currentTime);//当前系统时间转Calendar类型
            Calendar firstTimes = dataToCalendar(firstTime);//查询的数据时间转Calendar类型
            int year = currentTimes.get(Calendar.YEAR) - firstTimes.get(Calendar.YEAR);//获取年
            int month = currentTimes.get(Calendar.MONTH) - firstTimes.get(Calendar.MONTH);
            int day = currentTimes.get(Calendar.DAY_OF_MONTH) - firstTimes.get(Calendar.DAY_OF_MONTH);
            if (day < 0) {
                month -= 1;
                currentTimes.add(Calendar.MONTH, -1);
                day = day + currentTimes.getActualMaximum(Calendar.DAY_OF_MONTH);//获取日
            }
            if (month < 0) {
                month = (month + 12) % 12;//获取月
                year--;
            }
            long days = diff / (1000 * 60 * 60 * 24);
            long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60); //获取时
            long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);  //获取分钟
            long s = (diff / 1000 - days * 24 * 60 * 60 - hours * 60 * 60 - minutes * 60);//获取秒
            String CountTime = "" + year + "年" + month + "月" + day + "天" + hours + "小时" + minutes + "分" + s + "秒";
            return CountTime;
        }
    
        //Date类型转Calendar类型
        public static Calendar dataToCalendar(Date date) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            return calendar;
        }
    }
    
  • 相关阅读:
    有36辆自动赛车和6条跑道,没有计时器的前提下,最少用几次比赛可以筛选出最快的三辆赛车?----腾讯2016研发工程师在线模拟笔试题
    10G个整数,乱序排列,要求找出中位数。内存限制为 2G。只写出思路即可
    计算机网络总结(二)
    计算机网络总结(一)
    最小编辑距离
    寻找两个有序数组的中位数
    Linux下几款C++程序中的内存泄露检查工具
    DDIA
    推荐引擎
    Innodb中的事务隔离级别和锁的关系
  • 原文地址:https://www.cnblogs.com/Twittery/p/14331646.html
Copyright © 2011-2022 走看看