java8日期类LocalDate,LocalTime和LocalDateTime的基本操作

  至于为什么使用java8日期类以及Date的缺点等这里不做多解释, 直接上代码, 代码中都是日常项目中最可能用到的一些基本操作

 1.LocalDate类

package demo.java.jiangkd.java8.localdate;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;

import org.junit.Test;

import lombok.extern.slf4j.Slf4j;

/**
 * LocalDate
* LocalDate只会获取年月日 * *
@author jiangkd * @date 2020/06/05 */ @Slf4j public class LocalDateDemo { /** * 创建LocalDate */ @Test public void createLocalDateTest() { // 获取当前年月日 LocalDate now = LocalDate.now(); log("now", now); // 打印格式: yyyy-MM-dd, 例如: 2020-06-05 // 构造指定的年月日 now = LocalDate.of(2020, Month.JUNE, 5); log("now", now); // 2020-06-05 } /** * 获取年、月、日、星期几 的几种方式 */ @Test public void getDateTest() { // LocalDate now = LocalDate.now(); // int year = now.getYear(); log("year", year); year = now.get(ChronoField.YEAR); log("year", year); // 2020 // 月 , 1-12表示相应的月份 Month month = now.getMonth(); log("month", month); log("month", month.getValue()); int monthValue = now.getMonthValue(); log("month", monthValue); int month2 = now.get(ChronoField.MONTH_OF_YEAR); log("month2", month2); // 6 // int dayOfMonth = now.getDayOfMonth(); log("dayOfMonth", dayOfMonth); int day = now.get(ChronoField.DAY_OF_MONTH); log("day", day); // 5 // 星期, 1-7对应星期一星期日 DayOfWeek dayOfWeek = now.getDayOfWeek(); log("dayOfWeek", dayOfWeek); log("dayOfWeek", dayOfWeek.getValue()); int week = now.get(ChronoField.DAY_OF_WEEK); log("week", week); // 5 } /** * 对日期进行加减操作 */ @Test public void plusOrMinusTest() { // LocalDate now = LocalDate.now(); log("now", now); // 2020-06-05 // 对年 + 1 now = now.plusYears(1); log("now", now); // 2021-06-05 // 对月+2 now = now.plusMonths(2); log("now", now); // 2021-08-05 // 对月在加5, 等于13, 但是只有12个月, 会对年进行加1 now = now.plusMonths(5); log("now", now); // 2022-01-05 // 重置now对象, 6月30天, 测试日加上30天 now = LocalDate.now(); log("now", now); // 2020-06-05 // now = now.plusDays(30); log("now", now); // 2020-07-05 // 对日期的修改还可以使用下面的方法 now = LocalDate.now(); // log("now", now); // 2020-06-05 now = now.plus(30, ChronoUnit.DAYS).minus(2, ChronoUnit.MONTHS); log("now", now); // 2020-05-05 // 相应的减操作 now.minus和plus类似, 不做赘述 } /** * 日期格式化 */ @Test public void localDateFormatterTest() { // LocalDate now = LocalDate.now(); log("now", now); // 2020-06-05 // String format = now.format(DateTimeFormatter.ofPattern("yyyy/MM/dd")); log("format", format); // 2020/06/05 } private void log(String info, Object obj) { log.info("{} -> {}", info, obj); } }

   2.LocalTime类

package demo.java.jiangkd.java8.localdate;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;

import org.junit.Test;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class LocalTimeDemo {

    /**
     * 创建LocalDateTime
     */
    @Test
    public void createLocalTimeTest() {
        //
        LocalTime now = LocalTime.now();
        log("now", now); // 09:09:18.450
        // 构建LocalTime, 参数分别是 时,分,秒
        now = LocalTime.of(9, 10, 30);
        log("now", now); // 09:10:30
    }

    /**
     * 获取时分秒
     */
    @Test
    public void getTimeTest() {
        //
        LocalTime now = LocalTime.now();
        //
        int hour = now.getHour();
        log("hour", hour);
        hour = now.get(ChronoField.HOUR_OF_DAY);
        log("hour", hour);
        //
        int minute = now.getMinute();
        log("minute", minute);
        minute = now.get(ChronoField.MINUTE_OF_HOUR);
        log("minute", minute);
        //
        int second = now.getSecond();
        log("second", second);
        second = now.get(ChronoField.SECOND_OF_MINUTE);
        log("second", second);
    }

    @Test
    public void localTimeFormatterTest() {
        //
        LocalTime now = LocalTime.now();
        log("now", now);
        //
        String format = now.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
        log("format", format); // 09-58-10
        format = now.format(DateTimeFormatter.ofPattern("HH:mm:ss,SSS"));
        log("format", format); // 09-58-10,912
    }

    private void log(String info, Object obj) {
        log.info("{} -> {}", info, obj);
    }
}

  3.LocalDateTime类

package demo.java.jiangkd.java8.localdate;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

import org.junit.Test;

import lombok.extern.slf4j.Slf4j;

/**
 * LocalDateTime,获取年月日时分秒,等于LocalDate+LocalTime
 * 
 * @author jiangkd
 * @date 2020/06/05
 */
@Slf4j
public class LocalDateTimeDemo {

    /**
     * 创建LocalDateTime
     */
    @Test
    public void createLocalDateTimeTest() {
        //
        LocalDateTime now = LocalDateTime.now();
        log("now", now); // now -> 2020-06-05T09:28:39.095
        now = LocalDateTime.of(LocalDate.now(), LocalTime.now());
        log("now", now); // now -> 2020-06-05T09:28:39.112
    }

    /**
     * 获取LocalDate和LocalTime
     */
    @Test
    public void getLocalDateOrTimeTest() {
        //
        LocalDateTime now = LocalDateTime.now();
        // 获取LocalDate
        LocalDate localDate = now.toLocalDate();
        log("localDate", localDate); // 2020-06-05
        // 获取LocalTime
        LocalTime localTime = now.toLocalTime();
        log("localTime", localTime); // 09:30:53.802
    }

    /**
     * 格式化
     */
    @Test
    public void localDateTimeFormatterTest() {
        //
        LocalDateTime now = LocalDateTime.now();
        log("now", now); // 2020-06-05T10:01:05.556
        //
        String format = now.format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH-mm-ss"));
        log("format", format); // 2020/06/05 10-01-05
    }

    private void log(String info, Object obj) {
        log.info("{} -> {}", info, obj);
    }
}

 

你可能感兴趣的:(java8日期类LocalDate,LocalTime和LocalDateTime的基本操作)