从ZonedDateTime开启日期时间的管理

有时候服务器对于时间日期,只会返回给你一个字符串,而你根据业务需求要把该字符串转换成各种需要的格式。下面是本人总结的一个工具类,话说Java 8的时间日期真的很好用,我们服务器返回的是标准时间,即ZonedDateTime格式,其他格式的类似。

/**
 * Created by Apple on 17/4/21.
 * 时间日期工具类,注意这里的时间字符串都是标准时间,即ZonedDateTime样式
 */

public class DateUtils {

    /**
     * 根据给定的时间返回对应格式的字符串
     *  1、小于一分钟---"刚刚"
     *  2、大于一分钟而在本日内的,返回格式为"HH:mm"
     *  3、今年之内而不再本日内,格式为"MM-dd"
     *  4、否则,格式为"yyyy-MM-dd"
     * @param strDate
     * @return
     */
    public static String getFormatDate(String strDate) {
        DateTimeFormatter formatter;
        ZonedDateTime zonedDateTime = ZonedDateTime.parse(strDate);
        if (zonedDateTime.toLocalDate().equals(LocalDate.now())) {
            if (getOverMinutes(strDate) <= 1) {
                return "刚刚";
            } else {
                formatter = DateTimeFormatter.ofPattern("HH:mm", Locale.CHINA);
                return zonedDateTime.format(formatter);
            }
        } else {
            if (zonedDateTime.toLocalDate().getYear() == LocalDate.now().getYear()) {
                formatter = DateTimeFormatter.ofPattern("MM-dd", Locale.CHINA);
            } else {
                formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.CHINA);
            }
            return zonedDateTime.format(formatter);
        }
    }

    /**
     * 将给定的秒数转换为一段持续时间,如70s,可为01:10
     * @param s
     * @return
     */
    public static String sec2Duration(int s) {
        int sec;
        if (s < 0) {
            return "00:00";
        }
        int min = s / 60;
        if (min < 60) {
            sec = s % 60;
            return unitFormat(min) + ":" + unitFormat(sec);
        } else {
            int hour = min / 60;
            min = min % 60;
            sec = s - hour * 3600 - min * 60;
            return unitFormat(hour) + ":" + unitFormat(min) + ":" + unitFormat(sec);
        }
    }
    public static String unitFormat(int i) {
        return i >= 0 && i < 10 ? "0" + i : i + "";
    }

    /**
     * 给定开始时间,返回还剩多少天多少时多少分的字符串数组
     * @param startTime
     * @return
     */
    public static String[] getLeftStr(String startTime) {
        Instant start = ZonedDateTime.parse(startTime).toInstant();
        long minutes = Duration.between(Instant.now(), start).toMinutes();
        if (minutes < 60 && minutes > 0) {
            return new String[]{"0", "0", minutes + ""};
        } else if (minutes <= 0) {
            return null;
        }
        long hours = minutes / 60;
        if (hours < 60) {
            minutes = minutes % 60;
            return new String[]{"0", hours + "", minutes + ""};
        } else {
            long days = hours / 24;
            hours = hours % 24;
            minutes = minutes - days * 24 * 60 - hours * 60;
            return new String[]{days + "", hours + "", minutes + ""};
        }
    }
    /**
     * 根据创建时间和总共时间得到当前剩余时间
     * @param createStr
     * @param totalMills
     * @return
     */
    public static long getLeftMills(String createStr, long totalMills) {
        long leftMills = totalMills - getOverMills(createStr);
        return leftMills >= 0 ? leftMills : 0;
    }
    public static long getLeftMinutes(String createStr, long totalMinutes) {
        long leftMinutes = totalMinutes - getOverMinutes(createStr);
        return leftMinutes >= 0 ? leftMinutes : 0;
    }
    public static long getLeftHours(String createStr, long totalHours) {
        long leftHours = totalHours - getOverHours(createStr);
        return leftHours >= 0 ? leftHours : 0;
    }
    /**
     * 根据跟定的时间得到到目前为止过了多少秒
     * @param time
     * @return
     */
    public static long getOverMills(String time){
        return getOverTime(time).toMillis();
    }
    public static long getOverMinutes(String time){
        return getOverTime(time).toMinutes();
    }
    public static long getOverHours(String time){
        return getOverTime(time).toHours();
    }
    private static Duration getOverTime(String time) {
        Instant timeInstant = ZonedDateTime.parse(time).toInstant();
        Instant now = Instant.now();
        return Duration.between(timeInstant, now);
    }

    /**
     * 将UNIX时间戳(秒级)格式化成ZoneDateTime的格式
     */
    public static String format2ZoneDateTimeFromSecond(long timestamp) {
        return ZonedDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.of("Asia/Shanghai")).toString();
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值