工具类(一):JDK8日期工具类(持续更新中)

持续更新中

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

/**
 * TODO JDK8 日期工具类(不用于线上,因为JDK8太先进,只用于个人总结)
 *
 * @author liuzebiao
 * @Date 2020-4-29 10:13
 */
public class DateUtils {

    /**
     * 获取当前时间的时间戳(10位:不带毫秒)
     */
    public static Long getTimeStamp() {
        LocalDateTime now = LocalDateTime.now();
        return now.toEpochSecond(OffsetDateTime.now().getOffset());
    }

    /**
     * 获取当前时间的时间戳(13位:带毫秒)
     */
    public static Long getTimeStampWithMs() {
        LocalDateTime now = LocalDateTime.now();
        return now.toInstant(OffsetDateTime.now().getOffset()).toEpochMilli();
    }

    /**
     * Long --> String
     */
    public static String long2String(Long time, String pattern) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
        return dtf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));
    }

    /**
     * String --> Long
     */
    public static Long string2Long(String time, String pattern) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
        LocalDateTime parse = LocalDateTime.parse(time, dtf);
        return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    /**
     * 获取本月第一天
     */
    public static LocalDate firstDayOfThisMonth() {
        LocalDate today = LocalDate.now();
        return today.with(TemporalAdjusters.firstDayOfMonth());
    }

    /**
     * 获取本月第N天
     */
    public static LocalDate dayOfThisMonth(int n) {
        LocalDate today = LocalDate.now();
        return today.withDayOfMonth(n);
    }

    /**
     * 获取本月最后一天
     */
    public static LocalDate lastDayOfThisMonth() {
        LocalDate today = LocalDate.now();
        return today.with(TemporalAdjusters.lastDayOfMonth());
    }

    /**
     * 获取指定月的最后一天
     * @param month 1-12
     */
    public static LocalDate lastDayOfMonth(int year,int month) {
        LocalDate ofDate = LocalDate.of(year, month, 1);
        return ofDate.with(TemporalAdjusters.lastDayOfMonth());
    }

    /**
     * 获取本月第一天的开始时间
     */
    public static String startOfThisMonth(String pattern) {
        LocalDateTime ofDateTime = LocalDateTime.of(firstDayOfThisMonth(), LocalTime.MIN);
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
        return ofDateTime.format(dtf);
    }

    /**
     * 取本月最后一天的结束时间
     */
    public static String endOfThisMonth(String pattern) {
        LocalDateTime ofDateTime = LocalDateTime.of(lastDayOfThisMonth(), LocalTime.MAX);
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
        return ofDateTime.format(dtf);
    }

    public static void main(String[] args) {
        System.out.println(firstDayOfThisMonth());
        System.out.println(dayOfThisMonth(7));
        System.out.println(lastDayOfThisMonth());
        System.out.println(lastDayOfMonth(2020, 2));
        System.out.println(startOfThisMonth("yyyy-MM-dd HH:mm:ss"));
        System.out.println(endOfThisMonth("yyyy-MM-dd HH:mm:ss"));

    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

扛麻袋的少年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值