天天看点

[Java开发之路](3)Java常用类

1.包装类

大家对基本数据类型都非常熟悉,例如 int、float、double、boolean、char 等。基本数据类型是不具备对象的特性,比如基本类型不能调用方法、功能简单。。。,为了让基本数据类型也具备对象的特性, java 为每个基本数据类型都提供了一个包装类,这样我们就可以像操作对象那样来操作基本数据类型。 

基本类型和包装类之间的对应关系:

[Java开发之路](3)Java常用类

包装类主要提供了两大类方法:

    1. 将本类型和其他基本类型进行转换的方法

    2. 将字符串和本类型及包装类互相转换的方法

<code>package com.qunar.test;</code>

<code> </code>

<code>public class helloworld {</code>

<code>public static void main(string[] args) {</code>

<code></code>

<code>// 定义int类型变量,值为100</code>

<code>int num = 100;</code>

<code>// 创建integer包装类对象,表示变量num的值</code>

<code>integer integer=new integer(num);</code>

<code>// 将integer包装类转换为double类型</code>

<code>double num2 = integer.doublevalue();</code>

<code>// 将integer包装类转换为float类型</code>

<code>float num3 = integer.floatvalue();</code>

<code>// 将integer包装类转换为int类型</code>

<code>int num4 = integer.intvalue();</code>

<code>system.out.println("integer包装类:" + integer);</code>

<code>system.out.println("double类型:" + num2);</code>

<code>system.out.println("float类型:" + num3);</code>

<code>system.out.println("int类型:" + num4);</code>

<code>}</code>

2.java 中基本类型和包装类之间的转换

基本类型和包装类之间经常需要互相转换,以 integer 为例:

<code>// 定义integer包装类对象</code>

<code>integer a = new integer(3);</code>

<code>// 将对象和基本数据类型进行运算</code>

<code>int b = a + 5;</code>

<code>system.out.println("b-&gt;"+b);</code>

在 jdk1.5 引入自动装箱和拆箱的机制后,包装类和基本类型之间的转换就更加轻松便利了。

那什么是装箱和拆箱呢?我们分别来看下

装箱:把基本类型转换成包装类,使其具有对象的性质,又可分为手动装箱和自动装箱

<code>// 定义一个int基本类型值</code>

<code>int num = 14;</code>

<code>// 手动装箱</code>

<code>integer x = new integer(num);</code>

<code>// 自动装箱</code>

<code>integer y = num;</code>

<code>system.out.println("x-&gt;"+x+" y-&gt;"+y);</code>

拆箱:和装箱相反,把包装类对象转换成基本类型的值,又可分为手动拆箱和自动拆箱

<code>// 定义一个integer包装类</code>

<code>integer integer = new integer(8);</code>

<code>// 手动拆箱</code>

<code>int x = integer.intvalue();</code>

<code>// 自动拆箱</code>

<code>int y = integer;</code>

3.java 中基本类型和字符串之间的转换

在程序开发中,我们经常需要在基本数据类型和字符串之间进行转换。

基本类型转换为字符串有三种方法:

(1)使用包装类的 tostring() 方法

(2)使用string类的 valueof() 方法

(3)用一个空字符串加上基本类型,得到的就是基本类型数据对应的字符串

<code>int num = 102;</code>

<code>// 使用包装类的 tostring() 方法</code>

<code>string str1 = integer.tostring(num);</code>

<code>// 使用string类的 valueof() 方法</code>

<code>string str2 = string.valueof(num);</code>

<code>// 用一个空字符串加上基本类型,得到的就是基本类型数据对应的字符串</code>

<code>string str3 = num + "";</code>

将字符串转换成基本类型有两种方法:

(1)调用包装类的 parsexxx 静态方法

(2)调用包装类的 valueof() 方法转换为基本类型的包装类,会自动拆箱

<code>string str = "150";</code>

<code>// 调用包装类的 parsexxx 静态方法</code>

<code>int num1 = integer.parseint(str);</code>

<code>// 调用包装类的 valueof() 方法转换为基本类型的包装类</code>

<code>int num2 = integer.valueof(str);</code>

4. 使用 date 和 simpledateformat 类表示时间

在程序开发中,经常需要处理日期和时间的相关数据,此时我们可以使用 java.util 包中的 date 类。这个类最主要的作用就是获取当前时间,我们来看下 date 类的使用:

<code>import java.util.date;</code>

<code>// 使用默认构建方法创建date对象</code>

<code>date date = new date();</code>

<code>// 输出date对象</code>

<code>system.out.println(date);</code>

使用 date 类的默认无参构造方法创建出的对象就代表当前时间,我们可以直接输出 date 对象显示当前的时间,显示的结果如下:

sun dec 20 11:20:25 cst 2015  

从上面的输出结果中,我们发现,默认的时间格式不是很友好,与我们日常看到的日期格式不太一样,如果想要按指定的格式进行显示,如 2015-12-20 11:20:25 ,那该怎么做呢?

可以使用java.text 包中的 simpledateformat 来对日期时间进行格式化,如可以将日期转换为指定格式的文本,也可将文本转换为日期。

(1) 使用 format() 方法将日期转换为指定格式的文本

<code>import java.text.simpledateformat;</code>

<code>// 创建simpledateformat对象,指定日期格式</code>

<code>simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");</code>

<code>// 调用format()方法,格式化时间,转换为指定格式字符串</code>

<code>string today = sdf.format(date);</code>

<code>// 输出转换后的日期</code>

<code>system.out.println(today);</code>

代码中的 “yyyy-mm-dd hh:mm:ss” 为预定义字符串, yyyy 表示四位年, mm 表示两位月份, dd 表示两位日期, hh 表示小时(使用24小时制), mm 表示分钟, ss 表示秒,这样就指定了转换的目标格式,最后调用 format() 方法将时间转换为指定的格式的字符串。

输出结果:

2015-12-20 11:36:45  

(2)使用 parse() 方法将文本转换为日期

<code>import java.text.parseexception;</code>

<code>public static void main(string[] args) throws parseexception {</code>

<code>// 创建日期格式的字符串</code>

<code>string today = "2015年12月20日 12:19:40";</code>

<code>simpledateformat sdf = new simpledateformat("yyyy年mm月dd日 hh:mm:ss");</code>

<code>// 调用parse()方法,将字符串转换为日期</code>

<code>date date = sdf.parse(today);</code>

代码中的 “yyyy年mm月dd日 hh:mm:ss” 指定了字符串的日期格式,调用 parse() 方法将文本转换为日期。

运行结果: 

sun dec 20 12:19:40 cst 2015  

注意:

1、 调用 simpledateformat 对象的 parse() 方法时可能会出现转换异常,即 parseexception ,因此需要进行异常处理

2、 使用 date 类时需要导入 java.util 包,使用 simpledateformat 时需要导入 java.text 包

案例:

<code>// 使用format()方法将日期转换为指定格式的文本</code>

<code>simpledateformat sdf1 = new simpledateformat("yyyy年mm月dd日 hh时mm分ss秒");</code>

<code>simpledateformat sdf2 = new simpledateformat("yyyy/mm/dd hh:mm");</code>

<code>simpledateformat sdf3 = new simpledateformat("yyyy-mm-dd hh:mm:ss");</code>

<code>// 创建date对象,表示当前时间</code>

<code>date now = new date();</code>

<code>// 调用format()方法,将日期转换为字符串并输出</code>

<code>system.out.println(sdf1.format(now));</code>

<code>system.out.println(sdf2.format(now));</code>

<code>system.out.println(sdf3.format(now));</code>

<code>// 使用parse()方法将文本转换为日期</code>

<code>string d = "2014-6-1 21:05:36";</code>

<code>date date = sdf.parse(d);</code>

5. calendar 类的应用

date 类最主要的作用就是获得当前时间,同时这个类里面也具有设置时间以及一些其他的功能,但是由于本身设计的问题,这些方法却遭到众多批评,不建议使用,更推荐使用 calendar 类进行时间和日期的处理。

java.util.calendar 类是一个抽象类,可以通过调用 getinstance() 静态方法获取一个 calendar 对象,此对象已由当前日期时间初始化,即默认代表当前时间,如 calendar c = calendar.getinstance();

那么如何使用 calendar 获取年、月、日、时间等信息呢?我们来看下面的代码:

<code>import java.util.calendar;</code>

<code>// 抽象类 通过调用 getinstance() 静态方法获取一个 calendar 对象</code>

<code>calendar calendar = calendar.getinstance();</code>

<code>// 年</code>

<code>int year = calendar.get(calendar.year);</code>

<code>// 月</code>

<code>int month = calendar.get(calendar.month);</code>

<code>// 日</code>

<code>int day = calendar.get(calendar.day_of_month);</code>

<code>// 时</code>

<code>int hour = calendar.get(calendar.hour_of_day);</code>

<code>// 分</code>

<code>int minute = calendar.get(calendar.minute);</code>

<code>// 秒</code>

<code>int second = calendar.get(calendar.second);</code>

<code>system.out.println("当前时间:"+year+"年"+month+"月"+day+"日 "+hour+":"+minute+":"+second);</code>

其中,调用 calendar 类的 getinstance() 方法获取一个实例,然后通过调用 get() 方法获取日期时间信息,参数为需要获得的字段的值, calendar.year 等为 calendar 类中定义的静态常量。

运行结果:

当前时间:2015年11月20日  13:13:23  

calendar 类提供了 gettime() 方法,用来获取 date 对象,完成 calendar 和 date 的转换,还可通过 gettimeinmillis() 方法,获取此 calendar 的时间值,以毫秒为单位。如下所示:

<code>// calendar 对象转换为date对象</code>

<code>date date = calendar.gettime();</code>

<code>// 获取当前毫秒数</code>

<code>long time = calendar.gettimeinmillis();</code>

<code>system.out.println("当前时间:"+date);</code>

<code>system.out.println("当前毫秒数:"+time);</code>

当前时间:sun dec 20 13:17:13 cst 2015

当前毫秒数:1450588633247  

6. 使用 math 类操作数据

math 类位于 java.lang 包中,包含用于执行基本数学运算的方法, math 类的所有方法都是静态方法,所以使用该类中的方法时,可以直接使用类名.方法名,如: math.round();

常用的方法:

[Java开发之路](3)Java常用类

<code>// 定义一个double类型的变量</code>

<code>double num1 = 14.87;</code>

<code>// double类型变量强制类型转换为int类型,去掉小数位</code>

<code>int num2 = (int)num1;</code>

<code>system.out.println("强制类型转换:"+num2);</code>

<code>// 调用round()方法,四舍五入</code>

<code>long num3 = math.round(num1);</code>

<code>system.out.println("四舍五入:"+num3);</code>

<code>// 调用floor()方法,小于参数的最大整数</code>

<code>double num4 = math.floor(num1);</code>

<code>system.out.println("小于参数的最大整数:"+num4);</code>

<code>// 调用ceil()方法,大于参数的最小整数</code>

<code>double num5 = math.ceil(num1);</code>

<code>system.out.println("大于参数的最小整数:"+num5);</code>

<code>// 调用random()方法,[0,1]之间的随机浮点数</code>

<code>double num6 = math.random();</code>

<code>system.out.println("[0,1]之间的随机浮点数:"+num6);</code>

<code>// [0,99]之间的随机整数</code>

<code>int num7 = (int)(math.random()*99);</code>

<code>system.out.println("[0,99]之间的随机整数:"+num7);</code>