java8新特性之日期时间API

网友投稿 275 2023-01-22

java8新特性之日期时间API

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.printVoOdPBKyln(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)http://; //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);

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:短信接口api(短信接口api轰炸)
下一篇:开放api接口协议(API接口协议)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~