Java 时间相关的工具类整理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_26437925/article/details/81270344
  • 人性化的时间
package timeutil;

/**
 * @Author mubi
 * @Date 2018/7/29 上午11:36
 */
public enum HumanizedTimeEnum {
    LastYear("去年", "y", -1),
    LastMonth("上个月", "M", -1),
    LastHour("一小时前", "h", -1),
    LastTwoHour("两小时前", "h", -2),
    Yesterday("昨天", "d", -1),
    BeforeYesterday("前天", "d", -2),
    Tomorrow("明天", "d", 1),
    LastWeek("上周", "w", -1)
    ;

    // 成员变量
    String name;
    String unit;
    int offset;

    HumanizedTimeEnum(String name, String unit, int offset) {
        this.name = name;
        this.unit = unit;
        this.offset = offset;
    }

    public static void showAll() {
        for (HumanizedTimeEnum c : HumanizedTimeEnum.values()) {
            System.out.println(c);
        }
    }

    @Override
    public String toString() {
        return "HumanizedTimeEnum{" +
                "name='" + name + '\'' +
                ", unit='" + unit + '\'' +
                ", offset=" + offset +
                '}';
    }
}
  • 时间单位
package timeutil;

import java.util.Calendar;

/**
 * @Author mubi
 * @Date 2018/7/29 上午11:36
 */
public enum TimeUnitEnum {
    Year("y", Calendar.YEAR),
    Month("M", Calendar.MONTH),
    Day("d", Calendar.DATE),
    Hour("h", Calendar.HOUR),
    Minute("m", Calendar.MINUTE),
    Second("s", Calendar.SECOND),
    Week("w", Calendar.WEEK_OF_MONTH)
    ;

    // 成员变量
    String name;
    int unit;

    // 构造方法
    TimeUnitEnum(String name, int unit) {
        this.name = name;
        this.unit = unit;
    }

    /**
     *
     * @param name
     * @return 对应的时间单位
     */
    public static int getUnit(String name) {
        for (TimeUnitEnum c : TimeUnitEnum.values()) {
            if (c.getName().equals(name)) {
                return c.getUnit();
            }
        }
        return -1;
    }

    public String getName() {
        return name;
    }

    public int getUnit() {
        return unit;
    }

}
  • 时间工具类
package timeutil;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author mubi
 * @Date 2018/7/29 上午11:36
 */
public class TimeUtil{

    /**
     *
     * @param unixTimeBase 毫秒
     * @param unit 时间单位,见TimeUnitEnum
     * @param offset int类型的偏移值
     * @return
     * @throws Exception
     */
    public static Long offsetTime(Long unixTimeBase, String unit, int offset) throws Exception {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(unixTimeBase);
        int _unit = TimeUnitEnum.getUnit(unit);
        if(_unit == -1) {
            throw new Exception("such time unit not supported");
        }
        cal.add(_unit, offset);
        return cal.getTime().getTime();
    }

    public static Long offsetCommonTime(Long unixTimeBase, HumanizedTimeEnum commonOffset) throws Exception {
        return offsetTime(unixTimeBase, commonOffset.unit, commonOffset.offset);
    }


    public static Date unixTime2Str(Long unixTime) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(unixTime);
        return cal.getTime();
    }

    /**
     *
     * @param unixTime 毫秒
     * @param format  for example "yyyy-MM-dd HH:mm:ss"
     * @return
     */
    public static String unixTime2Str(Long unixTime, String format) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(unixTime);
        Date now = cal.getTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(now);
    }

}

猜你喜欢

转载自blog.csdn.net/qq_26437925/article/details/81270344
今日推荐