Java8中处理日期时间

首先看一个对比

public class LocalDateDemo {

    public static void main(String[] args) {
               
        //对比
        Calendar cal = Calendar.getInstance();
        System.out.println(cal.get(Calendar.YEAR));
        System.out.println(cal.get(Calendar.MONTH)); // 0 - 11
        System.out.println(cal.get(Calendar.DATE));
        System.out.println(cal.get(Calendar.HOUR_OF_DAY));
        System.out.println(cal.get(Calendar.MINUTE));
        System.out.println(cal.get(Calendar.SECOND));
        // Java 8
        LocalDateTime dt = LocalDateTime.now();
        System.out.println(dt.getYear());
        System.out.println(dt.getMonthValue()); // 1 - 12
        System.out.println(dt.getDayOfMonth());
        System.out.println(dt.getHour());
        System.out.println(dt.getMinute());
        System.out.println(dt.getSecond());
    }

输出结果:

2020
0
7
14
51
44
2020
1
7
14
51
44

Java8中LocalDate的部分使用

/**
 * @program: java8_demo
 * @description: java8中处理日期和时间
 * @author: XZQ
 * @create: 2020-01-07 14:37
 **/
public class LocalDateDemo {

    public static void main(String[] args) {
        LocalDate now = LocalDate.now();//当前日期
        System.out.println(now);//2020-01-07
        System.out.println(now.getYear());//获取年份 2020
        System.out.println(now.getMonthValue());//获取月份值 1
        System.out.println(now.getMonth());//获取月份 JANUARY
        System.out.println(now.getDayOfMonth());//获取日期 7


        LocalDate date = LocalDate.of(2018, 06, 20);//处理特定日期
        System.out.println(date);//2018-06-20


        System.out.println("----------------------");
        LocalTime localTime = LocalTime.now();//获取当前之间 只包含时间信息 无日期
        System.out.println(localTime);//14:45:24.562016700


        System.out.println("----------------------");
        LocalTime localTime1 = LocalTime.now();
        System.out.println("当前时间" + localTime);//当前时间14:45:24.562016700
        LocalTime localTime2 = localTime.plusHours(2);//增加2小时
        System.out.println("当前时间增加两小时" + localTime1);//当前时间增加两小时14:45:24.562016700


        LocalDate plusDate = now.plus(1, ChronoUnit.WEEKS);//一周后的日期
        System.out.println(plusDate);//2020-01-14


        LocalDate minusDate = now.minus(1, ChronoUnit.YEARS);//一年前的日期
        LocalDate plusDate1 = now.plus(1, ChronoUnit.YEARS);//一年后的日期
        System.out.println(minusDate);//2019-01-07
        System.out.println(plusDate1);//2021-01-07

        
        //Java 判断日期是早于还是晚于另一个日期
        LocalDate tomorrow = LocalDate.of(2018, 6, 20);
        if (tomorrow.isAfter(now)) {
            System.out.println("Tomorrow comes after today");
        }
        LocalDate yesterday = now.minus(1, ChronoUnit.DAYS);
        if (yesterday.isBefore(now)) {
            System.out.println("Yesterday is day before today");
        }
    }
}

输出结果:


2020-01-07
2020
1
JANUARY
7
2018-06-20
----------------------
14:56:16.466634900
----------------------
当前时间14:56:16.466634900
当前时间增加两小时14:56:16.466634900
2020-01-14
2019-01-07
2021-01-07
Yesterday is day before today
发布了24 篇原创文章 · 获赞 2 · 访问量 414

猜你喜欢

转载自blog.csdn.net/qq_42107430/article/details/103874601