1. 程式人生 > 實用技巧 >常用方法:String、Math、Date

常用方法:String、Math、Date

----String

package com.xzm.常用方法;

public class _01_字串類String {

    public static void main(String[] args) {
        
        //字串引用型別(類)
        String str = "ABCD-abcd-1234";
        
        //方法.length()        
        System.out.println("字元個數:" + str.length());
        
        //charAt(索引)
        System.out.println("索引0的字元:" + str.charAt(0));
        
        
//替換 System.out.println( str.replace("-", "★") );//全部 System.out.println( str.replaceFirst("-", "☻") );//第一個 //判斷:str中,是否包含字串"abc": Boolean System.out.println( str.contains("abc") ); //統一轉換大小寫 System.out.println( str.toUpperCase() ); System.out.println( str.toLowerCase() );
//字串轉換成字元陣列 //每一個字都變成陣列中的一個元素 char[] arr1 = str.toCharArray(); for(int i=0; i<arr1.length; i++) { System.out.println( "arr1["+i+"]="+arr1[i]+";" ); } //按照界定符,切割成字串陣列 String[] arr2 = str.split("-"); for
(int i=0; i<arr2.length; i++) { System.out.println( "arr2["+i+"]="+arr2[i]+";" ); } //查詢,返回指定內容的第一個字元所在的索引 //沒有就是 -1 System.out.println("第一次-符號出現在索引:"+ str.indexOf("-") ); System.out.println("最後一次-符號出現在索引:"+ str.lastIndexOf("-") ); System.out.println("#符號有沒有:"+ str.indexOf("#") ); //========================================== str = "abcdefghijklmn"; //擷取:索引5開始,後面全部 System.out.println( str.substring(5) ); //擷取:索引0開始,索引5-1結束 System.out.println( str.substring(0, 5) ); // equals判斷內容是否相同:返回Boolean // String.format(),格式化字串內容。 // .***valuOf(),轉換成其他資料型別。 } }

-----Math

package com.xzm.常用方法;

public class _02_科學計算Math {

    public static void main(String[] args) {
        
        //科學計算:Math
        
        //隨機數:返回0-1之間,沒有1的,隨機浮點double
        System.out.println( Math.random() );
        
        //四捨五入:long 或 int
        System.out.println( Math.round(3.999) );
        
        
        //向上或向下取整:返回 double
        System.out.println( Math.ceil(4.00000001) );        
        System.out.println( Math.floor(5.9) );

        //次方:返回double,支援負數
        System.out.println( Math.pow(5,-8) );
        
    }

}

----Date

package com.xzm.常用方法;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class _03_日期時間 {

public static void main(String[] args) {

//基於 util常用工具包的日期類物件
Date dt = new Date();

//預設格式輸出
System.out.println(dt);

//獲取指定部分,得到的都是int

//年:從1900年開始計算的。
int year = dt.getYear();
System.out.println("今年是:" + (1900+year));

//月:0-11 getMonth()
//日:1-31 getDate()
//星期:0-6 getDay()
//時間都正常的。
//這些操作方式已經淘汰了,但是依然可以使用【正確的】

//=============================================

//使用新的物件 Calendar物件
//裡面都是可以自己設定操作。

//物件 = 物件.靜態方法();
Calendar cl = Calendar.getInstance();

//設定日期
cl.set(Calendar.YEAR, 2020);

//獲取
System.out.println(Calendar.YEAR);

//=================================================
//【瞭解:定時器,使用Calendar】


//建立時間物件
Calendar cd = Calendar.getInstance();

//設定:每天12點正
cd.set(Calendar.HOUR, 12);
cd.set(Calendar.MINUTE, 0);
cd.set(Calendar.SECOND, 0);

//獲取24小時對應的毫秒
int peroid = 1000 * 60 * 60 * 24;


//定義定時器
Timer t = new Timer();
//呼叫方法執行(處理類物件,日期,毫秒)
t.scheduleAtFixedRate(new MyTask(), cl.getTime(), peroid);

}

}


//===========================================
// 定時器的方法
class MyTask extends TimerTask{
@Override
public void run() {
System.out.println("時間到了.......");
}
}