温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java8日期时间API如何使用

发布时间:2022-02-23 15:52:27 来源:亿速云 阅读:125 作者:iii 栏目:开发技术

这篇文章主要介绍“Java8日期时间API如何使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“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.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);

关于“Java8日期时间API如何使用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI