實用類

 日期類:
 現在講的是 java.util.Date類  
 date()
 date(long mills)  

 public class DateDemo{ 
 public static void main(String[] args) { 

 Date date = new Date();  
 //默認調用toString方法
    System.out.println(date.toString());  

    //獲取當前的系統的時間 毫秒數
    long time = System.currentTimeMillis();
    System.out.println("time : " + time); 

    //如何將一個Date對象變成一個其他格式的字符串  1990-02-03 12:20:30
    //SimpleDateFormat對象   Format:格式化
    //參數pattern : 日期格式
    //1.創建SimpleDateFormat對象 來定義日期格式 
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String s1 = dateFormat.format(date);
    System.out.println(s1+"\n");  
 }
 }  


1.生成隨機數
RandomDemo 
Random random = new Random();
//nextInt(int number)隨機生成一個0-number的值(不包含number)
for (int i = 0; i < 1000; i++) {
    System.out.println(random.nextInt(100)); 
}

2.生成隨機數
Math類

class MathDemo {   
public static void main(String[] args) {  
//隨機得到一個0-1的小數(不包含1)
double random = Math.random(); 
//其他的方法
     * abs()            求絕對值
     * max(int a, int b) 求最大值
     * min(int a, int b) 求最小值
     * ceil()           
     * floor()
     * round(double a)   

     * ceil()           
     * floor()
     * round(double a)
       double a = 3.14;
    //ceil 天花板  
    System.out.println(Math.ceil(a));
    //floor 地板 
    System.out.println(Math.floor(a));
    //round 周圍附近 四捨五入
    System.out.println(Math.round(a));
}
}


object類 
方法1:equals();             比較內容 通過同一種方法運算出來的是一樣的
方法2:hashCode();
方法3:toString();
方法4:getClass();

 方法1:equals();    
    ObjectDemo demo = new ObjectDemo(); 
        demo.age = 10;
        ObjectDemo demo1 = new ObjectDemo();
        demo1.age = 10;
        System.out.println("相等 ? : " + demo.equals(demo1));   //true   

方法2:    hashCode()
 就是使用了 哈希算法(也稱散列算法) 


方法3:toString();   
 打印默認調用方法@左邊爲完整類名也是字節碼文件名字@右邊是哈希可碼值   

方法4: getClass() 
因爲一切皆是對象,類型也不例外,在Java使用類型類來表示一個類型。所有的類型類都是Class類的實例  

    Class clazz = demo.getClass();

    String name = clazz.getName();
    System.out.println(name);  


 * currentTimeMillis() //獲取當前系統的時間 單位 : 毫秒值
 * 
 * exit(int status)        //退出虛擬機
 * 
 * gc();                       //回收java中的垃圾    

String類
獲取
getBytes(); 獲取一個字節數組
將一個字符串轉化爲一個字節數組byte[]的方法
getBytes(String charsetName)
length() 獲取字符串長度
System.out.println(“length : ” + a.length());
charAt(int index); 獲取該字符串對應下標的字符
indexOf(int ch) 獲取該字符在該字符串中第一次出現的下標
lastIndexOf(int ch) 獲取該字符在該字符串中最後一次出現的下標
截取
subString(int beginIndex) 截取從beginIndex到該字符串末尾的所有字符
subString(int beginIndex, int endIndex) 截取從beginIndex到endIndex(不包含endIndex)的所有 字符
包頭不包尾
split(String regex); 以 regex這個字符串來切割 原字符串,獲得一個新的字符串數組
trim();//去除字符串2端的空格(字符串中的空格不去除)

轉換
toUpperCase() 將原字符串中的字母全部大寫
toLowerCase() 將原字符串中的字母全部小寫

判斷
equals() 判斷字符串中的內容是否相等
equalsIgnoreCase(String anotherString) 判斷字符串中的內容是否相等(忽略字符的大小寫)
startsWith() 判斷原字符串是否以傳入的字符串開頭
endsWith(String suffix) 判斷原字符串是否以傳入的字符串爲結尾
isEmpty() 判斷字符串的長度是否爲0
contains(String anotherString) 判斷原字符串是否包含傳入的字符串
compareTo(String anotherString) 比較字符串的大小(按照字典順序abcdefg..)

連接
concat() 跟 + 一模一樣的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章