「这是我参与2022首次更文挑战的第15天,活动详情查看:2022首次更文挑战」
一 基本类型包装类
- 将基本数据类型封装成对象的好处在于在对象中定义更多的功能方法操作该数据
- 常用的操作之一:用于基本数据类型与字符串之间的转换
| 基本数据类型 | 包装类 |
|---|
| byte | Byte |
| short | Short |
| int | Integer(重点) |
| long | Long |
| float | Float |
| double | Double |
| char | Character(特殊) |
| boolean | Boolean |
1.1 Integer类概述和使用
- Integer:包装一个对象中原始类型int的值
- 常用方法
| 方法名 | 说明 |
|---|
| public static Integer vlaueOf(int i) | 返回表示指定的int值的 Integer 实例 |
| public static Integer vlaueOf(String s) | 返回一个保存指定的 Integer 对象 String |
public class IntegerDemo {
public static void main(String[] args) {
Integer i1 = new Integer(100);
System.out.println(i1);
Integer i2 = new Integer("100");
System.out.println(i2);
}
}
1.2 int 和 String 的相互转换
1、int 转换为 String 类型
public static String valueOf(int i):返回 int 参数的字符串表示形式。该方法是 String 类中的方法
2、String 转换为 int 类型
public static int parselnt(String s):将字符串解析为int类型。该方法是 Integer 类中的方法
package ceshi;
public class IntegerDemo {
public static void main(String[] args) {
int number = 10;
String s = String.valueOf(number);
System.out.println(s);
String s1 = "100";
int i = Integer.parseInt(s1);
System.out.println(i);
}
}
1.3 自动装箱和拆箱
- 自动装箱: 可以直接把基本数据类型的值或变量赋值给包装类
- 自动拆箱: 可以把包装类的变量直接赋值给基本数据类型
public class PackageClass {
public static void main(String[] args) {
Integer i = Integer.valueOf(100);
Integer ii = 100;
int i1 = ii;
ii +=200;
System.out.println(ii)
Integer iii = null;
if(iii != null) {
iii += 300;
}
}
}
- 注意:在使用包装类的时候,如果是操作最好先判断是否为null;推荐只要是对象,再使用前必须进行不为null判断
二 日期类
2.1 Date类
| 方法名 | 说明 |
|---|
| public Date() | 创建当前系统的此刻日期时间对象 |
| public Date(long time) | 把时间毫秒值转换成日期对象 |
package ceshi;
import java.util.Date;
public class DateDemo {
public static void main(String[] args) {
Date d1 = new Date();
System.out.println(d1);
long date = 60;
Date d2 = new Date(date);
System.out.println(d2);
}
}
| 方法名 | 说明 |
|---|
| public long getTime() | 获取日期对象从1970年1月1日00:00:00 到现在的毫秒值 |
| public void setTime(long time) | 设置时间,给的是毫秒值 |
package ceshi;
import java.util.Date;
public class DateDemo {
public static void main(String[] args) {
Date d = new Date();
System.out.println(d.getTime());
long time = 1000*60*60;
d.setTime(time);
System.out.println(d);
}
}
2.2 SimpleDateFormat类 [ˈsɪmpl]
- 可以对日期格式化和解析
- 日期和时间格式由日期和时间模式字符串指定,在日期和时间模式字符串中,从 'A' 到 'Z' 以及从 'a' 到'z' 引号的字母被解释为表示日期或时间字符串的组件的模式字母
- 常用的模式字母对应: y---年;M---月;d---日;H---时;m---分;s---秒;E---星期几;a---上午 / 下午
- 构造方法:
| 方法名 | 说明 |
|---|
| public SimpleDateFormat() | 使用默认模式和日期模式 |
| public SimpleDateFormat(String pattern) | 指定时间的格式创建简单日期格式化对象 |
| 方法名 | 说明 |
|---|
| public String format(Date date) | 将日期格式化为日期 / 时间字符串 |
| public String format(Object time) | 将时间毫秒格式化为日期 / 时间字符串 |
| public Date parse(String source) [pɑːz] | 从给定的字符串开始解析文本生成日期 |
package ceshi;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatDemo {
public static void main(String[] args) throws ParseException {
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss E a");
String s = sdf.format(d);
System.out.println(s);
String s1 = "2020年1月1日 8:25:12";
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date d1 = sdf1.parse(s1);
System.out.println(d1);
}
}
2.3 Calendar日历类
- Calendar 代表了系统此刻日期对应的日历对象
- Calendar 是一个抽象类,不能直接创建对象
- 常用方法
| 方法名 | 说明 |
|---|
| public static Calendar getInstance() | 返回一个日历类的对象 |
| public int get(int field) | 返回给定日历的值 |
| public void set(int field,int value) | 修改日历的某个字段信息 |
| public abstract void add(int field,int amount) | 根据日历的规则,将指定的时间量添加或减去给定的日历字段 |
| public final void set(int year,int month,int date) | 设置当前日历年月日 |