Java常用的类

其他一些常用的类

Date

Date就是一个简单的日期类,以毫秒数为单位,从1970年1月1日00:00:00开始经过的毫秒数,之后再通过毫秒数计算出年、月、日
等 1000毫秒=1秒
构造方法:
public Date():返回当前时间
public Date(long data):返回指定毫秒数的对应日期常用方法:
getTime():获取当前时间的毫秒数
setTime():设置时间的毫秒数

public static void main(String[] args) {
// 获取当前日期
Date date = new Date();
System.out.println(date);
// getTime():获取当前时间的毫秒数
System.out.println(date.getTime()); //1657259431651
System.out.println(date.getYear()); // 返回一个值,该值是从包含本开始时间的年份中减去1900
date.setTime(1657259400000L);
System.out.println(date);
}

SimpleDateFormat

日期格式化类,可以按照实际的日期格式转换,可以按照你需要指
定字符串格式转换
yyyy:表示年
MM:表示月 dd:表示天
HH:表示时
mm:表示分
ss:表示秒

public static void main(String[] args) {
// 获取当前日期
Date date = new Date();
System.out.println(date);
System.out.println("============日期格式化=============");// SimpleDateFormat sdf = new
SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
SimpleDateFormat sdf = new
SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String newDate = sdf.format(date);
System.out.println(newDate);
}

Calendar

Calendar:日历类 不能直接创建对象,通过Calendar.getInstance():获取当前的时间
常用方法:get(int field) 获取时间字段值set(int year,int month,int date) 设置年月日 注意月减1
getTime() 把日历对象转成日期对象

System.out.println("=============日历类=============");
Calendar c = Calendar.getInstance();
System.out.println(c); // 获取年
int year = c.get(Calendar.YEAR);// 获取月
int month = c.get(Calendar.MONTH)+1;// 获取日
int day = c.get(Calendar.DAY_OF_MONTH);
System.out.println(year+"."+month+"."+day);// 设置年月日 设置2022-07-07 注意月份要减1
c.set(2022,7-1,7);
System.out.println(c);// getTime() 把日历对象转成日期对象
Date d = c.getTime();
System.out.println(sdf.format(d));

LocalDate、LocalTime和LocalDateTime

LocalDate 是一个不可变的日期时间对象,表示日期,通常被视为年月日。 也可以访问其他日期字段,例如日期,星期几和星期。 例如,值“2007年10月2日”可存储在LocalDate
now() 从默认时区的系统时钟获取当前日期 getYear() 获取年份字段
getMonth() 使用 Month枚举获取月份字段 LocalTime是一个不可变的日期时间对象,代表一个时间,通常被看作是小时 - 秒。 时间表示为纳秒精度
getHour() 获取时间字段。 getMinute() 获取小时字段。
LocalDateTime.now() 获取日期时间
LocalDateTime.of(2020, 4,01,14,40,30) 设置指定的日期时间

Math

Math数学工具类,包含执行基本数字运算的方法
abs(int a)绝对值
ceil(double a)向上取整
floor(double a)向下取整 round(double a)四舍五入
pow(double a,double b) 求a的b次方
random() 0到1之间的小数
max(int a,int b) 返回a和b的最大值
min(int a,int b) 返回a和b的最小值

public static void main(String[] args) {
// 圆周率
double pi = Math.PI;
System.out.println(pi);
// abs(int a)绝对值
System.out.println(Math.abs(-3)); // 3
// ceil(double a)向上取整
System.out.println(Math.ceil(-3.14)); // -3
// floor(double a)向下取整
System.out.println(Math.floor(-3.14)); // -4
// round(double a)四舍五入
System.out.println(Math.round(-3.14)); // -3
// pow(double a,double b) 求a的b次方
System.out.println(Math.pow(2,4)); // 16
// random() 0到1之间的小数
System.out.println(Math.random());
// 生成1-100的随机整数
System.out.println((int)(Math.random()*10));
}

System

System是系统类,定义在 java.lang 包中。System 类中定义了一些常用的类字段和方法,该类不能实例化。
常用方法:

gc(): java虚拟机回收长期不使用的对象(垃圾)

exit(int status):让操作系统终止该java程序的运行 0表示正常终止
System.out.print 标准的输出
System.err.print 错误的输出
System.getProperty(String key) 获取指定系统的属性,返回对应的名称
currentTimeMillis():返回当前系统时间距离 1970-1-1 0:0:0 的毫秒值
arraycopy(Object src, int srcPos,Object dest, int destPos,int
length):从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。常用于数组的插入和删除

public static void main(String[] args) {
System.out.println("out1"); // 标准输出
System.err.println("err2"); // 错误输出
System.out.println("out2");
// 获取指定操作系统的名称
String pro = System.getProperty("os.name");
System.out.println(pro); // Windows 10
System.exit(0); // jvm退出
System.err.println("err3");
System.out.println("out3");
System.err.println("err1");
}

Runtime

概述

每个应用程序都有一个 Runtime 类的实例,使得应用程序能够和其运行的环境相连接。

应用程序不能创建自己的 Runtime 实例,只能通过
getRuntime() 方法获取当前的运行时。

常用方法

getRuntime():返回与当前 Java 应用程序相关的运行时对象
totalMemory():返回 Java 虚拟机中的内存总量(此方法返回的值可能随时间的推移而变化,这取决于主机环境)
freeMemory():返回 Java 虚拟机中的空闲内存量(调用 gc 方法可能导致 freeMemory 返回值的增加)

public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
Runtime runtime1 = Runtime.getRuntime();
System.out.println(runtime == runtime1); //
true
long totalMemory = runtime.totalMemory();
System.out.println("totalMemory = " +
totalMemory); // 512753664
long freeMemory = runtime.freeMemory();
System.out.println("freeMemory = " +
freeMemory); // 504700544
}

大数运算类

概述

基本数据类型 long 、double 都有其取值范围,一般遇到超过范围
的数据,就需要用来大数运算类了。
● java.math.BigInteger :大整数。
● java.math.BigDecimal :大浮点。

Biglnteger

●构造方法:

public BigInteger(String val){}

●加法:

public BigInteger add(BigInteger val){}

●减法:

public BigInteger subtract(BigInteger val) {}

●乘法:

public BigInteger multiply(BigInteger val){}

●除法:

public BigInteger divide(BigInteger val){}

●余数:

public BigInteger remainder(BigInteger val){}

●示例:

public static void main(String[] args) {
BigInteger b1 = new BigInteger("124");
BigInteger b2 = new BigInteger("123");
System.out.println("加法:" + b1.add(b2)); //
加法:247
System.out.println("减法:" +
b1.subtract(b2)); // 减法:1
System.out.println("乘法:" +
b1.multiply(b2)); // 乘法:15252
System.out.println("除法:" + b1.divide(b2));
// 除法:1
System.out.println("余数:" +b1.remainder(b2)); // 余数:1
}
RoundingMode枚举类

●UP :远离零方向舍入的舍入模式。
●DOWN :向零方向舍入的舍入模式。
●CEILING :向正无限大方向舍入的舍入模式。
●FLOOR :向负无限大方向舍入的舍入模式。
●HALF_UP :向最接近数字方向舍入的舍入模式,如果与两个相邻
数字的距离相等,则向上舍入。
●HALF_DOWN :向最接近数字方向舍入的舍入模式,如果与两个
相邻数字的距离相等,则向下舍入。
●HALF_EVEN :向最接近数字方向舍入的舍入模式,如果与两个相
邻数字的距离相等,则向相邻的偶数舍入。
●UNNECESSARY :用于断言请求的操作具有精确结果的舍入模
式,因此不需要舍入。

BigDecimal
  • 构造方法:public BigDecimal(String val) {}

●加法:public BigDecimal add(BigDecimal augend){}
●减法:public BigDecimal subtract(BigDecimal subtrahend){}
●乘法:public BigDecimal multiply(BigDecimal multiplicand){}
●除法:public BigDecimal divide(BigDecimal divisor){} public BigDecimal divide(BigDecimal divisor,
RoundingMode roundingMode){}

●余数:public BigDecimal remainder(BigDecimal divisor){}
示例:

public static void main(String[] args) {
BigDecimal b1 = new BigDecimal("3.55");
BigDecimal b2 = new BigDecimal("2.12");
System.out.println("加法:" + b1.add(b2));
System.out.println("减法:" +
b1.subtract(b2));
System.out.println("乘法:" +
b1.multiply(b2));
System.out.println("除法:" + b1.divide(b2,
2, RoundingMode.HALF_UP));
}

包装类

Java中提供了八个基本数据类:
byte short int long float double char boolean
Java将基本数据类型封装成对象包装类,对应的包装类Byte Short Integer Long Float Double Character Boolean
除了int和char不一样是Integer,Character,其他六个都是首字母大写

八个包装类是引用数据类型,因为有时候不能使用基本数据类型,这时候就要通过引用类型的包装经过自动拆箱,转成基本数据类型。
包装类和基本数据类型可以互相转换:自动拆箱和自动装箱自动拆箱: 包装类==》基本数据类型
将基本数据类型包装类的对象转换成该基本数据类型的数值自动装箱: 基本数据类型==》包装类
将基本数据类型的数值转换成基本类型对应的包装类对象

public static void main(String[] args) {
Integer i = 10;
int a = 20;
Integer b = a;
// String==>int
String s = "10";
int aa = Integer.parseInt(s);
// int==>String
String ss = ""+10;
Integer ii = Integer.valueOf(10);
}
  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值