java中的常用类(包装类、Date类、Calendar类和Math类)
来源:互联网 发布:淘宝潮一点的女装店 编辑:程序博客网 时间:2024/06/05 23:42
包装类
包装类主要提供了两大类方法:
1. 将本类型和其他基本类型进行转换的方法
其实强制转换也可以做到某些部分,但是比如说把integer强制转换为String就会报错
包装类和强制转换并没有什么关系,它的目的是把基本数据类型变成对象。因为java是强类型语言,所以当函数接收的参数为对象时,传入的参数必须为对象而不能是基本数据类型,包装类解决了这一问题。
Integer包装类的构造方法:Integer(int value)/Integer(String s)
例:
int i = 2;Integer m = new Integer(5);Integer n = new Integer("5");
Integer包装类的常用方法:
JDK1.5 引入了自动装箱和拆箱的机制
装箱:把基本类型转换成包装类,使其具有对象的性质
int i = 10;Integer x = i;//自动装箱(当然也可以直接Integer x = 10)Integer x = new Integer(i);//手动装箱
拆箱:和装箱相反,把包装类对象转换成基本类型的值
Integer j = new Integer(10);int m = j;//自动拆箱int n = j.intValue();//手动拆箱
2. java中基本类型和字符串之间的转换
其中,基本类型转换为字符串有三种方法:
① 使用包装类的 toString() 方法
②使用String类的 valueOf() 方法
③ 用一个空字符串加上基本类型,得到的就是基本类型数据对应的字符串
//将基本类型转换为字符串int c = 10;String str1 = Integer.toString(c);String str2 = String.valueOf(c);String str3 = c + "";//注意空字符串就是空的 而不是空格
将字符串转换成基本类型有两种方法:
1. 调用包装类的 parseXxx 静态方法
2. 调用包装类的 valueOf() 方法转换为基本类型的包装类,会自动拆箱
//将字符串转换为基本类型String str = "8";int a = Integer.valueOf(str);int b = Integer.parseInt(str);
使用Date类和SimpleDateFormat类表示时间
使用 Date 类的默认无参构造方法创建出的对象就代表当前时间:
Date now = new Date();//使用默认的构造方法创建Date对象System.out.println(now);//输出//结果为 Wed Sept 13 15:10:22 CST 2017
CST代表China Standard Time(中国标准时间 即北京时间)
要想控制输出格式则需SimpleDateFormat类
//得到当前时间Date d = new Date();//规定输出格式(规定不同输出则不同)SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");//调用format方法,对Date的对象进行操作String now = sdf.format(d);
还可使用parse()方法将文本转换为日期
//创建日期格式的字符串String day = "2017年9月13日 15:23:56";//指定字符串日期格式SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");//调用parse()方法,将字符串转换为日期Date day = df.parse(day);//若输出 则为Wed Sept 13 15:23:56 CST 2017
注:1、 调用 SimpleDateFormat 对象的 parse() 方法时可能会出现转换异常,即 ParseException ,因此需要进行异常处理
2、 使用 Date 类时需要导入 java.util 包,使用 SimpleDateFormat 时需要导入 java.text 包
Calendar类
Date类中的很多方法已经过时,因此我们通常使用 Calendar 类进行时间和日期的处理
Calendar类是抽象类,且其构造方法为protected的,因此不能用构造方法来创建对象,API中提供了getInstance方法来创建对象
Calendar time = Calendar.getInstance();int year = time.get(Calendar.YEAR);int month = time.get(Calendar.MONTH)+1;//注意要+1,0表示1月份int day = time.get(Calendar.DAY_OF_MONTH);int hour = time.get(Calendar.HOUR_OF_DAY);int minute = time.get(Calendar.MINUTE);int second = time.get(Calendar.SECOND);System.out.println("当前时间:" + year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second);
Calendar.YEAR 等为 Calendar 类中定义的静态常量,因此要注意大写
Calendar 类提供了 getTime() 方法,用来获取 Date 对象,完成 Calendar 和 Date 的转换,还可通过 getTimeInMillis() 方法,获取此 Calendar 的时间值,以毫秒为单位。
Date date = time.getTime();//转换为Date对象Long millisSum = time.getTimeInMillis();//获取当前毫秒数
= =注意Calendar的拼写
Math类
Math 类位于 java.lang 包中,包含用于执行基本数学运算的方法, Math 类的所有方法都是静态方法,所以使用该类中的方法时,可以直接使用类名.方法名,如: Math.round();
Math类常用方法:
// 产生10以内的随机数 int x = (int) (Math.random()*10);
- java中的常用类(包装类、Date类、Calendar类和Math类)
- 黑马程序员--常用API(String、基本数据类型的包装类、System、Runtime、Date、Calendar、Math)
- Java API(Math类和Random类 包装类 Date类、Calendar类和DateFormat类)
- 常用类:System,Runtime,Math,Date,Calendar
- JAVA——(3)常用类:Math、Scanner、String、StringBuffer、Array、Calendar、Date、Character、包装类(Intger)
- JavaEE常用API【Date、DateFormat、Calendar、Math、System、包装类、正则表达式】总结
- day06-常用API【Date、DateFormat、Calendar、Math、System、包装类、正则表达式】
- 【Date、DateFormat、Calendar、Math、System、包装类、正则表达式】常用API
- Java中的常用工具类(Collectios、Arrays、System、Date、Calendar、Math)
- java中的Date和Calendar类的使用.还有math类种的函数使用
- JavaSE----API之常用类(Math、Random、System、BigInteger和BigDecimal、Date和DateFormat、Calendar)
- Java中的Date类和Calendar类
- java中的Date类和Calendar类
- 使用Java中的Date和Calendar类
- 使用Java中的Date和Calendar类
- 使用Java中的Date和Calendar类
- Java中的Date和Calendar类
- 使用Java中的Date和Calendar类
- 集合章节
- cocos2dx基础——场景类
- Understanding top command in unix
- 将两个有序链表合成一个有序链表
- 什么场景该用 MongoDB?
- java中的常用类(包装类、Date类、Calendar类和Math类)
- OSI七层与TCP/IP五层网络架构详解
- 金蝶K3 BOM独立控制跳层开关开发
- On Linux, how to tell how many cores of the machine are active?
- Vue.js2.0 入门实例
- Spring Boot参考教程(三)内部应用监控(Actuator)
- os
- Python定时器
- git stash 缓存以及恢复工作进度