您的位置:首页 > 其它

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

2017-01-04 23:47 691 查看
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);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐