Java——日期时间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);

在这里插入图片描述

在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值