测试类:

import java.time.*;
import java.time.format.DateTimeFormatter; public class App
{
public static void main( String[] args )
{ LocalDateTime time = LocalDateTime.now();
System.out.println(time.toString()); //输出日期时间:2019-05-04T18:27:55.240
System.out.println(time.toLocalDate()); //输出日期:2019-05-04
System.out.println(time.toLocalTime()); //输出时间:18:27:55.240
System.out.println(time.getDayOfMonth()); //输出当前日期月份的第几天:4
System.out.println(time.getDayOfWeek()); //输出档期日期周几:SATURDAY
System.out.println(time.getDayOfYear()); //当前日期在该年属于第几天:124
System.out.println(time.getHour()); //输出:18
System.out.println(time.getMinute()); //输出:27
System.out.println(time.getSecond()); //输出:55
System.out.println(time.getMonthValue()); //输出:5
System.out.println(time.getMonth()); //输出:MAY
System.out.println("=============================================="); //格式化输出:
DateTimeFormatter format = DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss");
System.out.println(time.format(format)); //输出:2019-05-04 18:27:55 //构造时间
LocalDateTime startTime = LocalDateTime.of(2019,05,04,17,59);
System.out.println(startTime.format(format)); //输出:2019-05-04 17:59:00
LocalDateTime endTime = LocalDateTime.of(LocalDate.now(), LocalTime.of(0,0,0));
System.out.println(endTime.format(format)); //输出:2019-05-04 00:00:00 //时间比较
System.out.println(time.isAfter(startTime)); //输出:true
System.out.println(time.isBefore(endTime)); //输出:false //时间运算
System.out.println(time.plusDays(-1).format(format)); //输出:2019-05-03 18:27:55
System.out.println(time.plusDays(1).format(format)); //输出:2019-05-05 18:27:55
System.out.println(time.plusMonths(-1).format(format)); //输出:2019-04-04 18:27:55
System.out.println(time.plusMonths(1).format(format)); //输出:2019-06-04 18:27:55
System.out.println(time.getHour()); //输出:18
System.out.println(time.withHour(1).format(format)); //输出:2019-05-04 01:27:55 //获取毫秒数时间戳
long milliSec = time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
System.out.println(milliSec); //输出:1556965675240
//获取秒数时间戳
long sec = time.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
System.out.println(sec); //输出:1556965675
//时间戳转换为时间
LocalDateTime time2 =LocalDateTime.ofInstant(Instant.ofEpochMilli(milliSec),ZoneId.systemDefault());
System.out.println(time2.format(format)); //输出:2019-05-04 18:27:55
LocalDateTime time3 = LocalDateTime.ofInstant(Instant.ofEpochSecond(sec),ZoneId.systemDefault());
System.out.println(time3.format(format)); //输出:2019-05-04 18:27:55 }
}

输出结果:

2019-05-04T18:27:55.240
2019-05-04
18:27:55.240
4
SATURDAY
124
18
27
55
5
MAY
==============================================
2019-05-04 18:27:55
2019-05-04 17:59:00
2019-05-04 00:00:00
true
false
2019-05-03 18:27:55
2019-05-05 18:27:55
2019-04-04 18:27:55
2019-06-04 18:27:55
18
2019-05-04 01:27:55
1556965675240
1556965675
2019-05-04 18:27:55
2019-05-04 18:27:55

Java8中的日期时间类的更多相关文章

  1. 都9012了,Java8中的日期时间API你还没有掌握?

    一,Java8日期时间API产生的前因后果 1.1 为什么要重新定义一套日期时间API 操作不方便:java中最初的Date不能直接对指定字段进行加减操作也不支持国际化,后来新增了Calendar,但 ...

  2. 【Java8新特性】关于Java8中的日期时间API,你需要掌握这些!!

    写在前面 Java8之前的日期和时间API,存在一些问题,比如:线程安全的问题,跨年的问题等等.这些问题都在Hava8中的日期和时间API中得到了解决,而且Java8中的日期和时间API更加强大.立志 ...

  3. 对Java8新的日期时间类的学习(二)

    示例11 在Java中如何判断某个日期是在另一个日期的前面还是后面 这也是实际项目中常见的一个任务.你怎么判断某个日期是在另一个日期的前面还是后面,或者正好相等呢?在Java 8中,LocalDate ...

  4. 对Java8新的日期时间类的学习(一)

    引用自Java译站http://it.deepinmind.com/java/2015/03/17/20-examples-of-date-and-time-api-from-Java8.html 除 ...

  5. jdk8环境下sprngboot/springmvc中JSR310新日期/时间类LocalDateTime显示效果带T

    如图所示: 日期时间类中带了一个T,以上这种格式LocalDateTime格式化的时候默认日期时间格式:ISO.DATE_TIME(按笔者目前的知识理解是ISO8601规范中的日期时间格式化) 想要把 ...

  6. Java基础——常用类之日期时间类

    如果有机会,请尝试Java8中全新的时间日期API!(参见Java8新特性随笔) 如果还是使用Java7及之前的版本,那么你可以尝试一些工具类(参考使用工具类相关的Hutool-DateUtil) 如 ...

  7. Android中关于日期时间与时区的使用总结

    在开发Android的过程中,出现过几次由于日期时间导致的问题,而且主要是由于时区的原因导致,所以一直想总结一下,形成一个良好的开发规范.   一.Unix时间戳   Unix时间戳(Unix tim ...

  8. 在mysql数据库中关于日期时间字段的处理

    在mysql数据库中关于日期时间字段的处理 在开发中,日期时间字段一般有如下几种设计 假设要获取2013-08-15日到2013-08-16日之间的记录 1. 直接使用日期时间类字段 相关sql语句如 ...

  9. 日期类时间类,日期时间类,单例模式,装箱与拆箱,数字类随机数,BigDecimal总结

    1.日期类,时间类,日期时间类 初步日期使用方法及格式转换方法(旧方法): 格式://Mon Jul 30 11:26:05 CST 2018             年月日时分秒    CST代表北 ...

随机推荐

  1. ffmpeg常用命令-学习

    文章标题:FFmpeg常用命令合集 文章地址:https://blog.csdn.net/lemon_tree12138/article/details/99719520

  2. node gm图片操作

    1,安首先要安装 GraphicsMagick或者ImageMagick 2,npm install gm   --save 3,编码测试 var fs = require('fs') //graph ...

  3. 30.第一个Linq 数据库查询

    使用Linq to Entity查询数据库 首先在项目中添加ADO.NET实体数据模型,如下 新建连接 勾选生成的表 点击完成即可看到两个建立的实体数据对象模型 模型生成好之后就可以直接使用了 Cus ...

  4. java设计模式解析(1) Observer观察者模式

      设计模式系列文章 java设计模式解析(1) Observer观察者模式 java设计模式解析(2) Proxy代理模式 java设计模式解析(3) Factory工厂模式 java设计模式解析( ...

  5. new char()与new char[]区别

    char *pc = new char(15); //开辟一个内存单元,并用括号里的初始化(用15来初始化你定义的指针所指向的那个char)char *pc = new char[15]; //开辟一 ...

  6. Web开发技术---简单的登录验证

    制作一个APP或系统最基础的是登录界面,下面通过一个简单的登录注册界面的程序,来熟练掌握Web开发的技术. 一.知识点: 1.在网页界面获取用户的输入信息 2.标签的基本应用 3.用户输入结果的错误提 ...

  7. C#格式化字符串使用

    1 前言 如果你熟悉Microsoft Foundation Classes(MFC)的CString,Windows Template Library(WTL)的CString或者Standard ...

  8. 180908 input

    input while if # -*- coding:utf-8 -*- flag = 0 while flag == 0 : username = input('请输入用户名:\n') passw ...

  9. map json 字符串 对象之间的相互转化

    1.对象与字符串之间的互转 将对象转换成为字符串 String str = JSON.toJSONString(infoDo); 字符串转换成为对象 InfoDo infoDo = JSON.pars ...

  10. python 使用 tibco ems

    emshelper.py #encoding=utf-8 import jpype jvmpath=r"C:\Program Files\Java\jre1.8.0_161\bin\serv ...