- 常用类
- 包装类
- 日期类
- Math类
- File类
一、常用类
1、包装类
JAVA并不是纯面向对象的语言。Java语言是一个面向对象的语言,但是Java中的基本数据类型却是不面向对象的。但是我们在实际使用中经常需要将基本数据转化成对象,便于操作。比如:集合的操作中。例如使用Map对象要操作put()方法时,需要传入的参数是对象而不是基本数据类型。为了解决这个不足,在设计类时为每个基本数据类型设计了一个对应的类进行代表,这样八个和基本数据类型对应的类统称为包装类(Wrapper Class)。包装类均位于java.lang包
基本数据类型和包装类
基本数据类 | 包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
除了int
和cha
的包装类有点不同外,其它六个基本数据类型的包装类就是类型的第一个字母大写
1.1、包装类的用途
- 将基本数据类型转换为引用类型,以便不能使用基本类型的时候使用(泛型、集合)。
- 包装类有构造器,提供了方法和属性,可以用来在字符串和数字之间转换等操作
包装类和基本类型之间存在自动装箱和自动拆箱操作,可以更加方便地切换
1.2、自动装箱和拆箱
Integer int1 = 123; // 自动装箱
int a= new Integer(123); // 自动拆箱
基本数据类型直接赋值给包装类叫做自动装箱,把一个包装类对象赋值给基本数据类型叫做自动拆箱(基本数据类型和包装类要对应)
2、常用操作
2.1、求两数的最大值和最小值
System.out.println(Integer.max(5,8)); // 求最大值,8
System.out.println(Integer.min(5,8)); // 求最小值,5
2.2、字符串转换为数字
注意:字符串转换为数字,字符串必须为纯数字
// 方式一
Integer int1 = new Integer("123");
System.out.println(int1);
// 方式二
int1 = Integer.valueOf("123");
System.out.println(int1);
1.3.4、数字转换为字符串
// 方式一
String str = String.valueOf(123);
System.out.println(str);
// 方式二,一般使这种
str = 123+"";
System.out.println(str);
二、日期类
在程序中,日期的存储也是个数字,是个长整型的数字。0代表1970年1月1日 00:00:00,而我们处于东八区,所以为1970年1月1日 08:00:0 ,往前推,负数表示之前的时间,往后算正数表示之后的时间。
1、常用日期类和方法
1.1、System类(系统类)
方法 | 描述 |
---|---|
public static native long currentTimeMillis(); | 返回当前时间(以毫秒为单位) |
System.out.println(System.currentTimeMillis());
运行结果 |
---|
1582676559883 |
1.2、Date类(日期类)
构造器|方法 | 描述 |
---|---|
new Date(); | 分配一个 Date 对象,并初始化它,以便它代表它被分配的时间,测量到最近的毫秒。 |
new Date(long date); | 分配一个 Date 对象,并将其初始化为0指定毫秒数。 |
long getTime(); | 返回自1970年1月1日以来,由此 Date 对象表示的00:00:00 GMT的毫秒 数 。 |
void setTime(long time); | 设置此 Date 对象以表示1970年1月1日00:00:00 GMT后的 time 毫秒的时间点。 |
Date date = new Date();
System.out.println(date);
date = new Date(1582676098801L);
System.out.println(date);
date = new Date();
System.out.println(date.getTime());
date.setTime(1582676098801L);
System.out.println(date);
运行结果 |
---|
Wed Feb 26 08:22:39 CST 2020 Wed Feb 26 08:14:58 CST 2020 1582676559899 Wed Feb 26 08:14:58 CST 2020 |
1.3、SimpleDateFormat类(格式器)
该类是DateFormat
的子类
构造器|方法 | 描述 |
---|---|
new SimpleDateFormat(String pattern); | 使用给定的模式构造一个 SimpleDateFormat 。 |
public final String format(Date date); | 将日期格式化成日期/时间字符串。 |
public Date parse(String source); | 从给定字符串的开始解析文本以生成日期。 |
parse有异常,需要捕获或抛出
日期格式化,在日期格式化的过程中,会用到模板。在API中有详细说明
Date date = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm"); // 指定字符串形式
System.out.println(df.format(date)); // 日期转换为指定形式字符串
String str = "2020-01-01 01:02:03";
date = df.parse(str); // 这里有异常
System.out.println(date);
运行结果 |
---|
2020-02-26 08:53:54 Wed Jan 01 01:03:02 CST 2020 |
2、JDK8提供的日期类
类 | 方法 | 描述 |
---|---|---|
Yera | static Year now() | 从默认时区的系统时钟获取当前年份。 |
Month(枚举) | JANUARY… | 月份 |
LocalDate | LocalDate now() | 从默认时区的系统时钟获取当前日期。(不包含时分秒) |
LocalDate | of(int year, int month, int dayOfMonth) | 这将返回一个LocalDate 与指定的年,月和日。 该日期必须在年和月中有效,否则将抛出异常。 |
LocalDateTime | LocalDateTime now() | 从默认时区的系统时钟获取当前日期。(包含时分秒) |
LocalDateTime | of(int year, int month, int dayOfMonth, int hour, int minute, int second) | 返回一个LocalDateTime 与指定的年,月,日,月,时,分和秒。 该日期必须在年和月中有效,否则将抛出异常。 |
LocalDateTime | LocalDateTime from(TemporalAccessor temporal) | 使用指定的格式化程序格式化此日期时间。 |
LocalDateTime | parse(CharSequence text) | 从一个文本字符串获取一个 LocalDateTime 的实例。 |
System.out.println(Year.now());
System.out.println(Month.JANUARY);
System.out.println(LocalDate.now());
System.out.println(LocalDate.of(2020,2,25));
System.out.println(LocalDateTime.now());
System.out.println(LocalDateTime.of(2020,2,25,1,2,3));
运行结果 |
---|
2020 JANUARY 2020-02-26 2020-02-25 2020-02-26T08:55:08.174 2020-02-25T01:02:03 |
3、JDK8提供的格式化类
类 | 方法 | 描述 |
---|---|---|
DateTimeFormatter | ofPattern(String pattern) | 使用指定的模式创建格式化程序。 |
// 日期转为指定形式字符串
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // 设置格式
LocalDateTime now = LocalDateTime.now(); // 获取对象
System.out.println(now.format(formatter)); // 格式化
// 字符串转为日期
String str = "2020-01-02 01:02:03";
System.out.println(LocalDateTime.parse(str,formatter)); // 字符串转换为日期
运行结果 |
---|
2020-02-26 09:02:26 2020-01-02T01:02:03 |
三、Math类
System.out.println(Math.abs(-4)); // 绝对值
System.out.println(Math.ceil(3.14)); // 向上取整
System.out.println(Math.floor(3.14)); // 向下取整
System.out.println(Math.round(3.5)); // 四舍五入
System.out.println(Math.sqrt(4)); // 开方
System.out.println(Math.pow(2, 2)); // 幂
System.out.println(Math.max(5,8)); // 最大值
System.out.println(Math.min(5,8)); // 最小值
运行结果 |
---|
4 4.0 3.0 4 2.0 4.0 8 5 |
四、File类
file对象不是一个实际的文件,只是Java对象和文件的映射
1、常见的方法
// 创建file对象
File file = new File("a.txt");
File file1 = new File("test1","a.txt");
// 文件是否存在,返回boolean类型
System.out.println(file.exists());
// 文件是否可读,返回boolean类型
System.out.println(file.canRead());
// 文件是否可写,返回boolean类型
System.out.println(file.canWrite());
// 是否是一个文件夹,返回boolean类型
System.out.println(file.isDirectory());
// 是否是一个文件,返回boolean类型
System.out.println(file.isFile());
// 是否不可见(隐藏),返回boolean类型
System.out.println(file.isHidden());
// 返回最后修改的时间,返回long类型
System.out.println(file.lastModified());
// 文件的大小(字节),返回long类型
System.out.println(file.length());
// 返回文件或目录名称,返回String类型
System.out.println(file.getName());
// 返回文件或目录相对路径,返回String类型
System.out.println(file.getPath());
// 返回文件或目录绝对路径,返回String类型
System.out.println(file.getAbsolutePath());
运行结果 |
---|
true true true false true false 1582622889527 18 a.txt a.txt F:\eclipse-workspace\Day14\a.txt |
2、文件和文件夹的创建
2.1、文件创建
File file = new File("B.txt"); // 创建文件
file.createNewFile();
2.2、文件夹创建
该文件夹创建方式只能创建一层
File file1 = new File("B"); // 创建文件夹
file1.mkdir();
2.3、多层文件夹创建
File file1 = new File("A\\B"); // 创建多层文件
file1.mkdirs();
3、删除文件或文件夹
File file = new File("B.txt");
file.delete(); // 删除文件
File file1 = new File("B");
file1.delete(); // 删除文件夹(只能删除一个目录)
4、获取文件(文件夹)列表
File[] files = new File("E:").listFiles(); // 获取E盘文件夹下的目录(只有根目录那一层)
System.out.println(Arrays.toString(files));
运行结果 |
---|
[E:$RECYCLE.BIN, E:\8uftp, E:\Android, E:\apache-tomcat-9.0.30, E:\Atom Beta x64, E:\eclipse, E:\Git, E:\HBuilderX, E:\hexo, E:\IntelliJ IDEA 2019.3.2, E:\java, E:\mysql-8.0.19-winx64, E:\nodejs, E:\PyCharm 2019.3.2, E:\Python38, E:\Shadowsocks, E:\SQLyog, E:\System Volume Information, E:\Xftp6, E:\Xshell6] |