zoukankan      html  css  js  c++  java
  • TimeUtil工具类

    package com.ymw.sqlite.testData;

    import android.util.Log;

    import java.text.DecimalFormat;
    import java.text.ParsePosition;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    public class TimeUtil {

    private static final String TAG = "TimeUtil";


    /**
    * JAVA日期格式输出月份前面不想被自动补0
    *
    * @return 返回 2/15/2012 格式的STRING类型,用M月份不会自动补0,用MM月份会自动补0;
    * <p>
    * SimpleDateFormat sdf = new SimpleDateFormat("yyyyMdd");
    * System.out.println(sdf.format(new java.util.Date()));
    * JAVA日期格式输出月份前面不想被自动补0,那么就用SimpleDateFormat("M/dd/yyyy"); 一个M。
    * 如果想被自动补0,那么就用MM 即是SimpleDateFormat("MM/dd/yyyy");
    */
    public static String getTime_0() {
    //SimpleDateFormat formatter = new SimpleDateFormat("M/dd/yyyy");
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy/M/d");

    //Date currentTime = new Date();//得到当前系统时间
    String timeDes = "2020-11-17 15:33:00";
    Date currentTime = strToDateLong(timeDes);//得到当前系统时间

    String timeStr = formatter.format(currentTime); //将日期时间格式化
    //输出:2009-08-18 20:06:13
    Log.w(TAG, "getTime_0....." + timeStr);
    return timeStr;
    }


    /**
    * 将时间字符串转成 Date类型
    *
    * @param strDate:2020-6-25 15:00:00
    * @return
    */
    public static Date strToDateLong(String strDate) {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    ParsePosition pos = new ParsePosition(0);
    Date strtodate = formatter.parse(strDate, pos);
    return strtodate;
    }

    /**
    * java 实现 发布了多久的时间描述:几分钟后,几小时后,几天后,几个月后,几年后
    */
    public static void testTime() {
    String time = "2020-6-26 13:00:00";
    Date date = strToDateLong(time);
    String diffStr = timeUtile(date);
    Log.e(TAG, "diffTime=" + diffStr);
    }


    private static String timeUtile(Date endTime) {
    // 拿到当前时间戳和发布时的时间戳,然后得出时间戳差
    //Date curTime = new Date();
    Date curTime = strToDateLong("2020-6-25 14:00:00");
    if (endTime.getTime() < curTime.getTime()) {
    Log.e(TAG, "endTime < curTime....");
    return "";
    }

    long timeDiff = endTime.getTime() - curTime.getTime();
    //上面一行代码可以换成以下(兼容性的解决)
    // 单位换算
    long min = 60 * 1000;
    long hour = min * 60;
    long day = hour * 24;
    long week = day * 7;
    long month = week * 4;
    long year = month * 12;

    DecimalFormat df = new DecimalFormat("#");
    // 计算发布时间距离当前时间的周、天、时、分
    // floor返回最小值:2小时2分钟后即是2小时后; ceil返回最大值:2小时2分钟后即是3小时后
    double exceedyear = Math.floor(timeDiff / year);
    double exceedmonth = Math.floor(timeDiff / month);
    double exceedWeek = Math.floor(timeDiff / week);
    double exceedDay = Math.floor(timeDiff / day);
    double exceedHour = Math.floor(timeDiff / hour);
    double exceedMin = Math.floor(timeDiff / min);

    // 最后判断时间差到底是属于哪个区间,然后return
    if (exceedyear < 100 && exceedyear > 0) {
    return df.format(exceedyear) + "年后";
    } else {
    if (exceedmonth < 12 && exceedmonth > 0) {
    return df.format(exceedmonth) + "月后";
    } else {
    if (exceedWeek < 4 && exceedWeek > 0) {
    return df.format(exceedWeek) + "星期后";
    } else {
    if (exceedDay < 7 && exceedDay > 0) {
    return df.format(exceedDay) + "天后";
    } else {
    if (exceedHour < 24 && exceedHour > 0) {
    return df.format(exceedHour) + "小时后";
    } else {
    return df.format(exceedMin) + "分钟后";
    }
    }
    }
    }
    }
    }


    private static String getDatePoor(Date endDate, Date nowDate) {
    long nd = 1000 * 24 * 60 * 60;
    long nh = 1000 * 60 * 60;
    long nm = 1000 * 60;
    // long ns = 1000;
    // 获得两个时间的毫秒时间差异
    long diff = endDate.getTime() - nowDate.getTime();
    // 计算差多少天
    long day = diff / nd;
    // 计算差多少小时
    long hour = diff % nd / nh;
    // 计算差多少分钟
    long min = diff % nd % nh / nm;
    // 计算差多少秒//输出结果
    // long sec = diff % nd % nh % nm / ns;
    return day + "天" + hour + "小时" + min + "分钟";
    }


    }
  • 相关阅读:
    Python之字典
    Python之模块
    Python之字符串格式化
    结束循环
    循环
    C语言static和局部变量
    C static extern和全局变量
    extern static和函数
    C语言typedef
    C语言之文件包含
  • 原文地址:https://www.cnblogs.com/awkflf11/p/13199643.html
Copyright © 2011-2022 走看看