一、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是否早于bSystem.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-05LocalDate dateParam = LocalDate.parse(dateStr, df);System.out.println("String => LocalDate: " + dateParam); // String => LocalDate: 2020-02-05LocalDate 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);}

LocalDate的用法与String互转相关推荐

  1. C# IntPtr 与 string互转

    一.IntPtr 与 string互转 string str = "aa"; IntPtr init = Marshal.StringToHGlobalAnsi(str); str ...

  2. Java8 LocalDateTime获取时间戳(毫秒/秒)、LocalDateTime与String互转、Date与LocalDateTime互转

    本文收录在猪哥GitHub:https://github.com/pig6/Java 中,本项目收集一线大厂面试.实战.Java学习路线等. 本文目前提供:LocalDateTime获取时间戳(毫秒/ ...

  3. js对象(Object)和字符串(String)互转 - 代码篇

    文章目录 js对象(Object)和字符串(String):相互转化 1. 利用原生JSON对象,将对象转为字符串 截图如下: 2. 从JSON字符串转为对象 相关文章:[localstorage本地 ...

  4. xml,String互转

    看文章底部字符串转xml对象  一.互转 package com.tcwl.vsmp.mortgage.utils; import java.io.ByteArrayInputStream; impo ...

  5. java byte数组与String互转

    java byte数组与String互转 CreationTime--2018年7月6日14点53分 Author:Marydon 1.String-->byte[] 方法:使用String.g ...

  6. Oracle数据库Clob类型Blob类型与String互转

    Clob类型为大字段类型默认4000长度,Blob为二进制类型常用存文件数据:这两种大字段类型开发中不会经常遇到,博主整理了这两种大字段与String的转换方法可参考(使用Junit直接测试)其中ja ...

  7. byte[]与String互转对象和JSON相互转

    byte[]与String互转 // String转byte[]String str = "110";byte[] src = str.getBytes();// byte[]转S ...

  8. Set和String互转

    Set和String互转 public static String addJgqx(String jgid, String jgqx) {HashSet<String> names = S ...

  9. java format用法_Java String format() 方法

    例如: String 类的 format 方法可以格式化日期和时间import java.util.Date; import java.util.Locale; public class Main { ...

最新文章

  1. 将三维点云投影到XOZ面上
  2. html中输入框的创建
  3. 【BZOJ-13962865】识别子串字符串识别 后缀自动机/后缀树组 + 线段树
  4. ActiveMQ的集群与高可用
  5. spark学习:java版JavaRDD与JavaPairRDD的互相转换
  6. 【bzoj2406】矩阵 二分+有上下界可行流
  7. nuget包管理器控制台下的powershell脚本介绍
  8. hdu Robberies 2955 01背包
  9. tensorflow 2.X中构建模型的三种方式:Sequential, Functional, Subclassing
  10. 125KHz 100cm ID 读卡电路_NX系列PLC-NX-ID数字输入单元_欧姆龙继电器_欧姆龙PLC_欧姆龙接近开关...
  11. Java 给PDF文件添加水印
  12. scss exceeded maximum budget. Budget 4.00 kB was not met by 130 bytes with a total of 4.13 kB.
  13. L1-030——一帮一学习小组”是中小学中常见的学习组织方式,老师把学习成绩靠前的学生跟学习成绩靠后的学生排在一组。
  14. 配置DeepStreaks环境
  15. opencv 提取彩色图像轮廓
  16. 数字化时代,企业应该如何看待商业智能BI
  17. SpringCould整合oauth2
  18. QQ个性域名邮箱(免费企业邮箱)快速申请
  19. Mybatis-Plus 条件构造器Wrapper常用方法
  20. linux安装intel驱动

热门文章

  1. oracle数据迁移到mysql
  2. 全球及中国呼叫中心服务行业运营模式及发展战略分析报告2022-2028年
  3. Batch Normalize的几点说明
  4. 《机器学习实战》支持向量机(手稿+代码)
  5. python实现长截图_用 Python 实现长截图拼接
  6. MATLAB生成exe脱离matlab运行可执行程序
  7. Weex实战分享|Weex在极客时间APP中的实践
  8. Axure幻灯片案例
  9. Gitlab本地备份与远程机备份
  10. HUST-多媒体基础PPT目录