关于LocalDate、LocalTime、LocalDateTime
0.快速上手
看源码: https://www.matools.com/api/java8 在java.time中
//获取当前时间
LocalDateTime localDateTime = LocalDateTime.now();//想要设置当前时间为12点0分0秒(要记住结果不是修改之前的,而是重新生成的)
LocalDateTime localDateTime2 = LocalDateTime.now().withHour(12).withMinute(0).withSecond(0);//打印结果(看样子还要设置毫秒,自己试试吧!)
2022-10-20T12:00:00.703
1.前言
我们通常使用Date ,Calendar ,GregoiranCalendar ,SimpleDateFormat 来分别处理
日期、日历、公历、或者将日期时间格式化;
但它们四个都是线程不安全的,而且处理很复杂;
在项目开发中,使用这JDK8新特性里提供的3个时间类:
LocalDate、LocalTime、LocalDateTime可以大大简化我们的处理过程。
2.快速了解LocalDate、LocalTime、LocalDateTime
2.1 LocalDate (获取年月日)
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoField;//方式一:获取当前时间LocalDate localDate = LocalDate.now();//方式二:构造指定的年月日LocalDate localDate1 = LocalDate.of(2022, 8, 9);// 获取该时间的年/月/日int year1 = localDate.getYear();Month month1 = localDate.getMonth();//返回月份英文(大写),(Month是枚举类型)int day1 = localDate.getDayOfMonth();// 获取该时间年/月/日int year2 = localDate.get(ChronoField.YEAR);int month2 = localDate.get(ChronoField.MONTH_OF_YEAR);int day2 = localDate.get(ChronoField.DAY_OF_MONTH);// 获取该时间在一周中的所属位置DayOfWeek dayOfWeek1 = localDate.getDayOfWeek();//返回星期的的英文(大写)int dayOfWeek2 = localDate.get(ChronoField.DAY_OF_WEEK);
2.2 LocalTime (获取时分秒)
//创建LocalTimeLocalTime localTime = LocalTime.of(13, 51, 10);LocalTime localTime1 = LocalTime.now();//获取小时
int hour = localTime.getHour();
int hour1 = localTime.get(ChronoField.HOUR_OF_DAY);//获取分
int minute = localTime.getMinute();
int minute1 = localTime.get(ChronoField.MINUTE_OF_HOUR);//获取秒
int second = localTime.getSecond();
int second1 = localTime.get(ChronoField.SECOND_OF_MINUTE);
2.3 LocalDateTime(年月日时分秒)
//获取年月日时分秒,等于LocalDate+LocalTime//创建LocalDateTime
LocalDateTime localDateTime = LocalDateTime.now();LocalDateTime localDateTime1 = LocalDateTime.of(2019, Month.SEPTEMBER, 10, 14, 46, 56);LocalDateTime localDateTime2 = LocalDateTime.of(localDate, localTime);LocalDateTime localDateTime3 = localDate.atTime(localTime);LocalDateTime localDateTime4 = localTime.atDate(localDate);//获取LocalDate
LocalDate localDate2 = localDateTime.toLocalDate();//获取LocalTime
LocalTime localTime2 = localDateTime.toLocalTime();
2.4 Instant(秒,毫秒)
//创建Instant对象
Instant instant = Instant.now();//获取秒数
long currentSecond = instant.getEpochSecond();//获取毫秒数(时间戳)
long currentMilli = instant.toEpochMilli();
3.增减年月日时分秒的方法(plus/minus系列的方法)
使用以下的增加/减少时间的方法,就无需考虑时间不够(加/减)的问题.
比如时间为1月31号,现在增加一天,会变为2月1号.
3.1 增加相关的方法
plusYears(int offset)//:增加指定年份plusMonths(int offset)//:增加指定月份plusWeeks(int offset)//:增加指定周plusDates(int offset)//:增加指定日plusHours(int offset)//:增加指定时plusMinuets(int offset)//:增加指定分plusSeconds(int offset)//:增加指定秒plusNanos(int offset)//:增加指定纳秒
3.2 减少相关的方法
minusYears(int offset)//:减少指定年minusMonths(int offset)//:减少指定月minusWeeks(int offset)//:减少指定周minusDates(int offset)//:减少指定日minusHours(int offset)//:减少指定时minusMinuets(int offset)//:减少指定分minusSeconds(int offset)//:减少指定秒minusNanos(int offset)//:减少指定纳秒
3.3 设置时间(返回这个时间的副本,新对象)
withYear(int year)//设置年withMonth(int month)//设置月withDayOfMonth(int dayOfMonth)//设置日,取值范围(1-31) --设置2月31号会返回异常的哦!withDayOfYear(int dayOfYear)//设置日,取值范围(1-365)withHour(int hour)//设置小时withMinute(int minute)//设置分钟withSecond(int second)//设置秒withNano(int nanoOfSecond)//设置纳秒
3.4代码演示:
public class TimeOfStudy {public static void main(String[] args) {//当前时间为2022-02-14LocalDate localDate = LocalDate.now();System.out.println(localDate);//年int year = localDate.getYear();int year02 = localDate.plusYears(1L).getYear();int year03 = localDate.minusYears(1L).getYear();//打印信息System.out.println(year + "--->year");System.out.println(year02 + "--->year2");System.out.println(year03 + "--->year2");//月Month month = localDate.getMonth();int monthValue = localDate.getMonthValue();int monthValue02 = localDate.plusMonths(1L).getMonthValue();int monthValue03 = localDate.minusMonths(1L).getMonthValue();//打印信息System.out.println(month + "--->");System.out.println(monthValue + "--->monthValue");System.out.println(monthValue02 + "--->monthValue02");System.out.println(monthValue03 + "--->monthValue03");//日DayOfWeek dayOfWeek = localDate.getDayOfWeek();int dayOfMonth = localDate.getDayOfMonth();int dayOfMonth02 = localDate.plusDays(1L).getDayOfMonth();int dayOfMonth03 = localDate.minusDays(1L).getDayOfMonth();int dayOfYear = localDate.getDayOfYear();//打印信息System.out.println(dayOfWeek + "--->dayOfWeek");System.out.println(dayOfMonth + "--->dayOfMonth");System.out.println(dayOfMonth02 + "--->dayOfMonth02");System.out.println(dayOfMonth03 + "--->dayOfMonth03");System.out.println(dayOfYear + "--->dayOfYear");}
}
4.与Data的相互转化
看源码:https://www.matools.com/api/java8
4.1 Date转LocalDateTime
Date date = new Date();//获取系统时区ZoneId zoneId = ZoneId.systemDefault();LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), zoneId);
4.2 LocalDateTime转Date
ZoneId zoneId = ZoneId.systemDefault();LocalDateTime localDateTime = LocalDateTime.now();Date date = Date.from(localDateTime.atZone(zoneId).toInstant());
5.与时间戳的相互转换
ZoneId(时区), ZoneOffset(偏移数据)
5.1: 获取系统时间戳
long l = System.currentTimeMillis();System.out.println("System:: "+l);
5.2: 使用Instant获取时间戳
long l = Instant.now().toEpochMilli();System.out.println("Instant:: "+l);
5.3: 使用LocalDate转换为时间戳
ZoneId zoneId = ZoneId.systemDefault();LocalDate localDate = LocalDate.now();long timestampDay = localDate.atStartOfDay(zoneId).toInstant().toEpochMilli();System.out.println("Day:: "+timestampDay);
5.4: 使用LocalDateTime获取时间戳
LocalDateTime localDateTime = LocalDateTime.now();long timestamp = localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();System.out.println("DateTime:: " +timestamp);
5.5: 时间戳转Date
System.out.println(new Date(放入时间戳));
6 LocalDateTime之间的比较
使用就是A.compareTo(B);时间A大于B返回个正值,反之亦然。
关于LocalDate、LocalTime、LocalDateTime相关推荐
- java年月日时分秒格式_Java 日期时间 LocalDate LocalTime LocalDateTime类
// LoacalDate, LocalTime, LocalDateTime类// 不要使用构造器来构造LocalDate类的对象.实际上,应该使用LocalDate.now()静态工厂方法代表你调 ...
- jdk8中新增的日期处理类LocalDate,LocalTime,LocalDateTime,ZoneId,ZonedDateTime详解
基本概念 时刻 所有计算机系统内部都用一个整数表示时刻,这个整数是距离格林尼治标准时间1970年1月1日0时0分0秒的毫秒数,可以理解时刻就是绝对时间,它与时区无关; 不同时区对同一时刻的解读,即年月 ...
- 时间处理相关类(Calendar,Instant,LocalDate,LocalTime,LocalDateTime)
Calendar Calendar的创建 Calendar calendar=Calendar.getInstance(); 获取当前时间的年,月,日,时,分,秒 System.out.println ...
- java8 时间处理 LocalDate LocalTime LocalDateTime 及毫秒转换
LocalDate 各种和日期的操作,以天为最小单位,支持 日,月,年,星期的增加和减少,计算方便,格式化灵活.没有时分秒. LocalTime 没有日期,只是时间,就是时分秒,毫秒,可以选择不用毫秒 ...
- Java 8日期– LocalDate,LocalDateTime,即时
Java 8 Date Time API is one of the most sought after change for developers. Java has been missing a ...
- mybatis java8_mybatis如何使用Java8的日期LocalDate和LocalDateTime详解
前言 相信大家应该都知道,在实体Entity里面,可以使用java.sql.Date.java.sql.Timestamp.java.util.Date来映射到数据库的date.timestamp.d ...
- Java8 Date API 详解 - LocalDate,LocalDateTime,Instant
转载来源:https://www.journaldev.com/2800/java-8-date-localdate-localdatetime-instant Java8 Date API 详解 - ...
- Java8 ,LocalDate,LocalDateTime处理日期和时间工具类,
Java8 ,LocalDate,LocalDateTime处理日期和时间工具类 日期格式化 1.获取今天的日期 2.在Java 8 中获取年.月.日信息 3.在Java 8 中处理特定日期 4.在J ...
- java8 mysql datetime_在JDBC中使用Java8的日期LocalDate、LocalDateTime
在实体Entity里面,可以使用java.sql.Date.java.sql.Timestamp.java.util.Date来映射到数据库的date.timestamp.datetime等字段 但是 ...
- LocalDate、LocalDateTime互转String
目录 1 LocalDate,String互转 LocalDate转String String转LocalDate 2 LocalDateTime,String互转 LocalDateTime转Str ...
最新文章
- 驰骋工作流引擎JFlow与activiti的对比之4种包含多实例的模式
- jQuery学习笔记一
- 【 Vivado 】使用Vivado设计一个加/减法器的IP核(简洁英文版)
- 帧同步_什么是帧同步什么是状态同步
- nginx mysql设置远程连接超时_nginx中的超时设置
- oracle最新版本是多少_运维日记|关于Oracle的补丁你需要知道的事
- struts2关键配置及函数总结,
- STOLUCK:经济下行的当下 ,STO或将帮助中小企业度过寒冬
- perl python json_Perl解析JSON数据精解
- mysql面试题2020_2020年MySQL数据库面试题
- HDU2026 首字母变大写【入门】
- HWUI(硬件加速绘制UI)简介
- ctr z撤回反向_Ctrl+Z 的反快捷键是什么
- 外壳防护等级/IP防护等级说明
- 支付宝支付demo运行流程
- IE取消或设置代理服务器脚本
- VSCode: Acquiring CodeLLDB platform package 自动加载不成功解决方案
- 10-123 A3-3查找产品表中最低的单价
- 搭建直播源码与软件开发的注意事项 1
- 树莓派驱动6轴传感器MPU6500与SPI初体验(二)