详解Java关于JDK中时间日期的API

网友投稿 538 2022-12-08

详解Java关于JDK中时间日期的API

JDK 8 之前日期和时间的API测试

//1.System类中的currentTimeMillis()

public void test1(){

long time = System.currentTimeMillis();

//返回当前时间与1970年1月1日0时0分0秒之间以毫秒为时间为单位的时间差。

//称为时间戳

System.out.println(time);//1628416744054

}

/*

java.util.Date类

|---java.sql.Date类(两个是子父类的关系)

1.两个构造器的使用

>构造器一:Date():创建一个对应当前时间的Date对象

>构造器二:创建指定毫秒数的Date对象

2.两个方法的使用

>toString():显示当前的年、月、日、时、分、秒

>getTime():获取当前Date对象对应的毫秒数。(时间戳)

3.java.sql.Date对应着数据库中的日期类型的变量

>如何实例

>如何将java.util.Date对象转换为java.sql.Date对象

*/

@Test

public void test2(){

bSBXDMfPqx //构造器一:Date():创建一个对应当前时间的Date对象

Date date1 = new Date();

System.out.println(date1.toString());//Sun Aug 08 1bSBXDMfPqx8:07:27 CST 2021

//构造器二:创建指定毫秒数的Date对象

Date date2 = new Date(1534798293927L);

System.out.println(date2.toString());//Tue Aug 21 04:51:33 CST 2018

//创建java.sql.Date对象

java.sql.Date date3 = new java.sql.Date(237493269533L);

System.out.println(date3);//1977-07-12

//如何将java.util.Date对象转换为java.sql.Date对象

//情况一:(面向对象)

// Date date4 = new java.sql.Date(23682368236343L);

// java.sql.Date date5 = (java.sql.Date)date4;

//情况二:

Date date6 = new Date();

java.sql.Date date7 = new java.sql.Date(date6.getTime());

}

/*

SimpleDateFormat的使用:SimpleDateFormat对日期Date类的格式化和解析

1.两个操作:

1.1 格式化:日期--->字符串

1.2 解析:格式化的逆过程:字符串--->日期

2.SimpleDateFormat的实例化

*/

@Test

public void testSimpleDateFormat() throws ParseException {

//实例化SimpleDateFormat

SimpleDateFormat sdf = new SimpleDateFormat();

/http:///格式化:日期--->字符串

Date date = new Date();

System.out.println(date);

String format = sdf.format(date);

System.out.println(format);

//解析:格式化的逆过程,字符串--->日期

String str = "21-8-9 下午3:17";

Date date1 = sdf.parse(str);

System.out.println(date1);

//******************按照指定的方式格式化和解析:调用带参的构造器*****************************

// SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyy.MMMMM.dd GGG hh:mm aaa");

SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm ss");

//格式化

String format1 = sdf1.format(date);

System.out.println(format1);//2021-08-09 04:02 13

//解析:要求字符串必须是符合SimpleDateFormat识别的格式(通过构造器参数体现),否则,报异常

Date date2 = sdf1.parse("2019-08-09 04:02 13");

System.out.println(date2);

}

/*

练习一:字符串"2020-09-08"转换为java.sql.Date

*/

@Test

public void testExer() throws ParseException {

String brith = "2020-09-08";

SimpleDateFormat sdf1 = new SimpleDateFormat("yyy-MM-dd");

Date date = sdf1.parse(brith);

java.sql.Date brithDate = new java.sql.Date(date.getTime());

System.out.println(brithDate);

}

/*

Calendar日历类(抽象类)的使用

*/

@Test

public void testCalendar(){

//1.实例化

//方式一:创建其子类(GregorianCalendar)的对象

//方式二:调用其静态方法getInstance()

Calendar calendar = Calendar.getInstance();

//2.常用方法

//get()

int days = calendar.get(Calendar.DAY_OF_MONTH);

System.out.println(days);

System.out.println(calendar.get(Calendar.DAY_OF_YEAR));

//set()

calendar.set(Calendar.DAY_OF_MONTH,22);

days = calendar.get(Calendar.DAY_OF_MONTH);

System.out.println(days);

//add()

calendar.add(Calendar.DAY_OF_MONTH,-3);

days = calendar.get(Calendar.DAY_OF_MONTH);

System.out.println(days);

//getTime():日历类 -->Date

Date date1 = calendar.getTime();

calendar.setTime(date1);

days = calendar.get(Calendar.DAY_OF_MONTH);

System.out.println(days);

}

JDK 8中关于日期和时间的API测试

LocalDate 、 LocalTime 、 LocalDateTime 类是其中较重要的几个类,它们的实例是不可变的对象,分别表示使用 ISO —8601日历系统的日期、时间、日期和时间。它们提供了简单的本地日期或时间,并不包含当前的时间信息,也不包含与时区相关的信息。

> LocalDate 代表 IOS 格式( yyyy - MM - dd )的日期,可以存储生日、纪念日等日期。> LocalTime 表示一个时间,而不是日期。

> LocalDateTime 是用来表示日期和时间的,这是一个最常用的类之一。

注: ISO —8601日历系统是围际标准化组织制定的现代公民的门期和时间的表示法,也就是公历。

/*

LocalDate,LocalTime,LocalDateTime的使用

*/

public void test1(){

//now():获取当前的日期,时间,日期+时间

LocalDate localDate = LocalDate.now();

LocalTime localTime = LocalTime.now();

LocalDateTime localDateTime = LocalDateTime.now();

System.out.println(localDate);//2021-08-09

System.out.println(localTime);//21:19:25.264

System.out.println(localDateTime);//2021-08-09T21:19:25.264

//of():设置指定的年,月,日,时,分,秒.没有偏移量

LocalDateTime localDateTime1 = LocalDateTime.of(2020,10,6,13,23,43);

System.out.println(localDateTime1);

//getXxx():获取相关的属性

System.out.println(localDateTime.getDayOfMonth());

System.out.println(localDateTime.getDayOfWeek());

System.out.println(localDateTime.getMonth());

System.out.println(localDateTime.getMonthValue());

System.out.println(localDateTime.getMinute());

//体现不可变性

//withXxx():设置相关的属性

LocalDate localDate1 = localDate.withDayOfMonth(22);

System.out.println(localDate);

System.out.println(localDate1);

LocalDateTime localDateTime2 = localDateTime.withHour(4);

System.out.println(localDate);

System.out.println(localDateTime2);

}

/*

Instant的使用

类似于java.util.Date类

*/

@Test

public void test2(){

//now():获取本初子午线对应的标准时间

Instant instant = Instant.now();

System.out.println(instant);//2021-08-09T15:08:46.818Z

//添加时间的偏移量

OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));

System.out.println(offsetDateTime);//2021-08-09T23:08:46.818+08:00

//toEpochMilli():获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数-->Date类的getTime()

long milli = instant.toEpochMilli();

System.out.println(milli);//1628521726818

//ofEpochMilli():通过给定的毫秒数,获取Instant实例-->Date(long millis)

Instant instant1 = Instant.ofEpochMilli(12243455253L);

System.out.println(instant1);//1970-05-22T16:57:35.253Z

}

/*

DateTimeFormatter:格式化或解析日期\时间

类似于SimpleDateFormat

*/

@Test

public void test3(){

// 方式一:预定义的标准格式,如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME

DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;

//格式化:日期-->字符串

LocalDateTime localDateTime = LocalDateTime.now();

String str1 = formatter.format(localDateTime);

System.out.println(localDateTime);//2021-08-09T23:43:52.820

System.out.println(str1);//2021-08-09T23:43:52.82

//解析:字符串-->日期

// TemporalAccessor parse = formatter.parse(" 2021-08-09T23:43:52.82");

// System.out.println(parse);

// 方式二:本地化相关的格式,如:ofLocalizedDateTime

// FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT:适用于LocalDateTime

DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);

//格式化:

String str2 = formatter1.format(localDateTime);

System.out.println(str2);//2021年8月10日 上午10时32分48秒

// 本地化相关的格式,如:ofLocalizedDate()

// FormatStyle.FULL / FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT:适用于LocalDate

DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);

//格式化

String str3 = formatter2.format(LocalDate.now());

System.out.println(str3);//2021-8-10

// 重点!!!!!!!:方式三:自定义的格式.如:ofPattern("yyyy-MM-dd hh:mm:ss")

DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");

//格式化

String str4 = formatter3.format(LocalDateTime.now());

System.out.println(str4);//2021-08-10 10:53:40

//解析

TemporalAccessor accessor = formatter3.parse("2021-08-10 10:53:40");

System.out.println(accessor);//{MinuteOfHour=53, NanoOfSecond=0, MicroOfSecond=0, HourOfAmPm=10, SecondOfMinute=40, MilliOfSecond=0},ISO resolved to 2021-08-10

}

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

上一篇:深入浅出讲解Java比较器及数学常用类
下一篇:详解SpringBoot中JdbcTemplate的事务控制
相关文章

 发表评论

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