day11 常用类 System Runtime Math Random Date DateFormat SimpleDateFormat Calendar

System

arraycopy

currentTimeMillis

exit

gc

package system_01;

import java.util.Arrays;

public class SystemDemo {
	public static void main(String[] args) {
		int[] arr = {1,2,3,4,5,6,7,8};
		int[] arr2 = new int[10];
		System.arraycopy(arr, 6, arr2, 0, 2);
		System.out.println(Arrays.toString(arr2));
		System.out.println("----------------------------------------");
		long time = System.currentTimeMillis();
		System.out.println(time);
		System.out.println("----------------------------------------");
		//System.exit(0); 运行后直接退出了
		//System.out.println("111111111");
		new SystemDemo();
		new SystemDemo();
		System.gc();
	}

	@Override
	protected void finalize() throws Throwable {
		System.out.println("结束");
	}
	
	

}

Runtime

getRuntime

exec

package _02_Runtime;

import java.io.IOException;

public class RuntimeDemo {

	public static void main(String[] args) throws IOException {
		Runtime run = Runtime.getRuntime();
		run.exec("notepad");
	}

}
数学相关的类

Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。 

Random类,生成随机数    我想求23到123的随机数,其实可以换个思路,23+(int)(Math.random(100)+1)

Random random = new Random();
		int xy = random.nextInt(100);
		System.out.println(xy);
ThreadLocalRandom类 1.7出来的,提供了更加安全的随机数 是单例模式的

ThreadLocalRandom tlr = ThreadLocalRandom.current();
		int wu = tlr.nextInt(0, 11);
		System.out.println(wu);
UUID类:生成通用唯一标识符 一般用于数据库的主键,作为令牌机制的标志.

String str1 = UUID.randomUUID().toString();
		String str2 = UUID.randomUUID().toString();
		System.out.println(str1);
		System.out.println(str2);
BigInteger和BigDecimal
BigInteger就是比long还要大,BigDecimal确保精度的问题

package _05_BigInteger_BigDecimal;

import java.math.BigDecimal;

public class BigDecimalDemo {
	public static void main(String[] args) {
		double d1 = 2.0;
		double d2 = 1.9;
		System.out.println(d1-d2);
		BigDecimal b1 = new BigDecimal("2.0");
		BigDecimal b2 = new BigDecimal("1.9");
		System.out.println(b1.subtract(b2));
	}

}
日期相关的类

Date

DateFormat   做日期格式转化

SimpleDateFormat使用自定义的时间格式

DateFormat是SimpleDateFormat的父类

package _06_Date;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateDemo {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		Date date = new Date();
		System.out.println(date);
		System.out.println("----------------------------------------");
		DateFormat format = DateFormat.getDateInstance();
		String time = format.format(date);
		String time2 = DateFormat.getDateTimeInstance().format(date);
		System.out.println(time);
		System.out.println(time2);
		//String转到date
		Date date2 = format.parse(time);
		System.out.println(date2);
		System.out.println("----------------------------------------");
		String pattern = "yyyy年MM月dd日 HH:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		String timesdf = sdf.format(date);
		System.out.println(timesdf);
	}

}
Calendar 表示日历,也是个抽象类

package _06_Date;

import java.util.Calendar;

public class CalendarDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Calendar calendar = Calendar.getInstance();
		
		getDate(calendar);
		//设置偏移量
		calendar.add(Calendar.YEAR, 1);
		getDate(calendar);	
		
		//把Calendar转化为date
		System.out.println(calendar.getTime());
	}
	public static void getDate(Calendar calendar){
		int year  = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int date  = calendar.get(Calendar.DATE);
		int hours = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		int second = calendar.get(Calendar.SECOND);
		System.out.println(year+"年"+month+"月"+date+"日"+"   "+hours+":"+minute+":"+second);
		
	}

}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值