一篇搞定java常用类库,泛型的三种使用方式,以及Arrays,BigDecimal,Date,Calendar,System你都会了吗?

泛型

概念:泛型,即“参数化类型”。就是将类型由原来的具体的类型参数化,类似于方法中的变量参数,此时类型也定义成参数形式(可以称之为类型形参),然后在使用/调用时传入具体的类型(类型实参)。

如何用

第一种,泛型类

public class ClassName<T>{
	private T data;
	public T getData() {
	return data;
	}
	public void setData(T data) {
	this.data = data;
	}
}

创建对象时传入String作为泛型,在调用对象方法时显示接受的就是String
在这里插入图片描述

第二种,泛型接口

public interface IntercaceName<T>{
T getData();
}

实现接口时,可以选择指定泛型类型,也可以选择不指定, 如下:
指定类型:

public class Interface1 implements IntercaceName<String> {
	private String text;
	@Override
	public String getData() {
	return text;
	}
}

不指定类型:

public class Interface1<T> implements IntercaceName<T> {
	private T data;
	@Override
	public T getData() {
	return data;
	}
}

第三种,泛型方法

private static <T> T 方法名(T a, T b) {}

在这里插入图片描述

泛型限制类型

<T extends 类或接口1 & 接口2>

在这里插入图片描述

泛型中的通配符 ?

类型通配符是使用?代替方法具体的类型实参。
在这里插入图片描述
可见,多态转型失败
1、 <? extends Parent> 指定了泛型类型的上届
在这里插入图片描述

2 、<? super Child> 指定了泛型类型的下届
在这里插入图片描述

3 、<?> 指定了没有限制的泛型类型,有点像Object
在这里插入图片描述

泛型的好处

1、 提高代码复用率
2、 泛型中的类型在使用时指定,不需要强制类型转换(类型安全,编译器会检查类型)

注意:在编译之后程序会采取去泛型化的措施,也就是说Java中的泛型,只在编译阶段有效

Objects 1.7以后出现

此类包含static实用程序方法,用于操作对象或在操作前检查某些条件。
在这里插入图片描述

System系统类

简介:
1、System 类包含一些有用的类字段和方法。它不能被实例化。
2、在 System 类提供的设施中,有标准输入、标准输出和错误输出流;对外部定义的属性和环境变量的访问;加载文件和库的方法;还有快速复制数组的一部分的实用方法。

PrintStream err:“标准”错误输出流。
InputStream in:“标准”输入流。
PrintStream out:“标准”输出流。

常用方法
void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) :从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。
long currentTimeMillis() :返回以毫秒为单位的当前时间。  
void exit(int status) :终止当前正在运行的 Java 虚拟机。
void gc():运行垃圾回收器。  
Properties getProperties():确定当前的系统属性。 
String getProperty(String key):获取指定键指示的系统属性。 
void setErr(PrintStream err) :重新分配“标准”错误输出流。 
void setIn(InputStream in):重新分配“标准”输入流。 
void setOut(PrintStream out):重新分配“标准”输出流。 
String setProperty(String key, String value) :设置指定键指示的系统属性。 

JDK1.8之前日期时间类

在这里插入图片描述

java.lang.System类

System类提供的public static long currentTimeMillis()用来返回当前时间与1970年1月1日0时0分0秒GMT之间以毫秒为单位的时间差

java.util.Date

它的对象表示一个特定的瞬间,精确到毫秒。
理解:一维的时间轴,选择1970年1月1日0时0分0秒时间为0刻度,1毫秒一刻度
构造方法:
Date(): 源代码:this(System.currentTimeMillis());
Date(long date) :分配 Date对象并初始化它以表示自1970年1月1日00:00:00以来的指定毫秒数
常用方法:
getTime():返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
toString():把此 Date 对象转换为以下形式的 String: dow mon dd hh:mm:ss zzz yyyy 其中:
dow 是一周中的某一天 (Sun, Mon, Tue, Wed, Thu, Fri, Sat), zzz是时间标准。

java.util.Calendar

Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。瞬间可用毫秒值来表示,它是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00.000,格里高利历)的偏移量。
人们对于时间的认识是:某年某月某日,这样的日期概念。计算机是long类型的数字。通过Calendar在二者之间搭起桥梁。而且Calendar提供了很多关于日期时间计算的方法。
在这里插入图片描述
GregorianCalendar(公历)是Calendar的一个具体子类,提供了世界上大多数国家/地区使用的标准日历系统。

 public static void main(String[] args) {
        //默认语言环境的时间(时区)
        Calendar c = new GregorianCalendar();

        int year=c.get(Calendar.YEAR);
        int month=c.get(Calendar.MONTH);
        int date=c.get(Calendar.DAY_OF_MONTH);

        int hour=c.get(Calendar.HOUR_OF_DAY);
        int minute=c.get(Calendar.MINUTE);
        int second=c.get(Calendar.SECOND);
        int mill=c.get(Calendar.MILLISECOND);
        int week=c.get(Calendar.DAY_OF_WEEK);

        StringBuffer dateStr=new StringBuffer();
        dateStr.append(year).append("年");
        dateStr.append(month+1).append("月");
        dateStr.append(date).append("日").append("  ");
        dateStr.append(hour).append("时");
        dateStr.append(minute).append("分");
        dateStr.append(second).append("秒");
        dateStr.append(mill).append("毫秒").append("  ");

        String[] weeks={"日","一","二","","四","五","六"};
        dateStr.append("星期").append(weeks[week-1]);

        System.out.println(dateStr);
    }
public static void main(String[] args) {
		Calendar c = new GregorianCalendar(2015, 6, 13);
//		c.set(2016, Calendar.DECEMBER, 4, 12, 12, 0);
//		c.setTime(new Date());
		//15天之后
		//c.add(Calendar.DATE, 15);
		//2个月之前
		//c.add(Calendar.DAY_OF_MONTH, -2);
		//12小时之后
		c.add(Calendar.HOUR, 12);
		
		Date time = c.getTime();//转成日期
		System.out.println(time);
	}

java.text.DateFormat和SimpleDateFormat

y , M, d H m s 注意大小写

public static void main(String[] args) {
		Date date = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss 是本年的第几D");
		System.out.println(sf.format(date));
		
		String s = "2020-08-01 14:12:23";
		SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date d = sf2.parse(s);
			System.out.println(d);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

在这里插入图片描述

数学相关类BigInteger、BigDecimal

BigInteger可以表示不可变的任意精度的整数

构造方法:
BigInteger(String val):根据字符串构建BigInteger对象
常用方法:
BigInteger add(BigInteger val) :返回其值为 (this + val) 的 BigInteger。
BigInteger subtract(BigInteger val) :返回其值为 (this - val) 的 BigInteger。
BigInteger multiply(BigInteger val) :返回其值为 (this * val) 的 BigInteger。
BigInteger divide(BigInteger val) :返回其值为 (this / val) 的 BigInteger。整数相除只保留整数部分。
BigInteger remainder(BigInteger val) :返回其值为 (this % val) 的 BigInteger。
BigInteger[] divideAndRemainder(BigInteger val):返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组。
BigInteger pow(int exponent) :返回其值为 (thisexponent) 的 BigInteger。

	@Test
	public void test2(){
//		long num1 = 12345678901234567890L;//out of range 超过long的范围
		BigInteger num1 = new BigInteger("12345678901234567890");
		BigInteger num2 = new BigInteger("92345678901234567890");
		
//		System.out.println("和:" + (num1 + num2));//错误的
		System.out.println("和:" + num1.add(num2));
		System.out.println("减:" + num1.subtract(num2));
		System.out.println("乘:" + num1.multiply(num2));
		System.out.println("除:" + num2.divide(num1));//两个整数相除只保留整数部分
		System.out.println("幂次方:" + num2.pow(5));
	}

在商业计算中,要求数字精度比较高,所以用到java.math.BigDecimal类。BigDecimal类支持不可变的、任意精度的有符号十进制定点数。

构造器:
BigDecimal(double val)
BigDecimal(String val)
常用方法:
BigDecimal add(BigDecimal augend) :返回一个 BigDecimal,其值为 (this + augend),其标度为 max(this.scale(), augend.scale())。
BigDecimal subtract(BigDecimal subtrahend) :返回一个 BigDecimal,其值为 (this - subtrahend),其标度为 max(this.scale(), subtrahend.scale())。
BigDecimal multiply(BigDecimal multiplicand):返回一个 BigDecimal,其值为 (this × multiplicand),其标度为 (this.scale() + multiplicand.scale())。
BigDecimal pow(int n) :返回其值为 (thisn) 的 BigDecimal,准确计算该幂,使其具有无限精度。
BigDecimal divide(BigDecimal divisor): 返回一个 BigDecimal,其值为 (this / divisor),其首选标度为 (this.scale() - divisor.scale());如果无法表示准确的商值(因为它有无穷的十进制扩展),则抛出 ArithmeticException。
BigDecimal divide(BigDecimal divisor, int roundingMode) :返回一个 BigDecimal,其值为 (this / divisor),其标度为 this.scale()。
BigDecimal divide(BigDecimal divisor, int scale, int roundingMode) :返回一个 BigDecimal,其值为 (this / divisor),其标度为指定标度。

	@Test
	public void test3(){
		BigDecimal num1 = new BigDecimal("-12.1234567890123456567899554544444332");
		BigDecimal num2 = new BigDecimal("89.6734567890123456567899554544444333");
		System.out.println("和:" + num1.add(num2));
		System.out.println("减:" + num1.subtract(num2));
		System.out.println("乘:" + num1.multiply(num2));
		System.out.println("除:" + num2.divide(new BigDecimal("2")));//可以整除(除尽)就对,不能整除就报异常
		System.out.println("除:" + num2.divide(num1,BigDecimal.ROUND_HALF_UP));
		System.out.println("除:" + num2.divide(num1,BigDecimal.ROUND_DOWN));//往零的方向舍去
		System.out.println("除:" + num2.divide(num1,BigDecimal.ROUND_FLOOR));//往小的方向舍去
		System.out.println("除:" + num2.divide(num1,BigDecimal.ROUND_CEILING));//往大的方向舍去
	}
	```

猜你喜欢

转载自blog.csdn.net/qq_43206800/article/details/108281851