Java8六大新特性之六 新日期时间类型

LocalDate 的基本使用实例
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;

public class NewDateOperateTest {
    public static void main(String[] args) {

        //获取今天
        LocalDate today = LocalDate.now();
        System.out.println(today);

        //获取固定某一天
        LocalDate d1=  LocalDate.of(2020,1,2);
        System.out.println(d1);

        //获取固定某一天
        LocalDate d2 = LocalDate.parse("2020-11-11");
        System.out.println(d2);

        //获取某个月的第一天
        LocalDate d3 = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
        System.out.println(d3);

        //某个月的第三天
        LocalDate d4 = LocalDate.now().withDayOfMonth(3);
        System.out.println(d4);

        //某个月的最后一天
        LocalDate d5 = today.with(TemporalAdjusters.lastDayOfMonth());
        System.out.println(d5);

        //给某个时间加两天
        LocalDate d6 = LocalDate.now().plusDays(2);
        System.out.println(d6);

        //取2020年10月底一个周三
        LocalDate d7 = LocalDate.parse("2020-10-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.WEDNESDAY));
        System.out.println(d7);

    }
}

执行结果:

2020-11-15
2020-01-02
2020-11-11
2020-11-01
2020-11-03
2020-11-30
2020-11-17
2020-10-07

Process finished with exit code 0
LocalTime 基本使用
指定时间,时分秒,获取时分秒
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

public class NewTimeOperateTest {
    public static void main(String[] args) {

        //时间类型带纳秒
        LocalTime t1 = LocalTime.now();
        System.out.println(t1);
        //时间类型不带纳秒
        LocalTime t2 = LocalTime.now().withNano(0);
        System.out.println(t2);
        //时间类型重写分钟
        LocalTime t4 = LocalTime.now().withMinute(20).withNano(0);
        System.out.println(t4);
        //字符串指定时间
        LocalTime t5 = LocalTime.parse("22:22:22");
        System.out.println(t5);
        //获取日期时间类型
        LocalDateTime dt = LocalDateTime.now();
        System.out.println(dt);
    }
}

执行结果:

18:43:16.918
18:43:16
18:20:16
22:22:22
LocalDateTime的基本使用

时间的加减,时间的时区转换,时间的format,时间的间隔计算

 package allen.java.JDK18;

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

public class NewDateTimeOperateTest {
     public static void main(String[] args) {

        //获取日期时间类型
        LocalDateTime dt = LocalDateTime.now();
        System.out.println(dt);

        //对时间进行时区转换
        String dt1 = LocalDateTime.now().toInstant(ZoneOffset.of("-5")).toString();
        System.out.println(dt1);

        //用百万毫秒进行时区转换,对世界就行格式化
        long time = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss:SSS");
        String dfd = df.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.of("Asia/Shanghai")));
        System.out.println(dfd);

        //格式化时间的不同方式
        DateTimeFormatter df1 = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        String d1 = LocalDateTime.now().format(df1);
        String d2 = df1.format(LocalDateTime.now());
        System.out.println(d1);
        System.out.println(d2);

        //计算时间间隔
        Instant ins = Instant.now();
        Instant ins1 = Instant.now().plusSeconds(10);
        Long duration = Duration.between(ins,ins1).toMillis();
        System.out.println(duration);

        //计算日期间隔
        LocalDate localDate = LocalDate.of(2018,12,8);
        LocalDate localDate1 = LocalDate.now();
        Period p = Period.between(localDate,localDate1);
        System.out.println("相隔"+ p.getYears() + "年" +p.getMonths() + "月"+p.getDays()+"天");

        //获取下个周六
        LocalDateTime localDateTime = LocalDateTime.now();
        localDateTime.with(TemporalAdjusters.next(DayOfWeek.SATURDAY));

    }
}

执行结果:

2020-11-15T18:43:16.958
2020-11-15T23:43:16.958Z
2020-11-15 18:43:16:959
2020年11月15日
2020年11月15日
10000
相隔1年11月7天

猜你喜欢

转载自blog.csdn.net/pengweismile/article/details/109707538