java工具包 获取时间/时间转时间戳/时间戳转换时间/剩余时间

DayTimeUtils .Class

public class DayTimeUtils {
    /**
     * 获取本日剩余 秒
     *
     * @return 时间戳/1000
     */
    public static int getLastSeconds() {
        // 得到今天 晚上的最后一刻 最后时间
        String last = getTime() + " 23:59:59";
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parse = LocalDateTime.parse(last, fmt);
        long latDate = parse.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
        // 得到的毫秒 除以1000转换 为秒
        return (int) (latDate - System.currentTimeMillis()) / 1000;
    }

    /**
     * 获取时间 yyyy-MM-dd
     *
     * @return 时间
     */
    public static String getTime() {
        LocalDate now = LocalDate.now();
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return now.format(fmt);
    }

    /**
     * 获取时间 yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getTimeTo() {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return now.format(fmt);
    }

    /**
     * 时间戳转日期/时间
     *
     * @param seconds 时间戳
     * @param pattern 格式
     * @return
     */
    public static String timeStamp2Date(long seconds, String pattern) {
        String time = "暂无数据";
        if (TextUtils.isEmpty(pattern)) pattern = "yyyy-MM-dd  HH:mm:ss";
        LocalDateTime dateTime = LocalDateTime.ofEpochSecond(seconds / 1000L, 0, ZoneOffset.ofHours(8));
        if (seconds != 0) time = dateTime.format(DateTimeFormatter.ofPattern(pattern));
        return time;
    }

    /**
     * 日期/时间转时间戳
     *
     * @param date    时间
     * @param pattern 格式
     * @return
     */
    public static long date2TimeStamp(String date, String pattern) {
        long timeStamp;
        if (TextUtils.isEmpty(pattern)) pattern = "yyyy-MM-dd HH:mm:ss";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        LocalDateTime localDateTime = LocalDateTime.parse(date, formatter);
        timeStamp = localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
        return timeStamp;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值