Java implements the time difference between a certain time and the current time

The time-related classes in java include Data class and Canlendar class

Data class

1. Use a parameterless constructor

Data nowTime = new Date()
#输出的就是当前的时间

2. The constructor with parameters is no longer explained here

In addition, the static function public long currentTimeMillis() of the System class can obtain the current time of the system, the time difference from 8 o'clock on January 1, 1970, and the return is the number of milliseconds.

Generally, the Data type is converted to the Calendar type for convenient use.

转换函数:
//Date类型转Calendar类型
    public static Calendar dataToCalendar(Date date) {
    
    
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        return calendar;
    }

Calendar

Initialize the calendar instance

Calendar calendar = Calendar.getInstance();

You can get time, year, month, day and other information through the calendar.

The specific code is as follows:

Realization function: the time difference between a specific time and the current time is output with the largest time difference, in order: year, month, day, hour, minute, second.

主函数:
		StringTokenizer fenxi = new StringTokenizer(getTime("2020-07-29 11:37:17"),"|");
        int[] result = new int[2];
        int i = 0;
        while (fenxi.hasMoreTokens()){
    
    
            result[i] = Integer.parseInt(fenxi.nextToken());
            i++;
        }
        String[] time_list = {
    
    "年","月","天","小时","分钟","秒"};
        System.out.println(""+result[0]+time_list[result[1]]+"前");
public static String getTime(String log_time){
    
    
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time_now = new Date();
        long diff = 0;
        String CountTime = "";
        int year=0,month=0,day=0;
        long hours=0,minutes=0,s=0;
        try {
    
    
            Date time_ago = df.parse(log_time);
            diff = time_now.getTime() - time_ago.getTime();
            Calendar  currentTimes =dataToCalendar(time_now);//当前系统时间转Calendar类型
            Calendar  pastTimes =dataToCalendar(time_ago);//查询的数据时间转Calendar类型
            year = currentTimes.get(Calendar.YEAR) - pastTimes.get(Calendar.YEAR);//获取年
            month = currentTimes.get(Calendar.MONTH) - pastTimes.get(Calendar.MONTH);
            day = currentTimes.get(Calendar.DAY_OF_MONTH) - pastTimes.get(Calendar.DAY_OF_MONTH);
            if (month < 0) {
    
    
                month = (month + 12) % 12;//获取月
                year--;
            }
            if (day < 0) {
    
    
                month -= 1;
                currentTimes.add(Calendar.MONTH, -1); //减一个月
                day = day + currentTimes.getActualMaximum(Calendar.DAY_OF_MONTH);//获取日
            }
            long days = diff / (1000 * 60 * 60 * 24);
            hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60); //获取时
            minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);  //获取分钟
            s=(diff/1000-days*24*60*60-hours*60*60-minutes*60);//获取秒
            CountTime=""+year+"年"+month+"月"+day+"天"+hours+"小时"+minutes+"分"+s+"秒";
        } catch (ParseException e) {
    
    
            e.printStackTrace();
        }
        System.out.println(CountTime);
        return year != 0 ? ""+year+"|"+0 :
               month != 0 ? ""+month+"|"+1 :
               day != 0 ? ""+day+"|"+2 :
               hours != 0 ? ""+hours+"|"+3 :
               minutes != 0 ? ""+minutes+"|"+4 : ""+s+"|"+5 ;
    }
输出结果:2月前

Guess you like

Origin blog.csdn.net/weixin_43477545/article/details/109096335