计算从前某个时间距离现在经过了多久时间

文章目录

    • 摘要
    • 一、实现

摘要

计算过去某个时间距离现在经过了多久时间。

一、实现

package com.mtons.mblog.modules.template.method;

import freemarker.template.TemplateModelException;
import com.mtons.mblog.modules.template.BaseMethod;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Month;
import java.util.Date;
import java.util.List;

/**
 * 计算从前某个时间距离现在经过了多久时间
 * Created by langhsu on 2017/11/14.
 */
public class TimeAgoMethod extends BaseMethod {
     
    private static final long ONE_MINUTE = 60000L;
    private static final long ONE_HOUR = 3600000L;
    private static final long ONE_DAY = 86400000L;
    private static final long ONE_WEEK = 604800000L;

    private static final String ONE_SECOND_AGO = "秒前";
    private static final String ONE_MINUTE_AGO = "分钟前";
    private static final String ONE_HOUR_AGO = "小时前";
    private static final String ONE_DAY_AGO = "天前";
    private static final String ONE_MONTH_AGO = "月前";
    private static final String ONE_YEAR_AGO = "年前";
    private static final String ONE_UNKNOWN = "未知";

    @Override
    public Object exec(List arguments) throws TemplateModelException {
     
        Date time = getDate(arguments, 0);
        return format(time);
    }

    public static String format(Date date) {
     
        if (null == date) {
     
            return ONE_UNKNOWN;
        }
        long delta = new Date().getTime() - date.getTime();
        if (delta < 1L * ONE_MINUTE) {
     
            long seconds = toSeconds(delta);
            return (seconds <= 0 ? 1 : seconds) + ONE_SECOND_AGO;
        }
        if (delta < 45L * ONE_MINUTE) {
     
            long minutes = toMinutes(delta);
            return (minutes <= 0 ? 1 : minutes) + ONE_MINUTE_AGO;
        }
        if (delta < 24L * ONE_HOUR) {
     
            long hours = toHours(delta);
            return (hours <= 0 ? 1 : hours) + ONE_HOUR_AGO;
        }
        if (delta < 48L * ONE_HOUR) {
     
            return "昨天";
        }
        if (delta < 30L * ONE_DAY) {
     
            long days = toDays(delta);
            return (days <= 0 ? 1 : days) + ONE_DAY_AGO;
        }
        if (delta < 12L * 4L * ONE_WEEK) {
     
            long months = toMonths(delta);
            return (months <= 0 ? 1 : months) + ONE_MONTH_AGO;
        } else {
     
            long years = toYears(delta);
            return (years <= 0 ? 1 : years) + ONE_YEAR_AGO;
        }
    }

    private static long toSeconds(long date) {
     
        return date / 1000L;
    }

    private static long toMinutes(long date) {
     
        return toSeconds(date) / 60L;
    }

    private static long toHours(long date) {
     
        return toMinutes(date) / 60L;
    }

    private static long toDays(long date) {
     
        return toHours(date) / 24L;
    }

    private static long toMonths(long date) {
     
        return toDays(date) / 30L;
    }

    private static long toYears(long date) {
     
        return toMonths(date) / 365L;
    }


    public static void main(String[] args) throws ParseException {
     
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date pDate = dateFormat.parse("2016-12-01");
        System.out.println(format(pDate));
    }
}

你可能感兴趣的:(leetcode,图论)