java —— 常用类

一、System 类

System 类内部的构造方法是 private 修饰的,所以不能实例化,普通方法均为静态方法。

(一).currentTimeMillis()

括号内无参数,返回值为距离1970年1月1日0时0分0秒之间的毫秒数。

long time=System.currentTimeMillis();

(二).arraycopy()

括号内 5 个参数,分别是 .arraycopy(原数组,要复制原数组的开始索引,目标数组,复制到目标数组的开始索引,要复制原数组的长度)。

public static void main(String[] args)
{
    int[] a1={0,1,2,3};
    int[] a2=new int[7];
    System.arraycopy(a1,1,a2,2,a1.length-1);
    System.out.println(Arrays.toString(a2));
}

(三).exit()

括号内的参数要么是 0,要么是非零值,前者表示强制退出程序,后者需要根据需要自定义。

System.exit(0);

二、Date 类

Date 类需要实例化,实例化之后用 .getTime()、.getHour、.getDay() 等等方法,除 .getTime() 返回 long 类型之外,其余均返回 int 类型。

Date d=new Date();
d.getTime();   //与System.currentTimeMillis()一样返回距离格林威治标准时间之间的毫秒数
d.getHour();
d.getDay();

三、DateFormat 类

DateFormat 是一个将日期进行格式化显示的类。

(一)对日期格式化显示

.getDateInstance();

public static void main(String[] args)
{
    Date date=new Date();
    DateFormat df=DateFormat.getDateInstance();  //将工具类对象实例化为df
    String x=df.format(date);
    System.out.println(x);
}

(二)对时间格式化显示

 .getDateTimeInstance();

public static void main(String[] args)
{
    Date date=new Date();
    DateFormat df=DateFormat.getDateTimeInstance(); //将工具类对象实例化为df
    String x=df.format(date);
    System.out.println(x);
}

四、SimpleDateFormat 类

SimpleDateFormat 主要提供的是日期与字符串之间的转换。

(一)日期转字符串

public static void main(String[] args)
{
    Date date=new Date();
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); //编写输出格式
    String x=sdf.format(date);
    System.out.println(x);
}

(二)字符串转日期

public static void main(String[] args)
{
    String x="2020年5月10日10时05分30秒";
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日hh时mm分ss秒"); //编写来源格式
    Date date=sdf.parse(x);
    System.out.println(date);
}

五、Math类

求绝对值:Math.abs(double a)

求平方根:Math.sqrt(double a)

求较大值:Math.max(double a,double b)

求较小值:Math.min(double a,double b)

四舍五入:Math.round(double a)             //结果是整数

获取 0-1 之间的随机数:Math.random()

六、Random类

Random 生成随机数,需要先创建 Random 实例化对象。

public static void main(String[] args)
{
    Random rand=new Random();
    int x=rand.nextInt(10);    //随即生成0-9之间的整数
    System.out.println(x);
}

七、BigInteger 类

 BigInteger 表示大整数类,内部封装了常见的数学运算。使用时需要先声明一个 BigInteger 对象,数字用字符串的形式表示。

public static void main(String[] args)
{
    BigInteger bi1=new BigInteger("5555555555555555555");
    BigInteger bi1=new BigInteger("2222222222222222222");
    
    BigInteger bi3=bi1.add(bi2);             //求和
    BigInteger bi3=bi1.subtract(bi2);        //求差
    BigInteger bi3=bi1.multiply(bi2);        //求积
    BigInteger bi3=bi1.divide(bi2);          //取整
    BigInteger bi3=bi1.remainder(bi2);       //取余
}

八、BigDecimal 类

BigDecimal 用于处理超大型小数,使用方法与 BigInteger 类似。

public static void main(String[] args)
{
    BigDecimal bi1=new BigDecimal("1.5555555555555555555");
    BigDecimal bi1=new BigDecimal("0.2222222222222222222");
    
    BigDecimal bi3=bi1.add(bi2);             //求和
    BigDecimal bi3=bi1.subtract(bi2);        //求差
    BigDecimal bi3=bi1.multiply(bi2);        //求积
    BigDecimal bi3=bi1.divide(bi2,3,BigDecimal.ROUND_HALF_UP);          //求商
    BigDecimal bi3=bi1.remainder(bi2);       //取余
}

本案例中,bi1 除以 bi2 显然结果是无限小数,由于 BigDecimal 本身就是处理超大型小数的,所以会报错,否则就要无限显示下去。案例中,括号内增加的两个参数,3 表示保留三位小数,BigDecimal.ROUND_HALF_UP 表示四舍五入。

九、Timer 类和 TimerTask 类

两个类配合使用,以实现定时完成某项任务。

public static void main(String[] args)
{
    Timer ti=new Timer();                //实例化Timer对象
	
    TimerTask tisk=new TimerTask()
	{
		public void run() 
		{
			System.out.println("kaka");      //编写任务
		}
	};
		
    ti.schedule(tisk,3000);             //第一种:3秒钟之后执行任务tisk

    ti.schedule(tisk,new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒").parse("2025年3月1日 5时20分15秒"));                           
                                       //第二种:固定时间点执行任务tisk

    ti.schedule(tisk,3000,2000);       //第三种:3秒之后执行,每2秒执行一次
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值