java8中的日期转换

Posted by t298 on April 6, 2022

java中的一些日期转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.zzxsoft.cityone.currency.mtmanuscript.controller;

import java.text.ParseException;
import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

public class test {
    public static void main(String[] args) throws ParseException {
        String times = "2022-04-02";
        LocalDate time = LocalDate.parse(times, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        //季度
        int quarter = (time.getMonthValue() - 1) / 3 + 1;
        Month month = time.getMonth();
        Month firstMonthOfQuarter = month.firstMonthOfQuarter();
        Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);

        System.out.println();
        // 当前时间
        System.out.println("当前时间  " + time);
        // 当前季度
        System.out.println("当前季度  " + quarter);
        // 当前季度开始时间
        System.out.println("当前季度开始时间  " + LocalDate.of(time.getYear(), firstMonthOfQuarter, 1));
        // 当前季度结束时间
        System.out.println("当前季度结束时间  " + LocalDate.of(time.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(time.isLeapYear())));
        //当前月的第一天
        System.out.println("当前月的第一天  " + time.with(TemporalAdjusters.firstDayOfMonth()));
        // 当前月的最后一天
        System.out.println("当前月的最后一天  " + time.with(TemporalAdjusters.lastDayOfMonth()));
        // 一年的第一天
        System.out.println("一年的第一天  " + time.with(TemporalAdjusters.firstDayOfYear()));
        // 一年的最后一天
        System.out.println("一年的最后一天  " + time.with(TemporalAdjusters.lastDayOfYear()));
    }
}