常用API

Math:提供了操作數(shù)學(xué)運算的方法。都是靜態(tài)的。

常用的方法:
ceil():返回大于參數(shù)的最小整數(shù)。
floor():返回小于參數(shù)的最大整數(shù)。
round():返回四舍五入的整數(shù)。
pow(a,b):a的b次方。

返回一個隨機(jī)數(shù):
Math.random():返回一個大于等于0,小于1的隨機(jī)數(shù),是帶正號的double型

int d = (int)(Math.random()*6)+1;

Random r = new Random();
調(diào)用nextInt()、nextDouble()、nextLong()、nextFloat()、等

Random r = new Random();
int d = r.nextInt(6)+1;

Runtime:沒有構(gòu)造方法摘要,說明該類不可以創(chuàng)建對象。
又發(fā)現(xiàn)還有非靜態(tài)的方法。說明該類應(yīng)該提供靜態(tài)的返回該類對象的方法。
而且只有一個,說明Runtime類使用了單例設(shè)計模式。

      Runtime r = Runtime.getRuntime();
        
//      execute: 執(zhí)行。 xxx.exe 
        
        Process p = r.exec("notepad.exe");
        Thread.sleep(5000);
        p.destroy();//關(guān)進(jìn)程

System:類中的方法和屬性都是靜態(tài)的。

常見方法:
long currentTimeMillis();獲取當(dāng)前時間的毫秒值。

getProperties()

//獲取系統(tǒng)的屬性信息,并存儲到了Properties集合中。 
         * properties集合中存儲都是String類型的鍵和值。
         * 最好使用它自己的存儲和取出的方法來完成元素的操作。
         */
        Properties prop = System.getProperties();
Set<String> nameSet = prop.stringPropertyNames();
        
        for(String name : nameSet){
            String value = prop.getProperty(name);
            
            System.out.println(name+"::"+value);
        }

Date類

日期對象和毫秒值之間的轉(zhuǎn)換。

#######毫秒值-->日期對象 :
1,通過Date對象的構(gòu)造方法 new Date(timeMillis);
2,還可以通過setTime設(shè)置。
因為可以通過Date對象的方法對該日期中的各個字段(年月日等)進(jìn)行操作。

####### 日期對象-->毫秒值:
1,getTime方法。
因為可以通過具體的數(shù)值進(jìn)行運算。
代碼演示:

Date date = new Date();//將當(dāng)前日期和時間封裝成Date對象。
System.out.println(date);

結(jié)果:

Fri Dec 23 19:17:32 CST 2016

Date date2 = new Date(1335664696656l);//將指定毫秒值封裝成Date對象。
System.out.println(date2);//返回從1970年1月1日開始走過指定毫秒數(shù)后的時間

結(jié)果:

Sun Apr 29 09:58:16 CST 2012

Date date = new Date();//將當(dāng)前日期和時間封裝成Date對象。
System.out.println(date.getTime());//返回毫秒值

結(jié)果:

1482492087403


對日期對象進(jìn)行格式化。
  • 將日期對象-->日期格式的字符串。
  • 使用的是DateFormat類中的format方法。
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateInstance();
System.out.println(dateFormat.format(date));//默認(rèn)風(fēng)格

結(jié)果:

2016-12-23

DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);//FULL風(fēng)格
System.out.println(dateFormat.format(date));

結(jié)果:

2016年12月23日 星期五

DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
System.out.println(dateFormat.format(date));

結(jié)果:

2016年12月23日

DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);
System.out.println(dateFormat.format(date));

結(jié)果:

2016年12月23日 下午07時31分50秒

//帶時分秒的格式
DateFormat dateFormat = DateFormat.getDateTimeInstance();//默認(rèn)格式
System.out.println(dateFormat.format(date));

結(jié)果:

2016-12-23 19:32:45

自定義風(fēng)格
DateFormat dateFormat = new SimpleDateFormat("yyyy--MM--dd::HH:mm:ss");
        
String str_date = dateFormat.format(date);
        
System.out.println(str_date);

結(jié)果:

2016--12--23::19:36:46


將日期格式的字符串-->日期對象。

使用的是DateFormat類中的parse()方法。

String str_date = "2012年4月19日";
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
Date date = dateFormat.parse(str_date);
System.out.println(date);

結(jié)果:(注意風(fēng)格要對應(yīng),不然無法識別字符串,這是解析默認(rèn)格式的)

Thu Apr 19 00:00:00 CST 2012

String str_date = "2011---8---17";
DateFormat dateFormat = new SimpleDateFormat("yyyy---MM---dd");     
Date date = dateFormat.parse(str_date);
System.out.println(date);

結(jié)果:(注意:這是解析的自定義時間格式的)

Wed Aug 17 00:00:00 CST 2011


Calendar

通過getInstance()方法來獲得日歷對象
Calendar c = Calendar.getInstance();

int get(int field):返回給定日歷字段的值

Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH)+1;
int day = c.get(Calendar.DAY_OF_MONTH);
int week = c.get(Calendar.DAY_OF_WEEK);
System.out.println(year+"年"+month+"月"+day+"日"+week);

結(jié)果:

2016年12月23日6

set方法和add方法
void set(int year, int month, int date)
add(itn field,int amount)//指定字段做時間的偏移,amount為正數(shù)是加,負(fù)數(shù)為減

public static void main(String[] args) {

        Calendar c = Calendar.getInstance();
        
        int year = 2012;
        showDays(year);
    }
public static void showDays(int year) {
        
        Calendar c = Calendar.getInstance();
        c.set(year, 2, 1);//這里是3月,月份從0開始計數(shù),設(shè)置日期為2012年3月1日
        c.add(Calendar.DAY_OF_MONTH, -1);//將當(dāng)前設(shè)置的日向前偏移1天,指3月1日的前一天
        
        showDate(c);
    }

    public static void showDate(Calendar c) {
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH)+1;//月份是從0開始到11
        int day = c.get(Calendar.DAY_OF_MONTH);
        int week = c.get(Calendar.DAY_OF_WEEK);
        
        
        System.out.println(year+"年"+month+"月"+day+"日"+week);
    }

結(jié)果:

2012年2月29日4

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 最近學(xué)習(xí)到第 23 天了,還有 4 天時間我的 JavaSE 課程就要結(jié)束了,之后會有一個考試,需要復(fù)習(xí)一下,正好...
    SawyerZh閱讀 1,121評論 0 11
  • 國家電網(wǎng)公司企業(yè)標(biāo)準(zhǔn)(Q/GDW)- 面向?qū)ο蟮挠秒娦畔?shù)據(jù)交換協(xié)議 - 報批稿:20170802 前言: 排版 ...
    庭說閱讀 12,390評論 6 13
  • js API1.全局對象NAN 非數(shù)字值的特殊值infinity 代表正無窮的數(shù)據(jù)undefined 2.函數(shù)屬性...
    Aa劉德健閱讀 1,705評論 0 2
  • 注意:左對齊單個詞對其方法向上結(jié)構(gòu) object(女媧) 每個類直接或者簡介的繼承了object 重寫調(diào)用 pu...
    beatsl閱讀 502評論 0 0
  • 今早一睜開眼,就覺得有些喘不上氣。宿舍的同學(xué)都沒有說話,但是大家都心照不宣的明白即將離開學(xué)校這件事情。氣氛有些壓抑...
    Mary默然Ma閱讀 398評論 0 0

友情鏈接更多精彩內(nèi)容