Java SE进阶(6)时间日期类和异常

一、时间日期类

世界标准时间:格林威治时间—>原子钟

中国标准时间:北京时间(东八区)

计算机标准时间:1970年1月1日0时0分0秒(Unix操作系统诞生日)

我国计算机时间要+8小时

一、Date类

1、构造方法

public Date()	创建一个日期对象,表示默认时间
public Date(long time)	创建一个日期对象,表示指定时间(时间原点开始,date毫秒值)

2、成员方法

public long getTime() 获取时间对象的毫秒值。
public void setTime(long time) 设置时间传递毫秒值

二、SimpleDateFormat类

格式化(从Date转换成String)

public final String format(Date date)

解析(从String转换成Date)

public Date parse(String s)

案例:

//      使用SimpleDateFormat类,把 2018-03-04 转换为 2018年03月04日。
        String s = "2018-03-04";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  //模板格式
        Date parse = sdf.parse(s);//字符串转换成日期对象
        System.out.println(parse);

        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日");
        String format = sdf1.format(parse);//日期对象转换成字符串对象
        System.out.println(format);

二、LocalDateTime类

1、LocalDateTime类

2、LocalDate类

表示时间(年月日)

3、LocalTime类

表示日期(时分秒)

表示时间+日期(年月日时分秒)

public static LocalDateTime now() 获取当前系统时间(能获取到秒之后的值)
public static LocalDateTime of(,,,,,) 指定时间初始化一个LocalDateTime对象

LocalDateTime获取方法

public int getYear()	获取年
public int getMonthValue()	获取月(1-12public int getDayOfMonth()	获取月份中的第几天(1-31public int getDayOfYear()	获取年中的第几天(1-366public DayOfWeek getDayOfWeek()	获取星期
public int getMinute()	获取分钟
public int getHour()	获取小时

转换方法

public  LocalDate toLocalDate()	转成LocalDate对象
public  LocalTime toLocalTime()	转成LocalTime对象

格式化与解析

DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") 表示字符时间模板格式
public String format(指定格式) 把一个LocalDateTime格式化成一个字符串
public LocalDateTime parse(准备解析的字符串,解析格式) 把一个日期字符串解析成LocalDateTime对象
		LocalDateTime localDateTime = LocalDateTime.of(2020, 12, 12,13,14,15);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//模板格式

        String format = localDateTime.format(formatter);   //将日期格式化成字符串
        System.out.println(format); //2020-12-12 13:14:15

        LocalDateTime parse = LocalDateTime.parse(format, formatter);//将字符串格式化成日期
        System.out.println(parse);  //2020-12-12T13:14:15

时间,加减,直接修改法

public LocalDateTime plusYear(long year) 添加或者减去年(+-year)
...
public LocalDateTime minusYear(long year) 减去或者添加年
...
public LocalDateTime withYear(long year) 直接修改年
...

时间间隔对象

Period

public static Period between(开始时间,结束时间)  计算两个时间间隔
public int getYears() 获取这段时间年数
public int getMonths() 获取这段时间月数
public int getDays() 获取这段时间天数
public long toTotalMonths() 获取这段时间总月数

Duration

public static Duration between(开始时间,结束时间)  计算两个时间间隔
public long toSeconds() 获取这段时间的秒
public int toMillis() 获取这段时间的毫秒
public int toNanos() 获取这段时间的纳秒

三、异常

异常:就是程序中出现不正常的情况,程序在执行过程中,出现非正常情况,最终导致JVM虚拟机运行停止。

异常体系:

在这里插入图片描述

成员方法:

public String getMessage() 返回此throwable的详细信息字符串
public String toString() 返回此可抛出的简短描述
public void printStackTrace() 把异常的错误信息输出在控制台(常用)

一、抛出异常(throws)

格式:throws 异常名

该异常定义在方法后,表示抛出这个异常错误,并没有解决它,它会把这个异常抛给他的调用者,谁调用它就把异常抛给谁解决。没有对象处理的话,该异常就会层层上抛,最终抛给虚拟机,虚拟机解决的办法就是报出错误,停止程序。

在这里插入图片描述

二、捕获异常(try catch)

格式:

	try{
​		要执行的方法;
	}catch(异常类型 变量名){
​		发生异常要执行的操作;
	}

该异常是自己处理异常的方式,try中的语句是受保护的语句,当语句执行发生异常时会立即执行catch中的语句对异常进行处理并报错,此时程序不会停止运行,并且会继续执行try catch之外的代码继续运行程序。

在这里插入图片描述

三、自定义异常(thorw)

  1. 自定义异常目的:为了让异常信息更加见明知意。
  2. 只需要命名一个异常类,继承异常父类(RunTimeException运行时异常,Exception非运行时异常),实现它的空参和有参构造即可。
  3. 在需要抛出异常的执行语句后面写上 throw 异常名即可抛出异常。
  4. 注意该异常只能在方法内抛出。

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值