java入门知识点7:java的几种常用类

1.包装类
2.基本类型和包装类之间的转换
3.基本类型和字符串之间的转换
4.Date 和 SimpleDateFormat 类表示时间
5.Calendar 类
6.Math类

1.包装类
在这里插入图片描述
以Integer类型为例:
Integer类型的构造方法:
在这里插入图片描述
Integer包装类的常用方法:
在这里插入图片描述

        
	public static void main(String[] args) {	
	int score1 = 86; 
        
		
	Integer score2=new Integer(score1);
        
// 将Integer包装类转换为double类型
			
	double score3=score2.doubleValue();
        // 将Integer包装类转换为float类型

		
			
	float score4=score2.floatValue();
        
		// 将Integer包装类转换为int类型
		 
	int score5 =score2.intValue();

		
	System.out.println("Integer包装类:" + score2);
		
	System.out.println("double类型:" + score3);
		
	System.out.println("float类型:" + score4);
		
	System.out.println("int类型:" + score5);
	
}

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

int i = 10;   //定义int基本类型值
Integer x = new Integer(i); // 手动装箱
Integer y = i;    //自动装箱

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

Integer j = new Integer(8); //定义一个Integer包装类对象
int m = j.intValue();		//手动拆箱为Int类型
int n = j;		//自动拆箱为Int类型

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

基本类型转化为字符串的三种方法:
1>使用包装类的 toString() 方法
2>使用String类的 valueOf() 方法
3>用一个空字符串加上基本类型,得到的就是基本类型数据对应的字符串

int c = 10;
String str1 = Integer.toString(c);
String str2 = String.valueOf(c);
String str3 = c +"";

字符串转换成基本类型有两种方法:
1>调用包装类的 parseXxx 静态方法
2>调用包装类的 valueOf() 方法转换为基本类型的包装类,会自动拆箱

String str = "8";
int d = Integer.parseInt(str);
int e = Integer.valueOf(str);

4.Date 和 SimpleDateFormat 类表示时间

java.util 包中的Date类
使用 Date 类的默认无参构造方法创建出的对象就代表当前时间
java.text 包中的 SimpleDateFormat 类
对日期时间进行格式化,如可以将日期转换为指定格式的文本,也可将文本转换为日期

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

public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Date d = new Date();
		
		SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String today = s.format(d);
		System.out.println(today);
}
运行结果:当下时间

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


public static void main(String[] args) throws ParseException {
		// TODO Auto-generated method stub
		String day = "2019-8-7 19:5:0";
		SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = s.parse(day);
		System.out.println(date);
}
运行结果:Wed Aug 07 19:05:00 CST 2019
CST 代表 China Standard Time (中国标准时间)

注意:
调用 SimpleDateFormat 对象的 parse() 方法时可能会出现转换异常,即 ParseException ,因此需要进行异常处理

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

Calendar 类提供了 getTime() 方法,用来获取 Date 对象,完成 Calendar 和 Date 的转换,还可通过 getTimeInMillis() 方法,获取此 Calendar 的时间值,以毫秒为单位。


 public static void main(String[] args) {
		
		Calendar c = Calendar.getInstance();// 创建Calendar对象
		
        Date date = c.getTime();// 将Calendar对象转换为Date对象
		
		Long time = c.getTimeInMillis(); //或许当前毫秒数 
		
		// 创建SimpleDateFormat对象,指定目标格式
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:ms");
        
		// 将日期转换为指定格式的字符串
		String now = sdf.format(date);
		System.out.println("当前时间:" + now);
	}

运行结果:当前时间:2019-08-07 19:33:00:330

6.Math类
Math 类位于 java.lang 包中, Math 类的所有方法都是静态方法,直接使用类名.方法名,如: Math.round();

猜你喜欢

转载自blog.csdn.net/qq_43501462/article/details/98780465