首页 / JAVA / [java8新特性]新日期时间API
[java8新特性]新日期时间API
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了[java8新特性]新日期时间API,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5788字,纯文字阅读大概需要9分钟。
内容图文
![[java8新特性]新日期时间API](/upload/InfoBanner/zyjiaocheng/755/737094feeece4f7a8fafa4b71eab0e82.jpg)
JDK8前时间API
JDK8之前的日期时间API存在的问题
(1)可变性:像日期和时间这样的类应该是不可变的(例如String的不可变性)
public class Demo2 {
public static void main(String[] args) {
Date date=new Date();
date.setHours(12);
System.out.println(date);//Fri Jul 19 12:07:54 CST 2019
}
}
可以看出,调用了setHours()方法后,date产生变化,而要实现不可变性,setHours方法不再是void,而去返回一个Date对象,而是Date date2=date.setHours(12),产生一个新的date2,而不是对原来的date进行改变
(2)偏移性:Date中的年份是从1970开始的,而月份都从0开始
public class Demo2 {
public static void main(String[] args) {
Date date=new Date(0);
System.out.println(date);//Thu Jan 01 08:00:00 CST 1970
}
}
Date构造器可以传入一个毫秒偏移量,传入0得到初始时间是1970-1-1 00:00:00,年份是从1970年开始计算的
public class Demo2 {
public static void main(String[] args) {
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH,6);
System.out.println(calendar.DAY_OF_MONTH); //5
}
}
月份是从0开始计算的,不合理
(3)格式化只对Date有用,Calendar则不行
public class Demo2 {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = sdf.format(date);
System.out.println(dateStr); // 2019-07-19 20:21:00
Calendar calendar=Calendar.getInstance();
String calendarStr = sdf.format(calendar);// java.lang.IllegalArgumentException
}
}
此外Date(日期类)与Calendar(日历类)都不是线程安全的,且不能处理闰秒。
JDK8新API体验
1.java.time.LocalDate,java.time.LocalTime,java.time.LocalDateTime
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
//LocalDate、LocalTime、LocalDateTime
public class Demo {
public static void main(String[] args) {
//静态方法now():获取当前的日期、时间、日期+时间
LocalDate localDate=LocalDate.now();
LocalTime localTime=LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate); //2019-07-19
System.out.println(localTime); //20:25:55.448
System.out.println(localDateTime); //2019-07-19T20:25:55.448
//静态方法of():自定义日期、时间、日期+时间
LocalDate localDate1 = LocalDate.of(2020, 10, 10);//2020年10月10号
LocalTime localTime1=LocalTime.of(20,20,20,20);//20时20分20秒20毫秒
LocalDateTime localDateTime1 = LocalDateTime.of(2020, 10, 10, 20, 20, 20, 20);//2020年10月10号20时20分20秒20毫秒
//getXxx():可以获得很多信息
System.out.println(localDateTime.getDayOfMonth()); //获取本月的第几天
System.out.println(localDateTime.getMonthValue()); //获取月份(数值)
System.out.println(localDateTime.getMinute()); //获取分钟
//withXxx():对年月日等信息进行设置,返回一个新的对象
LocalDateTime localDateTime2 = localDateTime.withDayOfMonth(22);
System.out.println(localDateTime.getDayOfMonth());//19 体现不可变性
System.out.println(localDateTime2.getDayOfMonth());//22
//plusXxx():加减法运算
LocalDateTime localDateTime3 = localDateTime.plusDays(2); //在原来日期基础上加两天
System.out.println(localDateTime.getDayOfMonth());//19
System.out.println(localDateTime3.getDayOfMonth());//21
LocalDateTime localDateTime4 = localDateTime.minusDays(2);//在原来日期的基础上减两天
System.out.println(localDateTime.getDayOfMonth());//19
System.out.println(localDateTime4.getDayOfMonth());//17
}
}
总结:java.time.LocalDateTime 类似于java.util.Calendar
2.java.time.Instant
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
//java.time.Instant
public class Demo {
public static void main(String[] args) {
//静态方法now():获取当前的日期+时间
Instant instant=Instant.now();//按照中时区算的,而中国处在东八区,会和我们这相差8小时
System.out.println(instant);//2019-07-19T12:56:49.045Z
//atOffset() 设置偏移量8小时得到东八区时间
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);//2019-07-19T20:56:49.045+08:00
//获取对应的毫秒数 从1970-1-1 00:00:00格林威治时间到现在的毫秒数
long milli = instant.toEpochMilli();
System.out.println(milli);//1563541009045
//ofEpochMilli(毫秒偏移量)得到Instant对象
Instant instant1=Instant.ofEpochMilli(1563541009045l);
System.out.println(instant1);//2019-07-19T12:56:49.045Z
}
}
总结:java.time.Instant类似于java.util.Date
3.java.time.format.DateTimeFormatter
mport java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;
//java.time.format.DateTimeFormatter
public class Demo {
public static void main(String[] args) {
//实例化方式一:预定义的标准格式
//DateTimeFormatter.XXX
DateTimeFormatter formatter=DateTimeFormatter.ISO_LOCAL_DATE;
LocalDateTime localDateTime = LocalDateTime.now();
String localDateTimeStr = formatter.format(localDateTime);
System.out.println(localDateTime);//2019-07-19T21:18:10.728
System.out.println(localDateTimeStr);//2019-07-19
//实例化方式二:本地化相关的格式
//ofLocalizedDateTime(FormatStyle.XXX)
DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
String localDateTimeStr1 = formatter1.format(localDateTime);
System.out.println(localDateTimeStr1);//19-7-19 下午9:18
//ofLocalizedDateTime(FormatStyle.XXX)
LocalDate localDate = LocalDate.now();
DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
String localDateStr = formatter2.format(localDate);
System.out.println(localDateStr);//2019年7月19日 星期五
//实例化方式三:自定义格式
//new DateTimeFormatter("格式")
DateTimeFormatter formatter3 =DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String localDateTimeStr2 = formatter3.format(localDateTime);
System.out.println(localDateTimeStr2);//2019-07-19 21:18:10
//解析:字符串-->日期
TemporalAccessor accessor = formatter3.parse("2019-07-19 21:18:10");
System.out.println(accessor);//{},ISO resolved to 2019-07-19T21:18:10
}
}
总结:java.time.format.DateTimeFormatter类似于java.text.SimpleDateFormat
内容总结
以上是互联网集市为您收集整理的[java8新特性]新日期时间API全部内容,希望文章能够帮你解决[java8新特性]新日期时间API所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。