时间计算操作工具类

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

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";
}

}

猜你喜欢

转载自blog.csdn.net/qq_34942689/article/details/79963456
今日推荐