Java8常用时间方法(LocalDateTime操作)

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;

public class java8Time {
    public static void main(String[] args) {
        //现在时间
        System.out.println("----------------现在时间------------------");
        System.out.println(LocalDateTime.now());
        //格式化
        System.out.println("----------------格式化------------------");
        System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        //减10s
        System.out.println("-----------------减10s-----------------");
        System.out.println(LocalDateTime.now().minusSeconds(10).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        //加10s
        System.out.println("------------------加10s----------------");
        System.out.println(LocalDateTime.now().minusSeconds(-10).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        //减10个小时
        System.out.println("------------------减10个小时----------------");
        System.out.println(LocalDateTime.now().minusHours(10).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        //减10分钟
        System.out.println("------------------减10分钟----------------");
        System.out.println(LocalDateTime.now().minusMinutes(10).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        //减两周
        System.out.println("------------------减两周----------------");
        System.out.println(LocalDateTime.now().minusWeeks(2).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        //减两年
        System.out.println("------------------减两年----------------");
        System.out.println(LocalDateTime.now().minusYears(2).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        //构建一个时间
        System.out.println("------------------构建一个时间----------------");
        System.out.println(LocalDateTime.of(2021, 1, 1, 1, 1, 1, 1));

        //加10s
        System.out.println("-----------------加10s-----------------");
        System.out.println(LocalDateTime.now().plusSeconds(10).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        //加10个小时
        System.out.println("------------------加10个小时----------------");
        System.out.println(LocalDateTime.now().plusHours(10).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        //加10分钟
        System.out.println("------------------加10分钟----------------");
        System.out.println(LocalDateTime.now().plusMinutes(10).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        //加两周
        System.out.println("------------------加两周----------------");
        System.out.println(LocalDateTime.now().plusWeeks(2).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        //加两年
        System.out.println("------------------加两年----------------");
        System.out.println(LocalDateTime.now().plusYears(2).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        //修改小时
        System.out.println("------------------修改小时--------------------");
        System.out.println(LocalDateTime.now().withHour(20));

        //修改年份
        System.out.println("------------------修改年份--------------------");
        System.out.println(LocalDateTime.now().withYear(2018));
        //时间比较
        System.out.println("---------------------时间比较----Duration----------------");
        //毫秒
        System.out.println(Duration.between(LocalDateTime.now(), LocalDateTime.now().plusSeconds(360000)).toMillis());
        //分钟
        System.out.println(Duration.between(LocalDateTime.now(), LocalDateTime.now().plusSeconds(360000)).toMinutes());
        //小时
        System.out.println(Duration.between(LocalDateTime.now(), LocalDateTime.now().plusSeconds(360000)).toHours());
        //天
        System.out.println(Duration.between(LocalDateTime.now(), LocalDateTime.now().plusSeconds(360000)).toDays());


        System.out.println("-------------------------Period-------------------------");
        LocalDate localDate = LocalDate.of(2021, 1, 1);
        //毫秒
        System.out.println(Period.between(localDate, localDate.plusDays(2)).getDays());
        System.out.println(Period.between(localDate, localDate.plusDays(2)).getMonths());
        System.out.println(Period.between(localDate, localDate.plusDays(2)).getYears());

        System.out.println("---------------判断两个时间前后----------------------");
        LocalDateTime a = LocalDateTime.of(2012, 6, 30, 12, 00);
        LocalDateTime b = LocalDateTime.of(2012, 7, 1, 12, 00);
        boolean after = a.isAfter(b);
        boolean before = a.isBefore(b);
        System.out.println(after);
        System.out.println(before);


    }

}

你可能感兴趣的:(数据库,服务器,linux)