Why do package classes appear?
Basic data types do not have the characteristics of objects. For example, basic types cannot call methods and have simple functions. In order for basic data types to also have the characteristics of objects, Java provides a wrapper class for each basic data type, so that basic data types can be manipulated like objects. Each basic type has a corresponding packaging class:
I believe that all of you are very familiar with basic data types, such as int, float, double, boolean, Char et al. Basic data types do not have the characteristics of objects. For example, basic types cannot call methods and have simple functions. . . , In order for basic data types to also have the characteristics of objects, Java provides a wrapper class for each basic data type, so that we can operate basic data types like objects.
Correspondence between basic types and packaging classes:
Note: There are two packaging classes with special names. One is Integer and the other is Character, other basic data types are capitalized.
The packaging class mainly provides two categories of methods:
1. Methods for converting this type and other basic types
2. Converting strings to this type and Methods for converting wrapper classes to each other ;>>>>>>>>>>>>>>>>>>
Basic types in Java Conversion between basic types and packaging classes
It is often necessary to convert between basic types and packaging classes, taking Integer as an example (the operations of other packaging classes are similar):1 |
|
Boxing:
Convert the basic type into a packaging class so that it has the properties of an object, which can be divided into manual boxing and automatic boxing.1 |
|
1 |
|
1 |
|
1 |
|
different types cannot be automatically boxed, but different types can be automatically unpacked box. >>>>>>>>>>>>>>>>>>>>> ;>>>>>>>>>>>>>>>
The difference between basic types and strings in Java Conversion
In program development, we often need to convert between basic data types and strings. Among them,basic types are converted to strings
There are three methods:1. Use the toString() method of the wrapper class
2. Use String The valueOf() method of the class3. Add an empty string to the basic type, and the result is the string corresponding to the basic type dataThe code is as follows:1 |
|
1. Call the parseXxx static method of the packaging class
2 . Call the valueOf() method of the wrapper class to convert it to a basic type of wrapper class, which will automatically unbox it The code is as follows:1 |
|
2. Date and SimpleDateFormat classes
1 |
|
使用 Date 类的默认无参构造方法创建出的对象就代表当前时间,我们可以直接输出 Date 对象显示当前的时间,显示的结果如下:
其中, Thu 代表 星期四, Jul 代表 七月,06 代表 06 号, CST 代表 China Standard Time (中国标准时间,也就是北京时间,东八区)。
从上面的输出结果中,我们发现,默认的时间格式不是很友好,与我们日常看到的日期格式不太一样,如果想要按指定的格式进行显示,如 2017-07-06 13:31:28 ,那该怎么做呢?
此时就到了 java.text 包中的 SimpleDateFormat 类大显身手的时候了!!可以使用 SimpleDateFormat 来对日期时间进行格式化,如可以将日期转换为指定格式的文本,也可将文本转换为日期。
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1. 使用 format() 方法将日期转换为指定格式的文本
1 |
|
运行结果:
结果分析:
代码中的 “yyyy-MM-dd HH:mm:ss” 为预定义字符串, yyyy 表示四位年, MM 表示两位月份, dd 表示两位日期, HH 表示小时(使用24小时制), mm 表示分钟, ss 表示秒,这样就指定了转换的目标格式,最后调用 format() 方法将时间转换为指定的格式的字符串。
2. 使用 parse() 方法将文本转换为日期
1 |
|
运行结果:
结果分析:
代码中的 “yyyy年MM月dd日 HH:mm:ss” 指定了字符串的日期格式,调用 parse() 方法将文本转换为日期。因为将字符串转换为Date类型可能会抛出异常,所以要用try-catch语句捕获。
一定要注意哦:
1、 调用 SimpleDateFormat 对象的 parse() 方法时可能会出现转换异常,即 ParseException ,因此需要进行异常处理。
2、 使用 Date 类时需要导入 java.util 包,使用 SimpleDateFormat 时需要导入 java.text 包。
Date 类最主要的作用就是获得当前时间,同时这个类里面也具有设置时间以及一些其他的功能,但是由于本身设计的问题,这些方法却遭到众多批评,不建议使用,更推荐使用 Calendar 类进行时间和日期的处理。
java.util.Calendar 类是一个抽象类,可以通过调用 getInstance() 静态方法获取一个 Calendar 对象,此对象已由当前日期时间初始化,即默认代表当前时间,如 Calendar c = Calendar.getInstance();
那么如何使用 Calendar 获取年、月、日、时间等信息呢?我们来看下面的代码:
1 2 |
|
运行结果:
结果分析:
调用 Calendar 类的 getInstance() 方法获取一个实例,然后通过调用 get() 方法获取日期时间信息,参数为需要获得的字段的值, Calendar.Year 等为 Calendar 类中定义的静态常量。
注意:其中有两个特别的参数:DAY_OF_MONTH和HOUR_OF_DAY,千万不要写成DAY和HOUR了;MONTH获取的月份,0表示一月。
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Calendar 类提供了 getTime() 方法,用来获取 Date 对象,完成 Calendar 和 Date 的转换,还可通过 getTimeInMillis() 方法,获取此 Calendar 的时间值,以毫秒为单位。如下所示:
1 |
|
运行结果:
1.通过调用 getInstance() 静态方法获取一个 Calendar 对象---对象初始化
Calendar c = Calendar.getInstance();
2.通过调用 get() 方法获取日期时间信息
int month=c.get(Calendar.MONTH)+1;----0表示1月份
3.提供 getTime() 方法,用来获取 Date 对象
Date date=c.getTime();----将Calender对象转换为Date对象
4.通过 getTimeInMillis() 方法,获取此 Calendar 的时间值
long time=c.getTimeInMillis();----获取当前毫秒
Math 类位于 java.lang 包中,包含用于执行基本数学运算的方法, Math 类的所有方法都是静态方法,所以使用该类中的方法时,可以直接使用类名.方法名,如: Math.round();
常用的方法:
下面用代码来实现:
1 2 |
|
运行结果:
注意:要注意Math各个方法返回值类型,可以用强制类型转换来转为自己想要的类型。
PS: Math 类还提供了许多其他方法,各位小伙伴们可以按需去API文档查找想要知道的方法。
The above is the detailed content of Common classes that you must know in java. For more information, please follow other related articles on the PHP Chinese website!