包装2年java_java基础之包装类,Math,日期格式处理

包装类,Math,日期格式处理

包装类

Math:random(),round()

日期格式处理

Java提倡的是思想是一切皆对象,但是我们基本数据类型不是一个类,也就是说 没有对象概念,和Java提倡的思想相违背,为了解决这个矛盾,Java里面提供8种基本数据类型的包装类。

序号

基本数据类型

包装类

1

int

Integer

2

char

Character

3

float

Float

4

long

Long

5

short

Short

6

double

Double

7

boolean

Boolean

8

byte

Byte

public static voidmain(String[] args) {int i=10;

boolean flag=false;double j=10.00;

Integer k=new Integer(i);//基本数据类型转换成包装类对象(装箱)

Boolean f=newBoolean(flag);

Double d=newDouble(j);int x=k.intValue();//包装类转换成基本数据类型叫拆箱

boolean bo=f.booleanValue();double de=d.doubleValue();//进行拆箱

}

包装类除了解决Java提倡的思想之外呢,还可以进行什么操作呢?

·装箱:将基本数据类型转换成包装类对象

·拆箱:将包装类对象转换成基本数据类型

·将字符串转换成基本数据类型

以上的装箱和拆箱操作都是手动进行的,在JDK1.5之后,装箱和拆箱的操作可以自动进行。

public static voidmain(String[] args) {int i=10;

boolean flag=false;double j=10.00;

Integer k=i;//基本数据类型转换成包装类对象(装箱)

Boolean f=flag;

Double d=j;int x=k;//包装类转换成基本数据类型叫拆箱

boolean bo=f;double de=d;//进行拆箱

}

//将字符串转换成基本数据类型

private voidmian() {//TODO Auto-generated method stub

String s="1122";int x=Integer.parseInt(s);int y=Integer.valueOf(s);

}

开发的时候一定要注意,在转换的时候,注意异常的出现,比如说我们将字符串“aabb”转换成int类型的话,那么一定会报错(NumberFormatException)。

2Math

Math类是定义了一系列的数学运算方法,是使用static修饰的方法,表示可以通过Math类直接访问。我们在开发中一般只需要关注两个:

·四舍五入:

·取得随机数

public static voidmain(String[] args) {double d=100.53;int i = (int)Math.round(d);

System.out.println(i);

}

另外就是随机数:使用random()取随机数取出来的是0-1小数,是大于等于0,小于1。

范例:取1-10之间的随机数

public static voidmain(String[] args) {for (int i = 1; i < 10; i++) {double d=Math.random();//取0到1之间的随机数

int x=(int) Math.round(d*10);

System.out.println(x+" ");

}

}

3日期格式的处理

我们主要讲到的是Date类,是java.util包中的Date类,另外Calendar(日历)类,而后是日期格式转换类(DateFormat)。

Date:Date类中的大部分的方法都已经过时,一般只会用到构造方法取得系统当前的时间。

·public long getTime():取得自1970年1月1号开始的毫秒数

public static voidmain(String[] args) {

Date date= newDate();

System.out.println(date);

System.out.println(date.getTime());

}

如果我们现在要取得Date对象的准确的年月日时分秒毫秒呢?我们知道Date类中的方法已经过时,那么我们现在使用Calendar日历类完成操作。此类有大量的操作方法:

·public abstract void add(int field,  int amount):给指定的日历字段添加指定的值

·public int get(int field):取得指定日历字段的值

·public final Date getTime():转换成Date类型

·public void set(int field,  int value):指定的日历字段设置指定的值

·public final void setTime(Date date):将Date类型设置到Calendar对象中

范例:取得准确年月日时分秒毫秒

public static voidmain(String[] args) {

Date date= newDate();

System.out.println(date);

System.out.println(date.getTime());

Calendar ca= new GregorianCalendar();//取得日历对象(也可以取得系统当前时间)

System.out.println("年"+ca.get(Calendar.YEAR));

System.out.println("月"+ca.get(Calendar.MONTH)+1);//0到11个月

System.out.println("日"+ca.get(Calendar.DAY_OF_MONTH));

System.out.println("时"+ca.get(Calendar.HOUR_OF_DAY));

System.out.println("分"+ca.get(Calendar.MINUTE));

System.out.println("秒"+ca.get(Calendar.SECOND));

System.out.println("毫秒"+ca.get(Calendar.MILLISECOND));

System.out.println("今年第几周"+ca.get(Calendar.WEEK_OF_YEAR));

System.out.println("这个月第几周:"+ca.get(Calendar.WEEK_OF_MONTH));

}

3.日期格式的转换

在Java中使用DateFormat进行日期格式的转换,此类也是一个抽象类,实例化的时候必须依靠子类:SimpleDateFormat,常用的构造方法:

·public SimpleDateFormat(String pattern):参数表示的日期的转换规则

规则:y年,M月,d日,H小时,m分钟,s秒,S毫秒

DateFormat常用的方法:

·public final String format(Date date):将Date对象转换成字符串

·public Date parse(String source) throws ParseException:将字符串转换成日期

范例:将Date类型转换成字符串

public static voidmain(String[] args) {

Date date= newDate();

DateFormat df= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");//定义转换格式

String s= df.format(date);//进行转换

System.out.println(s);

}

}

范例:将字符串转换成日期

String s = "1998/10-10";

DateFormat df= new SimpleDateFormat("yyyy/MM-dd");//定义转换格式

try{

Date date= df.parse(s);//将字符串转化成日期

System.out.println(date);

}catch(Exception e) {

e.printStackTrace();

}

在开发过程中,一定要注意SimpleDateFormat定义的转换规则,如果转换规则定义错误,那么日期转换异常没法避免

小结

·使用包装类将字符串转换成基本数据类型

·会使用Math中的random()和round()方法

·学会使用Calendar类中的一些方法

·学会使用DateFormat进行日期格式的转换

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值