时间计算操作工具类

原创 2018年04月16日 17:18:49

public class TimeUtil {

public static long SECOND = 1;
public static long MINUTE = 60 * SECOND;
public static long HOUR   = 60 * MINUTE;
public static long DAY    = 24 * HOUR;
public static long MONTH  = 30 * DAY;
public static long YEAR   = 12 * MONTH;

public static String TIME_FORMAT       = "yyyy-MM-dd HH:mm";
public static String TIME_FORMAT_DAY   = "yyyy-MM-dd";
public static String TIME_FORMAT_OTHER = "MM月dd日 HH:mm";
public static String TIME_FORMAT_TODAY = "HH:mm";

public static String TIME_FORMAT_YEAR_MONTH_DAY = "yyyy.MM.dd";
public static String TIME_FORMAT_SECOND         = "HH时mm分ss秒后结束";


public static boolean moreThanOneMinute(long millisTime) {
    return millisTime > MINUTE * 1000;
}

public static boolean moreThanOneHour(long millisTime) {
    return millisTime > HOUR * 1000;
}

public static boolean moreThanOneDay(long millisTime) {
    return millisTime > DAY * 1000;
}

public static boolean moreThanOneMonth(long millisTime) {
    return millisTime > MONTH * 1000;
}

public static boolean monThanOneYear(long millisTime) {
    return millisTime > YEAR * 1000;
}

public static class TimeInfo {
    public String day;
    public String hour;
    public String minute;
    public String second;
}

public static boolean isToday(long time) {
    Calendar calendar = Calendar.getInstance();
    int      currDay  = calendar.get(Calendar.DAY_OF_MONTH);
    int      currMon  = calendar.get(Calendar.MONTH);
    int      currYear = calendar.get(Calendar.YEAR);
    calendar.clear();
    calendar.set(currYear, currMon, currDay);
    long todayTime = calendar.getTimeInMillis();
    return time >= todayTime && time < todayTime + DAY * 1000;
}

public static boolean isTomorrow(long time) {
    Calendar calendar = Calendar.getInstance();
    int      currDay  = calendar.get(Calendar.DAY_OF_MONTH);
    int      currMon  = calendar.get(Calendar.MONTH);
    int      currYear = calendar.get(Calendar.YEAR);
    calendar.clear();
    calendar.set(currYear, currMon, currDay);
    long todayTime = calendar.getTimeInMillis();
    return time >= todayTime + DAY * 1000 && time < todayTime + 2 * DAY * 1000;
}

public static boolean isMoreThanDay(long time) {
    return time - DAY * 1000 > 0;
}

/**
 * 获取时间格式
 *
 * @param time 时间  秒
 * @return
 */
public static String getTimeFormatSecond(long time) {
    return new SimpleDateFormat(TIME_FORMAT_SECOND).format(new Date(time));
}

public static TimeInfo getTimeInfo(long time) {
    int           day           = (int) (time / (1000 * 60 * 60 * 24));
    int           hour          = (int) ((time % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    int           minute        = (int) ((time % (1000 * 60 * 60)) / (1000 * 60));
    int           second        = (int) ((time % (1000 * 60)) / 1000);
    DecimalFormat decimalFormat = new DecimalFormat("00");
    TimeInfo      timeInfo      = new TimeInfo();
    timeInfo.day = validLength(day) ? String.valueOf(day) : decimalFormat.format(day);
    timeInfo.hour = validLength(hour) ? String.valueOf(hour) : decimalFormat.format(hour);
    timeInfo.minute = validLength(minute) ? String.valueOf(minute) : decimalFormat.format(minute);
    timeInfo.second = validLength(second) ? String.valueOf(second) : decimalFormat.format(second);
    return timeInfo;
}

private static boolean validLength(int time) {
    return String.valueOf(time).length() >= 2;
}

public static String getCountDownSecondString(long time, boolean start) {
    int           day         = (int) (time / (1000 * 60 * 60 * 24));
    int           hour        = (int) ((time % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    int           minute      = (int) ((time % (1000 * 60 * 60)) / (1000 * 60));
    int           second      = (int) ((time % (1000 * 60)) / 1000);
    int           millisecond = (int) (time % 1000);
    StringBuilder builder     = new StringBuilder();
    if (day != 0) {
        builder.append(day).append("天");
    }
    if (day != 0 || hour != 0) {
        if (hour < 10) builder.append("0");
        builder.append(hour).append("时");
    }
    if (day != 0 || hour != 0 || minute != 0) {
        if (minute < 10) builder.append("0");
        builder.append(minute).append("分");
    }
    if (second < 10) builder.append("0");
    if (start)
        builder.append(second).append("秒后结束");
    else
        builder.append(second).append("秒");
    return builder.toString();
}

/**
 * 倒计时
 */
public static String getCountDownHomeString(long time) {
    int           day         = (int) (time / (1000 * 60 * 60 * 24));
    int           hour        = (int) ((time % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    int           minute      = (int) ((time % (1000 * 60 * 60)) / (1000 * 60));
    int           second      = (int) ((time % (1000 * 60)) / 1000);
    int           millisecond = (int) (time % 1000);
    StringBuilder builder     = new StringBuilder();
    if (day != 0) {
        builder.append(day).append("天");
    }
    if (day != 0 || hour != 0) {
        if (hour < 10) builder.append("0");
        builder.append(hour).append("时");
    }
    if (day != 0 || hour != 0 || minute != 0) {
        if (minute < 10) builder.append("0");
        builder.append(minute).append("分");
    }
    if (second < 10) builder.append("0");
    builder.append(second).append("秒");
    return builder.toString();
}

/**
 * 获取时间格式
 *
 * @param time 时间  秒
 * @return
 */
public static String getTimeFormatToday(long time) {
    return new SimpleDateFormat(TIME_FORMAT_TODAY).format(new Date(time));
}


/**
 * 获取时间格式
 *
 * @param time 时间  秒
 * @return
 */
public static String getTimeFormatYearMonthDay(long time) {
    return new SimpleDateFormat(TIME_FORMAT_YEAR_MONTH_DAY).format(new Date(time));
}

/**
 * 获取时间格式
 *
 * @param time 时间  秒
 * @return
 */
public static String getTimeFormatOther(long time) {
    return new SimpleDateFormat(TIME_FORMAT_OTHER).format(new Date(time));
}

/**
 * 获取时间格式
 *
 * @param time 时间  秒
 * @return
 */
public static String getTimeFormat(long time) {
    return new SimpleDateFormat(TIME_FORMAT).format(new Date(time));
}

/**
 * 获取时间格式
 *
 * @param time 时间  秒
 * @return
 */
public static String getTimeFormatDay(long time) {
    return new SimpleDateFormat(TIME_FORMAT_DAY).format(new Date(time));
}


/**
 * 获取时间间隔格式  例:5分钟前 ,5小时前 ,5天前,5月前,2016年
 *
 * @param time
 * @return
 */
public static String getTimeDistance(Context context, long time) {
    String distanceTime  = null;
    long   distanceTimes = System.currentTimeMillis() / 1000 - time / 1000;

    int  curHour       = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
    int  curMin        = Calendar.getInstance().get(Calendar.MINUTE);
    int  curSecond     = Calendar.getInstance().get(Calendar.SECOND);
    long currentSecond = curHour * HOUR + curMin * MINUTE + curSecond;

    if (distanceTimes < MINUTE) {
        distanceTime = "刚刚";
    } else if (distanceTimes < HOUR) {
        distanceTime = distanceTimes / MINUTE + context.getResources().getString(R.string.time_before_minute);
    } else if (distanceTimes < currentSecond) {
        distanceTime = "今天" + getTimeFormatToday(time);
    } else if (distanceTimes < 1 * DAY + currentSecond) {
        distanceTime = "昨天" + getTimeFormatToday(time);
    } else if (distanceTimes < 2 * DAY + currentSecond) {
        distanceTime = "前天" + getTimeFormatToday(time);
    } else if (distanceTimes < YEAR) {
        distanceTime = getTimeFormatOther(time);
    } else {
        distanceTime = getTimeFormat(time);
    }
    return distanceTime;
}

public static String timeFormat(long timeMillis, String pattern) {
    SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.CHINA);
    return format.format(new Date(timeMillis));
}

public static String formatPhotoDate(long time) {
    return timeFormat(time, "yyyy-MM-dd");
}

public static String formatPhotoDate(String path) {
    File file = new File(path);
    if (file.exists()) {
        long time = file.lastModified();
        return formatPhotoDate(time);
    }
    return "1970-01-01";
}

}

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34942689/article/details/79963456

(原创)分享自己写的几个工具类(五)时间计算工具类

话不多说,直接上代码,拿来就能通用 /** * Created by 熊叶平 on 2017/6/16 0016. * 时间操作工具类 */ public class TimeUtil { ...
  • Android_xiong_st
  • Android_xiong_st
  • 2017-06-16 16:07:48
  • 241

java日期计算工具类【包含常用的日期计算方法】

java日期计算工具类【包含常用的日期计算方法】 package com.test; import java.sql.Timestamp; import java.text.SimpleDateF...
  • jammiwang19870815
  • jammiwang19870815
  • 2013-06-28 13:20:53
  • 941

时间计算工具类

package TimeJiSuan;import java.text.DecimalFormat;import java.util.Arrays;/** * 时间计算工具类 */public cla...
  • chendabian
  • chendabian
  • 2010-06-03 20:57:00
  • 314

Java常见日期格式及日期的计算工具类

Java 获取指定时间的日期,日期相差天数的计算
  • xiaoshen_joyin
  • xiaoshen_joyin
  • 2016-02-28 22:38:34
  • 1922

JS时间处理工具类

//1、日期格式化Date.prototype.Format = function (fmt) { var o = { "M+": this.getMonth() + 1, /...
  • Cantus_hjk
  • Cantus_hjk
  • 2017-11-22 16:04:54
  • 125

Java获取时间 时间计算 转换时间工具类

package com.mh.util;import java.sql.Timestamp; import java.text.ParseException; import java.text.S...
  • Ani521smile
  • Ani521smile
  • 2016-05-04 14:58:35
  • 2545

java工具类-计算相对时间的工具类即两个时间的时间差

java相对时间的工具类,此类中有两个暴露的方法,相对于当前时间的方法和相对于某时间的方法。 返回String,如:2小时前/3天2时13秒/昨天...
  • sgyingyin
  • sgyingyin
  • 2016-08-19 16:38:58
  • 2610

时间计算工具类01测试

package TimeJiSuan;public class TestTime {  public static void main(String[] args) {         Time t1...
  • chendabian
  • chendabian
  • 2010-06-03 21:00:00
  • 193

关于时间计算的工具类 DateTool

package com.dcc.server.util; import java.text.ParseException; import java.text.SimpleDateFormat; im...
  • levae1024
  • levae1024
  • 2017-11-24 13:20:34
  • 94

时间计算工具类(加/减)

package 时间日期类; import java.text.DecimalFormat; import java.util.Arrays; /** * 时间计算工具类 */ public...
  • randomnet
  • randomnet
  • 2013-01-03 11:05:30
  • 465
收藏助手
不良信息举报
您举报文章:时间计算操作工具类
举报原因:
原因补充:

(最多只允许输入30个字)