java8有很多时间上的新api,在操作时间的时候很好用,这儿算是个备忘录吧,(补充中。。。)

定位某个时间:of方法

LocalDateTime dateTime = LocalDateTime.of(2020, 2, 11, 13, 15, 12);
LocalDate date = LocalDate.of(2020, 2, 11);

计算两个时间的差值(day,hour,minute等)

LocalDateTime dateTime1 = LocalDateTime.of(2020, 2, 11, 13, 15, 12);
LocalDateTime dateTime2 = LocalDateTime.of(2020, 3, 11, 13, 15, 12);
Duration between = Duration.between(dateTime1, dateTime2);
System.out.println(between.toDays());
System.out.println(between.toHours());
System.out.println(between.toMinutes());

对某个时间进行增加或者减少

LocalDateTime dateTime = LocalDateTime.of(2020, 2, 11, 13, 15, 12);
LocalDateTime t1 = dateTime.plusDays(1);
System.out.println(t1);
LocalDateTime t2 = dateTime.plusMonths(1);
System.out.println(t2);

获取某个时间是否为闰年

  

LocalDateTime dateTime = LocalDateTime.of(2020, 2, 11, 13, 15, 12);
boolean leapYear = dateTime.toLocalDate().isLeapYear();
System.out.println(leapYear);
boolean leap = Year.of(2016).isLeap();

获取某月第一天,最后一天以及一共有多少天,以及当前月是第几月

LocalDateTime dateTime = LocalDateTime.of(2020, 2, 11, 13, 15, 12);
YearMonth month = YearMonth.from(dateTime);
LocalDate begin = month.atDay(1);
int length = month.lengthOfMonth();
LocalDate end = month.atEndOfMonth();
int monthValue = month.getMonthValue();

格式化日期

LocalDateTime dateTime = LocalDateTime.of(2020, 2, 11, 13, 15, 12);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
String r = dateTime.format(formatter);
TemporalAccessor parse = formatter.parse(r);
LocalDate from = LocalDate.from(parse);

java8中的常用日期操作的更多相关文章

  1. 大数据(5) - HDFS中的常用API操作

    一.安装java 二.IntelliJ IDEA(2018)安装和破解与初期配置 参考链接 1.进入官网下载IntelliJ IDEA https://www.jetbrains.com/idea/d ...

  2. Java常用日期操作

    对java中常用的日期操作进行整理. 1.日期格式化 /* * 日期格式化类(必须掌握) * API: * G Era 标志符 Text AD y 年 Year 1996; 96 M 年中的月份 Mo ...

  3. 【转载】java8中的Calendar日期对象(LocalDateTime)

    Java 8 推出了全新的日期时间API,Java 8 下的 java.time包下的所有类都是不可变类型而且线程安全. 下面是新版API中java.time包里的一些关键类: Instant:瞬时实 ...

  4. python中 列表常用的操作

    列表可以装大量的数据,不限制数据类型,表示方式:[]:列表中的元素用逗号隔开. lst = [] #定义一个空列表 lst = ["Tanxu",18,"女", ...

  5. 在MYSQL中插入当前时间,就象SQLSERVER的GETDATE()一样,以及对mysql中的时间日期操作。

    在看sql教程的时候,我学的是mysql,但是教程上面的一点在mysql里面是不支持的,所以就找了其他的替代的办法 sql教程上面是这样的: 通过使用类似 GETDATE() 这样的函数,DEFAUL ...

  6. ajax 中一些常用的操作

    $.post.$.get是一些简单的方法,如果要处理复杂的逻辑,还是需要用到jQuery.ajax() 一.$.ajax的一般格式$.ajax({ type: 'POST', url: url , d ...

  7. iOS中NSDate常用转换操作整合

    //当前时间格式化, 例:YYYY-MM-dd-EEEE-HH:mm:ss + (NSString *)getCurrentDataWithDateFormate:(NSString *)format ...

  8. MYSQL常用日期操作

    mysql查询今天.昨天.7天.近30天.本月.上一月 数据 今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT ...

  9. C#中关于常用日期的计算

    void Main() { DateTime dt = DateTime.Now; //当前时间 DateTime startWeek = dt.AddDays( - Convert.ToInt32( ...

随机推荐

  1. LED Decorative Light Supplier - LED Neon Application: 5 Advantages

    In the past 100 years, lighting has gone a long way. LED decorative lighting is now designed to meet ...

  2. Paper: A novel visibility graph transformation of time series into weighted networks

    1. Convert time series into weighted networks. 2. link prediction is used to evaluate the performanc ...

  3. Oracle查询当前用户和当前用户下的所有表

    转载自:http://blog.itpub.net/29485627/viewspace-1246317/ Oracle查询当前用户和当前用户下的所有表 (1)查询当前用户 SQL> show ...

  4. 正则表达式过滤url请求

    过滤url中带reset的url请求 atgBusSignFilter.setSignUriRegex("^.*/reset/.*$")等价于 atgBusSignFilter.s ...

  5. CentOS 7防火墙快速开放端口配置方法

    CentOS升级到7之后,发现无法使用iptables控制Linuxs的端口,baidu之后发现Centos 7使用firewalld代替了原来的iptables.下面记录如何使用firewalld开 ...

  6. Python - python3.7新增的contextvars vs Thread local(threading.local)

    总结 和threading.local()类似.Python3.7新增. thread.local(): 不同线程,同一个变量保存不同的值. contextvars: 不同上下文,同一个变量保存不同的 ...

  7. Bootstrap框架学习

    Bootstrap框架个人总结 https://blog.csdn.net/To_Front_End/article/details/51142716 Bootstrap 教程 https://www ...

  8. ubuntu---yolo报错darknet: ./src/cuda.c:36: check_error: Assertion `0' failed.

    装好darknet后,直接测试的时候,报错: darknet: ./src/cuda.c:36: check_error: Assertion `0' failed.解决办法是打开yolov3.cfg ...

  9. rancher三节点k8s集群部署例子

    rancher三节点k8s集群部署例子 待办 https://rorschachchan.github.io/2019/07/25/使用Rancher2-1部署k8s/

  10. IIS7.x经典模式与集成模式

    参考文档:http://book.51cto.com/art/200908/146143.htm 个人理解: 经典模式: 在IIS6中aspnet_isapi.dll只是ISAPI的一个实现,对asp ...