一、日期类
Date类,代表某一瞬间的日期时间,现在使用的是java.util.Date,使用时不要导错包。
1.1 构造方法
通过构造方法,可以创建日期对象,代表一个某一瞬间的时间
- 空参构造,创建当前时间
- 指定long型毫秒值创建时间(基础常识:毫秒值的原点:1970-01-01 00:00:00 0.000)
1.2 方法
- 方法基本都是过时了,不建议用,但是有两个没有过时,重点:
- long getTime(); //获得毫秒值
- void setTime(long t); //设置时间
//创建当前时间 Date d1 = new Date(); System.out.println(d1); //指定毫秒值,创建指定时间(从1970年1月1号为开头) Date d2 = new Date(1 * 24 * 60 * 60 * 1000); System.out.println(d2); //获得毫秒值 long time = d1.getTime(); System.out.println(time); //给日期对象设置毫秒值(long型,需要加L ),改变时间 d1.setTime(1609777777608L); System.out.println(d1);
结果:
Thu Mar 07 10:16:43 CST 2024 Fri Jan 02 08:00:00 CST 1970 1709777803675 Tue Jan 05 00:29:37 CST 2021 Process finished with exit code 0
1.3练习题
创建一个类,学生类 属性(整型年龄,字符串姓名,浮点型成绩,日期型生日
)要封装,创建对象查看效果
package com.qf.dete.T1;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Fighting!!!
*
* @desc
* @since 2024/3/7 9:43
*/
public class Student {
private int age;
private String name;
private double grade;
private Date birth;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
@Override
public String toString() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//格式化
String format = sdf.format(birth);
System.out.println(format);
return "Student{" +
"age=" + age +
", name='" + name + '\'' +
", grade=" + grade +
", birth=" + format +
'}';
}
}
class TestStudent{
public static void main(String[] args) {
Student s1 = new Student();
Date date = new Date();
s1.setAge(22);
s1.setGrade(90);
s1.setName("张三");
s1.setBirth(date);
System.out.println(s1);
}
}
二、SimpleDateFormat
SimpleDateFormat类用来对日期格式化和解析的
格式化(format):是将日期 --> 字符串
解析(parse):是将字符串 --> 日期
日期解析和格式化需要指定模板
y: 年
M: 月
d: 日
H: 24小时制 h是12小时制
m: 分
s: 秒
常用模板
- yyyy/MM/dd 2024/03/07
- yy年M月d日 24年3月7日
- yyyy-MM-dd HH:mm:ss 2024-03-07 10:04:50
public class Demo2 {
public static void main(String[] args) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
//格式化
String format = sdf.format(date);
System.out.println(format);
//解析
Date parse = sdf.parse("2001-11-22 12:12:12");
System.out.println(parse);
}
}
三、异常
3.1异常介绍
Throwable类是java中最大的异常父类,有两个子类:Error和Exception
Error:是程序出现的严重错误,一旦遇到必须抛出错误并解决
Exception:是程序中的异常
异常(Exception)就是程序中的报错。
异常(Exception)的分类:
- 运行时异常:RuntimeException及其子类
- 编译时异常:除了RuntimeException及其子类都是编译异常
运行期异常和编译期异常区别:
- 运行期异常是运行时才有可能出现的异常,编码阶段不用特殊处理
- 编译期异常编码阶段必须处理
如何处理异常(两种方式)?
- 抛出异常
- 捕获异常
3.2 抛出异常
3.2.1 抛出异常的语法:
- 在方法参数列表后{}前,使用
throws + 异常类名
声明抛出的异常类
3.2.2 抛出异常可以同时声明抛出多个异常,逗号隔开
public static void main(String[] args) throws ParseException ,ArithmeticException{ //随便写的 随便抛出两个异常 }
3.2.3 方法声明了抛出异常,如果真的抛出异常会有什么效果?
- 方法抛出异常前正常执行
- 抛出异常后会将信息打印到控制台
- 后续代码不再执行
3.2.4 异常抛出给调用者,异常是层层抛出的。
public class s {
//异常层层向上抛出 异常抛出给调用者
public static void main(String[] args) throws Exception {
m1();
}
public static void m1() throws Exception {
m2();
}
public static void m2() throws Exception {
m3();
}
public static void m3() throws Exception{
}
}
3.3 试图捕获异常
语法:
try{ //一些代码,可能会出现异常的代码 }catch(异常类 对象名){ //如果上面有异常,且能抓住,此处就会执行 } //执行顺序: //try内代码如果没问题,catch内就不执行,try内代码有报错,catch就执行 //无论有没有报错,try-catch后的代码都会执行
public class TextException { public static void main(String[] args) { try{ System.out.println(1); System.out.println(2); System.out.println(3 / 0); System.out.println(3.2); //3 / 0 报错,try内后不执行 }catch(Exception e){ System.out.println("抓住异常,记录日志"+e); } System.out.println(4); System.out.println(5); System.out.println(6); } }
//结果
1
2
抓住异常,记录日志java.lang.ArithmeticException: / by zero
4
5
6
Process finished with exit code 0
3.4 区别
-
抛出异常,异常处后续不再执行
-
捕获异常,try-catch后代码还可以执行
后续编码时,如何使用抛出?捕获?
- 异常后的代码还要执行用捕获异常
- 异常后的代码无关紧要用抛出
3.5 finally
finally最终的,配合try-catch一起使用
作用:finally里面的代码无论是抛出异常还是捕获异常最终都会执行
什么时候能用到?
- 用于关闭流的资源通道,释放资源
3.6 异常API
异常类有构造方法,可以创建对象
- 有一些方法
- getMessage();获得异常信息
- toString();将异常信息以字符串的方法返回
- printStackTrace();打印异常信息
四、自定义异常
- 创建一个异常类,命名合适
- 找到一个合适的父异常类,去继承
- 继承的是RuntimeException及其子类的异常,那么自己的异常就是运行期异常
- 否则是编译期异常
- 设置构造方法,通过super将异常转换成字符串转递给父类
- 使用异常
- 在方法内部,通过判断决定是否抛出异常
- 如果要抛出,通过throws+异常对象抛出
举例1:设置年龄越界异常,设置编译期异常
package com.qf.throwTest;
/**
* Fighting!!!
*
* @author LiWenJing
* @desc 设置年龄越界异常,设置编译期异常
* @since 2024/3/7 19:25
*/
//1.首先自定义一个异常类
public class AgeOutOfBounds extends Exception{
//2.设置构造方法,将想输出的异常文字传递进去
public AgeOutOfBounds(int age){
//3.通过super将异常转换成【字符串】转递给父类
super(String.valueOf(age));
}
}
class TestAge{
public static void main(String[] args) throws AgeOutOfBounds{
show();
}
//设置方法,如果是运行时异常不必抛出 否则抛出
public static void show() throws AgeOutOfBounds{
throw new AgeOutOfBounds(120);
}
}
举例2:练习: 设置一个价钱越界异常
,给手机类使用,当手机价格不合适的时候抛出异常
1.设置异常类
package com.qf.throwTest.PriceOut;
/**
* Fighting!!!
*
* @author LiWenJing
* @desc 练习: 设置一个`价钱越界异常`,给手机类使用,当手机价格不合适的时候抛出异常,设置运行期异常
* @since 2024/3/7 19:43
*/
public class PriceOutOfBounds extends RuntimeException{
public PriceOutOfBounds(double price){
super("价格"+price+"越界,价格必须大于等于0");
}
}
2.创建手机类,在设置价格方法里设置异常
package com.qf.throwTest.PriceOut;
/**
* Fighting!!!
*
* @author LiWenJing
* @desc
* @since 2024/3/7 19:43
*/
public class Phone {
private String name;
private double price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
if(price < 0){
throw new PriceOutOfBounds(price);
}else{
this.price = price;
}
}
@Override
public String toString() {
return "Phone{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
}
3.测试
package com.qf.throwTest.PriceOut;
/**
* Fighting!!!
*
* @author LiWenJing
* @desc
* @since 2024/3/7 19:48
*/
public class TestPriceOut {
public static void main(String[] args) {
Phone phone = new Phone();
phone.setPrice(-12000);
phone.setName("HUAWEI");
}
}
举例3:输入身份证,输出活了多少天
package com.qf.dete.T2;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
/**
* Fighting!!!
*
* @author LiWenJing
* @desc 输入身份证,输出活了多少天
* @since 2024/3/7 10:25
* 方法一:
*/
public class Demo3 {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的身份证号:");
String s = sc.next();
// System.out.println(s);
// System.out.println(s.substring(6, 14));
//截取出生年月日
String sub = s.substring(6, 14);
//设置SimpleDateFormat 要解析截取出来的字符串成日期类型
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date parse = sdf.parse(sub);
//解析出来的日期设置成毫秒值
long time1 = parse.getTime();
System.out.println("出生时间:"+time1);
// 获取现在时间的毫秒值
Date date = new Date();
long time2 = date.getTime();
System.out.println("现在时间:"+time2);
//相差的毫秒值
long result = time2 - time1;
long i = 1*24*60*60*1000;
int end = (int) (result / i);
System.out.println("此人出生到现在活了"+end+"天");
}
//410541200110258426
}
五、日历类
Calendar 日历类
package com.qf.api;
import java.util.Calendar;
/**
* @date 2024/2/2
* @desc
*/
public class Demo4_Calendar {
public static void main(String[] args) {
// Calendar是抽象类,提供静态方法可以获得当前类对象
Calendar calendar = Calendar.getInstance( );
System.out.println(calendar );
/**
* 单独获得年月日
* get(静态字段)
*/
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(year+"-"+month+"-"+day );
/**
* 单独设置年,月,日字段
*/
calendar.set(Calendar.YEAR,2000);
calendar.set(Calendar.MONTH,13);// 会进制递增
System.out.println(calendar.get(Calendar.YEAR) );
System.out.println(calendar.get(Calendar.MONTH) );
/**
* 同时指定年月日
*/
calendar.set(2010,10,10);
System.out.println(calendar.get(Calendar.YEAR) );
System.out.println(calendar.get(Calendar.MONTH) );
System.out.println(calendar.get(Calendar.DAY_OF_MONTH) );
}
}