LocalDate、LocalDateTime的用法与String互转

发布时间 2023-09-14 10:06:23作者: 花花_新世界

一、LocalDate常用用法

1.1、申明定义

LocalDate formatDate = LocalDate.of(2020, 2, 5); // 自定义
LocalDate today = LocalDate.now(); // 获取当前日期

1.2、getX() 获取年月日等

注意:获取月份使用getMonthValue()

System.out.println(formatDate.getMonth()); // FEBRUARY 获取所在月份(英文)
System.out.println(formatDate.getMonthValue()); // 2 获取所在月份(英文)
System.out.println(formatDate.getDayOfMonth()); // 5 获取所在天

1.3、plusX()、minusX()

LocalDate nextDay = formatDate.plusDays(1);
System.out.println(nextDay); // 2020-02-06 明天
LocalDate nextWeek = formatDate.plusWeeks(1);
System.out.println(nextWeek); // 2020-02-12 下周当天
LocalDate lastMonth = formatDate.minusMonths(1);
System.out.println(lastMonth); // 2020-01-05 上月当天

1.4、扩展用法

常用于报表中计算同比环比等日期

// 同环比日期计算
LocalDate firstWeekDay = formatDate.with(TemporalAdjusters.previous(DayOfWeek.MONDAY));
System.out.println(firstWeekDay); // 2020-02-03 本周第一天
LocalDate firstMonthDay = formatDate.with(TemporalAdjusters.firstDayOfMonth());
System.out.println(firstMonthDay); // 2020-02-01 本月第一天
LocalDate firstYearDay = formatDate.with(TemporalAdjusters.firstDayOfYear());
System.out.println(firstYearDay); // 2020-01-01 本年第一天
// 判断两个日期前后
boolean param = formatDate.isBefore(today);
System.out.println(param); // true 判断a是否早于b
// 计算两个日期的间隔天数
LocalDate start = LocalDate.parse("2019-12-01");
LocalDate end = LocalDate.parse("2020-02-05");
long days = start.until(end, ChronoUnit.DAYS);
System.out.println("days: " + days); // days: 66

二、LocalDate与String互转

2.1、LocalDate转String

DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate formatDate = LocalDate.of(2020, 2, 5);
String dateStr = formatDate.format(df);
System.out.println("LocalDate => String: " + dateStr);

2.2、String转LocalDate

DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate dateParam = LocalDate.parse(dateStr, df);
System.out.println("String => LocalDate: " + dateParam);

2.3 固定格式输出时间字符串

    /**
     * 时间截取--日
     */
    private String timeSubStringDay(LocalDateTime sendTime) {
        String localDateTimeStr = sendTime.format(DateTimeFormatter.ofPattern("MM-dd     HH:mm"));
        return localDateTimeStr;
    }

三、代码实践

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
 
/**
 * LocalDate日期转换
 *
 * @author: Tansj
 * @since: 2020/02/08
 */
public class Test {
 
    public static void main(String[] args) {
        LocalDate formatDate = LocalDate.of(2020, 2, 5);
        LocalDate today = LocalDate.now();
        System.out.println(formatDate); // 2020-02-05 当天
        System.out.println(today); // 2020-02-08 本日当天
        boolean param = formatDate.isBefore(today);
        System.out.println(param); // true 判断a是否早于b
        System.out.println("=========================================");
        System.out.println(formatDate.getMonth()); // FEBRUARY 获取所在月份(英文)
        System.out.println(formatDate.getMonthValue()); // 2 获取所在月份(数字)
        System.out.println(formatDate.getDayOfMonth()); // 5 获取所在天
        System.out.println("=========================================");
        LocalDate nextDay = formatDate.plusDays(1);
        System.out.println(nextDay); // 2020-02-06 明天
        LocalDate nextWeek = formatDate.plusWeeks(1);
        System.out.println(nextWeek); // 2020-02-12 下周当天
        LocalDate lastMonth = formatDate.minusMonths(1);
        System.out.println(lastMonth); // 2020-01-05 上月当天
        System.out.println("=========================================");
        LocalDate firstWeekDay = formatDate.with(TemporalAdjusters.previous(DayOfWeek.MONDAY));
        System.out.println(firstWeekDay); // 2020-02-03 本周第一天
        LocalDate firstMonthDay = formatDate.with(TemporalAdjusters.firstDayOfMonth());
        System.out.println(firstMonthDay); // 2020-02-01 本月第一天
        LocalDate firstYearDay = formatDate.with(TemporalAdjusters.firstDayOfYear());
        System.out.println(firstYearDay); // 2020-01-01 本年第一天
        System.out.println("=========================================");
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String dateStr = formatDate.format(df);
        System.out.println("LocalDate => String: " + dateStr); // LocalDate => String: 2020-02-05
        LocalDate dateParam = LocalDate.parse(dateStr, df);
        System.out.println("String => LocalDate: " + dateParam); // String => LocalDate: 2020-02-05
        LocalDate start = LocalDate.parse("2019-12-01");
        LocalDate end = LocalDate.parse("2020-02-05");
        long days = start.until(end, ChronoUnit.DAYS);
        System.out.println("days: " + days); // days: 66
    }
}

获取日期中的字符串 如"02-03"

String time = hydrologyVO.getSendTime().format(DateTimeFormatter.ofPattern("MM-dd"));

四、LocalDateTime与String、Long互转

2.1、LocalDateTime与String互转

LocalDateTime localDateTime=LocalDateTime.parse(dates,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
String localDateTimeStr=LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

2.2、LocalDateTime与Long互转

Long localDateTimeLong=Timestamp.valueOf(LocalDateTime.now()).getTime();
LocalDateTime localDateTimeLongTime=LocalDateTime.ofInstant(Instant.ofEpochMilli(localDateTimeLong)

示例:

 
public static void main(String[] args) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.now();
        String dateStr = localDateTime.format(fmt);
        System.out.println(dateStr);
    }
原文链接:https://blog.csdn.net/qq_34471241/article/details/121104081