JAVA常用类(包装类、时间处理相关类、Math等)

1、包装类(将基本数据类型转化为对象,实际就是实现包装类、基本数据类型、字符串三者转化。)
在这里插入图片描述
注:只有int和char的包装类名称不同

//把基本数据类型转成对象
Integer int1 = Integer.valueOf(10);//也可=new Integer(10)不推荐
// Integer对象转化成int
int newint = int1.intValue();
//jdk1.5之后自动装箱、拆箱
Integer a = 1234; // 自动装箱,编译器默认调用上面转换的方法
int a = b; //自动拆箱,编译器默认调用上面的转换方法
//注释: 包装类在自动装箱时为了提高效率,对于-128~127之间的值会进行缓存处理。Integer出来的对象是同一个对象。超过范围后,则不再是。

// 字符串转化成Integer对象
Integer int2 = Integer.parseInt("123");//也可=new Integer(“123”)不推荐
// Integer对象转化成字符串
String str1 = int2.toString();//或者直接+“”

// 一些常见int类型相关的常量
System.out.println("int能表示的最大整数:" + Integer.MAX_VALUE); 

//注:其他相似

2、时间处理相关类
2.1Date类,import java.util.Date;多数方法都被替代,一般不常用

//Date(n)中有参数,则1970.1.1+n毫秒
Date date = new Date();
System.out.println(date);//=>Wed May 27 17:44:10 CST 2020
System.out.println(date.getTime());//=>1590572650786//返回当前时间,自 1970 年 1 月 1 日 00:00:00至今

2.2DateFormat类
实现时间对象与字符串的转化。DateFormat是抽象类,一般用其子类SimpleDateFormat。

//需要传入格式化字符串:H小时数(0-23);h(1-23);E星期;D今年第几天
//时间对象转字符串
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//-:可换
String stringdate = date.format(new Date());

//字符串转时间类型
Date date2 = date.parse("1999-9-09 12:00:00");
System.out.println(date2);

2.3Calendar
实现关于日期计算的相关功能,如:年、月、日、时、分、秒的展示和计算。Calendar 类是一个抽象类,一般用其子类GregorianCalendar。

//设置日期格式
Calendar calendar2 = new GregorianCalendar();
calendar.set(calendar.YEAR, 2020);
// 日期计算
GregorianCalendar calendar3 = new GregorianCalendar();
calendar3.add(Calendar.DATE,7); //增加7天
System.out.println(calendar3);
// 日历对象和时间对象转化
Date d = calendar3.getTime();
GregorianCalendar calendar4 = new GregorianCalendar();
calendar4.setTime(new Date());
//注意:日期为1-7;周日在前面,周一、周二...

3、Math

//取整相关操作
System.out.println(Math.ceil(3.2));//=>4.0
System.out.println(Math.floor(3.2));//=>3.0
System.out.println(Math.round(3.2));//=>3
System.out.println(Math.round(3.8));//=>4
//绝对值、开方、a的b次幂等操作
System.out.println(Math.abs(-45));//=>45
System.out.println(Math.sqrt(64));//=>8.0
System.out.println(Math.pow(5, 2));//=>25
//随机数
System.out.println(Math.random());// [0,1)

4、枚举类
定义一组常量时,可以使用枚举类型,一般配合case语句使用

enum  枚举名 {
      枚举体(常量列表)
}

5、String类
在博客中有详细介绍。

6、File类
同上。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值