学习Java的第九天 | String类的总结 | 包装类——数字相关类 | 日期相关类 | 异常 |

1 String类常用方法的补充

String类在进行比较的时候,有两种比较方式
1==
比较引用数据类型的时候,比较的是地址
比较基本数据类型也就是值等,比较的是值
2 equal
String 重写了 Object的equals方法 用来比较值
忽略大小写的比较equalsIgnoreCase();

valueOf();//其他类型转字符串的方法
当我们要使用一个类的时候
1创建对象 调用方法,看看这个类有什么普通方法
2使用类名调用方法,看看这个类有什么静态方法

StringBuilder 字符串拼接,在原来的字符串基础上拼接
inser();//在什么位置插入
delete();//删除什么位置的数据
append();//在末尾拼接
reverse();//倒置
replace();//替换
capacity();//判断数据长度
toString();//转换为字符串类

public class Test02 {
    public static void main(String[] args) {
        StringBuilder stringBuilder = new StringBuilder("asdfjkl;");
        stringBuilder.insert(0,'8');
        stringBuilder.delete(5,8);
        stringBuilder.append(777);
        stringBuilder.reverse();
        stringBuilder.replace(0,4,"76543");
        System.out.println(stringBuilder.capacity());
        System.out.println(stringBuilder);
        String str = stringBuilder.toString();
        System.out.println(str);
    }
}

StringBuffer在原来的字符串基础上拼接,方法和StringBuffer基本相同
但StringBuffer是一个线程安全的类

2 包装类——数字相关类

1 以后要学习集合 泛型中不能放基本数据类型
2 类的属性 根据数据类型默认值 统一的默认值null

byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

下面重点介绍一下Integer,其他的可以类比
1 创建对象的方式
Integer i = new Integer(2);
Integer j = 5;

2 运行时常量池(范围是一个字节 -128~127)
使用Integer进行比较的时候,范围必须在一个字节里面,不然就会无法比较

Integer k = 10;
Integer j = 10;
k == j ; //=========>ture
Integer k = 1000;
Integer j = 1000;
k == j ; //=========>false 

3 引用数据类型和基本数据类型比较的时候,引用数据类型转换为基本数据类型
intValue()

4 基本数据类型和引用数据类型可以相互转换 装箱和拆箱
Integer i = 5;(自动装箱)基本转引用 基本数据类型可以使用方法,具有某种功能
类比例子:苹果装进礼物盒 还是苹果 但是现在是一个礼物 (功能)
int a = new Integer(5);(自动拆箱) 引用转基本 基本数据类型不可以使用方法,没有了功能

5 静态方法
parseInt();//把字符串中的数字转为Integer类型

String s = "9";
System.out.println(Integer.parseInt(s));

6 数字运算
这里涉及到Math类,Math提供的类中都是静态方法
pow();// 求几次方
E//2.71818
PI//3.1415926535
sqrt();//求平方根
floor();//向下取整
ceil();//向上取整
round();//四舍五入
random();//0-1的随机数,可以使用*10,取所需
rand.nextInt();//随机生成指定范围的数据 0~bound-1

public class Test03 {
    public static void main(String[] args) {
        System.out.println(Math.pow(5,2));//25
        System.out.println(Math.E);//2.718281828459045
        System.out.println(Math.PI);//3.141592653589793
        System.out.println(Math.sqrt(9));//3.0
        System.out.println(Math.floor(8.4));//8.0
        System.out.println(Math.ceil(8.4));//9.0
        System.out.println(Math.round(8.4));//8
        System.out.println(Math.random()*100);//被放大一百倍的随机数
    }
}

7 数字之间的精确运算
这里需要使用到一个BigDecimal类
add();//加法
subtract();//减法
multiply();//乘法

public class Test01 {
    public static void main(String[] args) {
        BigDecimal b1 = new BigDecimal("7");
        BigDecimal b2 = new BigDecimal("3");
        System.out.println(b1.add(b2));
        System.out.println(b1.subtract(b2));
        System.out.println(b1.multiply(b2))}
}

9 数字按照指定格式输出
DecimalFormat类
在这里插入图片描述

DecimalFormat df = new DecimalFormat(",##0.00");
System.out.println(df.format(b1.divide(b2, MathContext.DECIMAL32)));

3 日期相关类

Date类 用于日期的表示

y
M
d/ 一年中的第几天为D
12小时制h / 24小时制H
m
s
毫秒S

1 Date 转为 String

//获取date
Date date = new Date();
//使用格式化时间类 ,设置显示格式
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy");
//使用String类的format方法,格式化date
System.out.println(sdf1.format(date));//2021

2 String 转为 Date

//设定一个时间
String str = "2021-01-22 15:30:20";
//使用格式化时间类,设置上面所展示的格式
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");
try {
	//使用parse将String类转为Date  parse();接受字符串并格式化
	Date date = format.parse(str);
	//输出date
	System.out.println(date);
} catch (ParseException e) {
	e.printStackTrace();
}

还有一个就是Calender类 用于计算日期
在这个类中,要获取某个字段上的值,可以使用get
需要注意的是,这里获取到的关于月份的,都是要加上1的,因为它的月份下标是从0开始的

public class Test03 {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar.get(Calendar.YEAR));
        System.out.println(calendar.get(Calendar.MONTH));
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
        System.out.println(calendar.get(Calendar.DAY_OF_WEEK));
        System.out.println(calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
        System.out.println(calendar.get(Calendar.WEEK_OF_YEAR));
        System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
        System.out.println(calendar.get(Calendar.MINUTE));
        System.out.println(calendar.get(Calendar.HOUR));
    }
}

其字段无需解释,非常明确
当我们想要在原来的日历对象的基础上进行修改,可以先清空

public class Test03 {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR,2020);
        System.out.println(calendar.get(Calendar.YEAR));
    }
}

日期间隔的计算add();

1 Date --> String

Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = calendar.getTime();
String str = simpleDateFormat.format(date);
System.out.println(calendar);
System.out.println(str);

2 String --> Date

Calendar calendar = Calendar.getInstance();
String string = "2021-02-01 19:33:16";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
	simpleDateFormat.parse(string);
	Calendar c = simpleDateFormat.getCalendar();
	c.add(Calendar.YEAR,30);
	System.out.println(c.get(Calendar.YEAR));
} catch (ParseException e) {
		e.printStackTrace();
}

4 异常Throwable

异常有两种
1、exception 异常 可以解决
异常也分为运行时异常和非运行时异常
我们对非运行时异常的处理方式有两个:
1 捕获异常
try{
可能发生异常的代码 和 使用 可能发生遗产过的代码的返回值 的代码
}catch{
捕获肯能发生异常的 对象 这个对象是虚拟机创建的对象
}
如果这个异常发生了,执行这个catch中的代码,如果不发生就不会执行
catch可以有很多个
2 排除异常
在方法上加throws 异常类型
哪个方法执行,就把异常抛给谁。

public class Test03 {
    public static void main(String[] args) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        String string = "2021-02-01 19:33:16";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        simpleDateFormat.parse(string);
        Calendar c = simpleDateFormat.getCalendar();
        c.add(Calendar.YEAR,30);
        System.out.println(c.get(Calendar.YEAR));
        
    }
}

2 Error错误 不能解决

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值