LocalDate的用法与String互转

一、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互转

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"));

二、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);
    }
文章知识点与官方知识档案匹配,可进一步学习相关知识
————————————————
版权声明:本文为CSDN博主「月渐盈」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_34471241/article/details/121104081

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将String类型换为LocalDate类型,可以使用Java 8中的java.time.LocalDate类和java.time.format.DateTimeFormatter类。以下是一个示例代码: ```java import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class StringToLocalDate { public static void main(String\[\] args) { String dateStr = "2019-06-13"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); LocalDate localDate = LocalDate.parse(dateStr, formatter); System.out.println(localDate); } } ``` 在这个示例中,我们首先定义了一个String类型的日期字符串`dateStr`,然后创建了一个DateTimeFormatter对象`formatter`,指定了日期字符串的格式。接下来,我们使用`LocalDate.parse()`方法将日期字符串换为LocalDate类型的对象`localDate`,并使用指定的格式进行解析。最后,我们打印出换后的LocalDate对象。 请注意,这个示例中使用的日期格式是"yyyy-MM-dd",你可以根据实际情况调整格式。 #### 引用[.reference_title] - *1* *3* [LocalDate用法String互转](https://blog.csdn.net/qq_34471241/article/details/121104081)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v12^insert_chatgpt"}} ] [.reference_item] - *2* [String类型LocalDate类型](https://blog.csdn.net/weixin_42209440/article/details/91769582)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v12^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值