Java系列 - LocalDateTime

package com.ifdom.date;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

/**
 * @Author ifredomvip@gmail.com
 * @Date 2022/5/20 10:23
 * @Version 1.0.0
 * @Description
 **/
public class DateMain {
    public static void main(String[] args) {
        dateCalculation();
    }

    /**
     * @Description 日期转化为字符串
     **/
    public static void date2String() {
        LocalDate now = LocalDate.now();
        System.out.println(now);
        String s = now.toString();
        System.out.println(s);
    }

    /**
     * @Description 字符串转化为日期
     **/
    public static void string2Date() {
        // 字符串整体,使用 parse 方法
        String s = "2022-05-25";
        LocalDate localDate = LocalDate.parse(s);
        System.out.println(localDate);

        // 分别指定具体年月日,使用 of 方法
        LocalDate localDate1 = LocalDate.of(2022, 5, 25);
        System.out.println(localDate1);
    }

    /**
     * @Description 时间转化为字符串
     **/
    public static void datetime2String() {
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        String s = now.toString();
        System.out.println(s);

        // 不携带纳秒
        LocalDateTime localDateTime = now.withNano(0);
        System.out.println(localDateTime);

        // 分别指定具体年月日 时分秒,使用 of 方法。
        LocalDateTime localDate1 = LocalDateTime.of(2022, 5, 25, 1, 2);
        System.out.println(localDate1);
    }

    /**
     * @Description 字符串转化为时间
     **/
    public static void string2Datetime() {

        String s = "2022-05-25 12:12:22";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.parse(s, formatter);
        System.out.println(localDateTime);

        // 不携带格式化模式,那么字符串必须符合日期的ISO标准
        String s1 = "2022-05-25T10:15:31.581";
        LocalDateTime localDateTime1 = LocalDateTime.parse(s1);
        System.out.println(localDateTime1);
    }

    /**
     * @Description 格式化
     **/
    public static void dateFormatter() {
        // 日期
        LocalDate now = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String s = now.format(formatter);
        System.out.println(s);

        // 时间
        LocalDateTime now1 = LocalDateTime.now();
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String s1 = now1.format(formatter1);
        System.out.println(s1);
    }

    /**
     * @Description 日期计算
     **/
    public static void dateCalculation() {

        LocalDate date1 = LocalDate.of(2022, 5, 15);
        LocalDate date2 = LocalDate.of(2022, 3, 20);

        // 日期先后
        boolean date1After = date1.isAfter(date2);
        System.out.println(date1After);

        // 后X天的日期(X为整数即可)
        LocalDate tomorrow = date1.plusDays(1);
        System.out.println("明天:" + tomorrow);
        LocalDate twoAfterDay = date1.plusDays(2);
        System.out.println("后天:" + twoAfterDay);
        // 前X天的日期
        LocalDate yesterday = date1.plusDays(-1);
        System.out.println("昨天:" + yesterday);
        LocalDate laterDay = date1.plusDays(-2);
        System.out.println("前天:" + laterDay);

        // 后X周的日期(X为整数即可)
        LocalDate tomorrowWeek = date1.plusWeeks(1);
        System.out.println("下一周:" + tomorrowWeek);
        LocalDate twoAfterWeek = date1.plusDays(2);
        System.out.println("下下周:" + twoAfterWeek);
        // 后X周的日期(X为整数即可)
        LocalDate yesterdayWeek = date1.plusWeeks(-1);
        System.out.println("上一周:" + tomorrowWeek);
        LocalDate laterWeek = date1.plusDays(-2);
        System.out.println("上上周:" + twoAfterWeek);


        // 间隔
        Period period = Period.between(date1, date2);
        int periodYears = period.getYears();
        int periodMonths = period.getMonths();
        int periodDays = period.getDays();
        System.out.println("间隔 年:" + periodYears);
        System.out.println("间隔 月:" + periodMonths);
        System.out.println("间隔 日:" + periodDays);

        // 总天数
        long countDays = date1.toEpochDay() - date2.toEpochDay();
        System.out.println("日期相差的总天数:" + countDays);
    }

    /**
     * @Description 获取指定日期
     **/
    public static void dateSpecify() {
        LocalDate today = LocalDate.now();
        //获取当前月第一天:
        LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth());
        // 取本月最后一天
        LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth());
        //取下一天:
        LocalDate nextDay = lastDayOfThisMonth.plusDays(1);
        //当年最后一天
        LocalDate lastDay = today.with(TemporalAdjusters.lastDayOfYear());
        //2021年最后一个周日。
        LocalDate lastMondayOf2021 = LocalDate.parse("2021-12-31").with(TemporalAdjusters.lastInMonth(DayOfWeek.SUNDAY));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值