java8新特性之日期时间API
作者:我思想出了问题 发布时间:2023-06-20 09:15:50
jdk8之前
一、java.lang.System
long times = System.currentTimeMillis();
//返回的是当前时间与1970年1月1月1日0分0秒之间以毫秒为单位的时间差
//称为时间戳
System.out.println(times);
二、java.util.Date And java.sql.Date
将java.util.Date 对象转换为java.sql.Date对象:
//将java.util.Date 对象转换为java.sql.Date对象
Date date1 = new Date();
java.sql.Date date2 = new java.sql.Date(date1.getTime());
三、java.text.SimpleDateFormat
SimpleDateFormat是对日期Date类的格式化和解析。
两个操作:
1.格式化:
将日期转换为字符串:
SimpleDateFormat sfd = new SimpleDateFormat();
Date date = new Date();
System.out.println(date);
String formateDate = sfd.format(date);
System.out.println(formateDate);
也可以指定具体的格式化格式,查看具体的API格式。
例:指定格式的格式化输出(调用带参数的构造器)
Date date = new Date();
System.out.println(date);
SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String formateDate = sfd.format(date);
System.out.println(formateDate);
2.解析:
将 字符串 转换为 日期。即格式化的逆过程。
String str = "2021/4/12 下午10:16";
SimpleDateFormat sfd = new SimpleDateFormat();
Date date2 = sfd.parse(str);
System.out.println(date2);
这个注意要抛异常(传入的str格式要与Date的原格式一致,或者说要与SimpleDateFormate当前识别的格式相同)。
练习:将字符串“2021-04-13” 转换为java.sql.Date类型对象。
分析:首先将字符串解析为Date类型的对象,然后在转为java.sql.Date类型对象。
public static void testExper() throws ParseException {
String s = "2021-04-13";
SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd");
Date date = sfd.parse(s);
java.sql.Date date2 = new java.sql.Date(date.getTime());
System.out.println(date2);
}
四、java.util.Calendar
常用实例化方法:
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getClass()); //java.util.GregorianCalendar,其实还是子类类型的对象
常用方法:
1.get():获取常用的属性和信息。
2.set():设置:相当于把本身的日期给改变了
3.add():添加(增加时间、天数)
4.getTime():日历类----> Date类
5.setTime():Date类----> 日历类
Calendar calendar = Calendar.getInstance();
//System.out.println(calendar.getClass()); //java.util.GregorianCalendar
//get()
int days = calendar.get(calendar.DAY_OF_MONTH);//获取当前日期是这个月的第几天
System.out.println(days); //13
//set()
calendar.set(calendar.DAY_OF_MONTH, 22);//重新设置
days = calendar.get(calendar.DAY_OF_MONTH);//在重新获取
System.out.println(days); //22
//add()
calendar.add(calendar.DAY_OF_MONTH, 3);
days = calendar.get(calendar.DAY_OF_MONTH);
System.out.println(days); //25
//getTime():日历类----> Date类
Date date = calendar.getTime();
System.out.println(date); //Sun Apr 25 13:14:59 CST 2021
//setTime():Date类----> 日历类
Date date2 = new Date();
calendar.setTime(date2);
days = calendar.get(calendar.DAY_OF_MONTH);
System.out.println(days); //13
jdk8中:java.time
新日期时间API出现的背景:
可变性:像日期和时间这样的类应该是不可变的。
偏移性:Date中的年份是从1900开始的,而月份都是从0开始。
格式化:格式化只对Date有用,Calendar则不行。
此外,他们也不是线程安全的;不能处理闰秒。
一、常用类
说明:LocalDateTime类相较于其他两个类使用频率较高。
二、一些方法
(1)now():获取当前的日期、时间、日期+时间。(实例化方法一)
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate);
System.out.println(localTime);
System.out.println(localDateTime);
(2)of():设置指定时间的年、月、日、时、分。没有偏移量。(实例化方法二)
//of():设置指定时间的年、月、日、时、分。没有偏移量。
LocalDateTime localDateTime2 = LocalDateTime.of(2021, 4, 13, 15, 20);
System.out.println(localDateTime2);
(3)getXxx():获取…
(4)withXxx():修改(设置)…,这个方法不会改动原本的值。
(5)plusXxx():添加
(6)minusXxx():减
三、Instant
(1)now():获取本初子午线对应的标准时间。(实例化方法一)
//now():获取本初子午线对应的标准时间
Instant instant = Instant.now();
System.out.println(instant);
//添加时间的偏移量
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);
(2)toEpochMilli():获取毫秒数
long milli = instant.toEpochMilli();
System.out.println(milli);
(3)ofEpochMilli():通过给定的毫秒数,获取Instant实例 (实例化方法二)
//通过给定的毫秒数,获取Instant实例
Instant instant2 = Instant.ofEpochMilli(1618300028028l);
System.out.println(instant2);
四、DateTimeFormatter类
DateTimeFormatter类:格式化或解析日期、时间,类似于SimpleDateFormat。
(1)预定义的标准格式进行格式化:DateTimeFormatter.ISO_LOCAL_DATE_TIME
注意: 日期-----> 字符串
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
LocalDateTime localDateTime = LocalDateTime.now();
String str = formatter.format(localDateTime);//注意类型变化
System.out.println(localDateTime);
System.out.println(str);
(2)本地化相关的格式。如:ofLocalizedDateTime()。
//FormatStyle.SHORT / FormatStyle.LONG / FormatStyle.MEDIUM :适用于LocalDateTime
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
LocalDateTime localDateTime = LocalDateTime.now();
String str = formatter.format(localDateTime);
System.out.println(localDateTime);
System.out.println(str);
(3)自定义格式:ofPattern()
//自定义格式。如:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
String string = formatter.format(LocalDateTime.now());
System.out.println(string);
来源:https://blog.csdn.net/deku1018/article/details/115625581


猜你喜欢
- 概述JDK的bin目录下提供了很多命令工具,比如java.exe,javap.exe,javac.exe。。。。。。这些命令由jdk/lib
- 1、简单又有效的方法是使用PreparedStatement采用预编译语句集,它内置了处理SQL注入的能力,只要使用它的setXXX(如:s
- 之前写了一篇文章:Java 网络IO编程总结(BIO、NIO、AIO均含完整实例代码),介绍了如何使用Java原生IO支持进行网络编程,本文
- 本文实例讲述了java编程调用存储过程中得到新增记录id号的实现方法。分享给大家供大家参考,具体如下:关于ms sql server2000
- 1 前言项目中,目前主流的当然是微服务项目。为了应对高并发,以及保证自己的服务比较稳定,通常会把服务按照模块,或者具体的业务划分为多个独立的
- 目录环境依赖数据源方案一 使用 Spring Boot 默认配置方案二 手动创建脚本初始化使用 JdbcTemplate 操作实体对象DAO
- 起源 [1946: John von Neumann, Stan Ulam, and Nick Metropolis, all a
- RandomAccessFileRandomAccessFile是用来访问那些保存数据记录的文件的,你就可以用seek( )方法来访问记录,
- 前言在之前的文章中介绍过线程池的核心原理,在一次面试中面试官让手写线程池,这块知识忘记的差不多了,因此本篇文章做一个回顾。希望能够加深自己的
- refresh()该方法是 Spring Bean 加载的核心,它是 ClassPathXmlApplicationContext 的父类
- 前面聊了布隆过滤器,回归认识一下位图BitMap,阅读前文的同学应该发现了布隆过滤器本身就是基于位图,是位图的一种改进。位图先看一个问题,
- 1 关于自动内存管理Java是由jvm来管理内存,包括自动分配以及自动回收,因此它不容易出现内存泄漏和内存溢出问题。C/C++,由程序员手动
- 要用TextView使用渐变色,那我们就必须要了解LinearGradient(线性渐变)的用法。LinearGradient的参数解释Li
- 示例我们先来以这样一个场景引入: 在电脑城装机总有这样的经历。我们到了店里,先会有一个销售人员来询问你希望装的机器是怎么样的配置,
- 将jar包发布到Maven中央仓库(Maven Central Repository),这样所有的Java开发者都可以使用Maven直接导入
- Android 自定义imageview实现图片缩放实例详解 觉得这个自定义的imageview很好用 性能不错 所以
- Apache的POI项目可以用来处理MS Office文档,codeplex上还有一个它的.net版本。POI项目可创建和维护操作各种基于O
- 简介最近几年,各种新的高效序列化方式层出不穷,不断刷新序列化性能的上限,最典型的包括:专门针对Java语言的:Kryo,FST等等跨语言的:
- 一、关键字分类C语言一共多少个关键字呢?一般的书上,都是32个(包括本书),但是这个都是C90(C89)的标准。其实C99后又新增了5个关键
- Hadoop是什么?Hadoop是一个开发和运行处理大规模数据的软件平台,是Appach的一个用java语言实现开源软件框架,实现在大量计算