Date類
一般用於獲取時間
Date date1 = new Date();//獲取當前系統時間 Date date2 = new Date(10000);//獲取從標准基准時間起10000毫秒的時間點
Calendar類
Calendar是一個抽象類,一般是用來獲取時間和對時間做對象的操作
Calendar c = Calendar.newInstance();//newInstance方法是一個靜態的方法,直接通過類名調用
System.out.println(c.get(Calendar.DATE));//使用get方法獲取當前日歷的日期屬性值
System.out.println(c.getActualMaximum(Calendar.DATE)); //使用get方法獲取當前日歷的日期屬性值
c.set(Calendar.DATE, 2017); //使用set方法修改日歷時間
SimpleDateFormat類
SimpleDateFormat是一個用來解析(文本轉時間)和格式化(時間轉文本)日期的工具類
//解析 String dateStr = "2017-12-01 上午 10:10:10"; //原文本 String format = "yyyy-MM-dd a hh:mm:ss"; //定義一種解析格式 SimpleDateFormat sdf = new SimpleDateFormat(format); //實例化SimpleDateFormat Date date = sdf.parse(dateStr); //使用parse方法轉成日期格式 //格式化
Date date = new Date(); //原時間
String format = "yyyy-MM-dd a hh:mm:ss"; //定義一個格式
SimpleDateFormat sdf = new SimpleDateFormat(format); //實例化SimpleDateFormat
String dateStr = sdf.format(date); //使用format方式轉成文本格式
Math類
產生隨機的方式
//產生一個3~9之間的隨機數 int a = (int)(Math.random()*(9-3+1)+3); //9-3+1是壓縮區間,然后在平移3
random類
Random random = new Random(10);//以10為種子,使用線性同余公式產生偽隨機數 int i1 = random.nextInt();//產生一個隨機整數 int i2 = random.nextInt(10);//產生一個10以內的隨機整數 double d = random.nextDouble();//產生一個隨機double值 boolean b = random.nextBoolean();//產生一個隨機boolean值 random.setSeed(20);//將隨機數種子設置為20