1. 程式人生 > >java學習之路 之 Java常用類-Data類、Math類、BigInteger類、BigDecimai類 及 練習題

java學習之路 之 Java常用類-Data類、Math類、BigInteger類、BigDecimai類 及 練習題

日期類:

java.lang.System類

System類提供的public static long currentTimeMillis()用來返回當前時間與1970年1月1日0時0分0秒之間以毫秒為單位的時間差。
此方法適於計算時間差。
計算世界時間的主要標準有:
UTC(Universal Time Coordinated)
GMT(Greenwich Mean Time)
CST(Central Standard Time)

 java.util.Date類    

表示特定的瞬間,精確到毫秒
構造方法:
Date( )使用Date類的無引數構造方法建立的物件可以獲取本地當前時間。
Date(long date)
常用方法
getTime():返回自 1970 年 1 月 1 日 00:00:00 GMT 以來此 Date 物件表示的毫秒數。
toString():把此 Date 物件轉換為以下形式的 String: dow mon dd hh:mm:ss zzz yyyy 其中: dow 是一週中的某一天 (Sun, Mon, Tue, Wed, Thu, Fri, Sat),zzz是時間標準。
Date類的API不易於國際化,大部分被廢棄了,

java.text.SimpleDateFormat類

是一個不與語言環境有關的方式來格式化和解析日期的具體類。
它允許進行格式化(日期 文字)、解析(文字 日期)
格式化:
SimpleDateFormat() :預設的模式和語言環境建立物件
public SimpleDateFormat(String pattern):該構造方法可以用引數pattern指定的格式建立一個物件,該物件呼叫:
public String format(Date date):方法格式化時間物件date
解析:
public Date parse(String source):從給定字串的開始解析文字,以生成一個日期。

舉例:

Date date = new Date();  //產生一個Date例項
//產生一個formater格式化的例項
SimpleDateFormat formater = new SimpleDateFormat();	
System.out.println(formater.format(date));//列印輸出預設的格式	
SimpleDateFormat formater2 = new SimpleDateFormat("yyyy年MM月dd日 EEE HH:mm:ss");	
System.out.println(formater2.format(date)); 	//例項化一個指定的格式物件	
//按指定的格式輸出	
try {		
	Date date2 = formater2.parse(“2008年08月08日 星期一08:08:08");		 //將指定的日期解析後格式化按指定的格式輸出		
	System.out.println(date2.toString());	
} catch (ParseException e) {		
	e.printStackTrace();	
}

 java.util.Calendar(日曆)類

Calendar是一個抽象基類,主用用於完成日期欄位之間相互操作的功能。
獲取Calendar例項的方法
使用Calendar.getInstance()方法
呼叫它的子類GregorianCalendar的構造器。
一個Calendar的例項是系統時間的抽象表示,通過get(int field)方法來取得想要的時間資訊。比如YEAR、MONTH、DAY_OF_WEEK、HOUR_OF_DAY 、MINUTE、SECOND
public void set(int field,int value)
public void add(int field,int amount)
public final Date getTime()
public final void setTime(Date date)

Math類

java.lang.Math提供了一系列靜態方法用於科學計算
其方法的引數和返回值型別一般為double型。
abs     絕對值
acos,asin,atan,cos,sin,tan  三角函式
sqrt     平方根
pow(double a,doble b)     a的b次冪
log    自然對數
exp    e為底指數
max(double a,double b)
min(double a,double b)
random()      返回0.0到1.0的隨機數
long round(double a)     double型資料a轉換為long型(四捨五入)
toDegrees(double angrad)     弧度—>角度
toRadians(double angdeg)     角度—>弧度

BigInteger類

Integer類作為int的包裝類,能儲存的最大整型值為2的31次方減1,即20多億。
Bigintteger類的數字範圍較Integer類的數字範圍要大的多。可以支援任意精度的整數
構造:
Biginteger(String val)
常用方法:
public Biginteger abs()//返回其值是此 BigInteger 的絕對值.
public Biginteger add() //返回其值為 (this + val) 的 BigInteger。
public BigInteger subtract(BigInteger val) //返回其值為 (this - val) 的 BigInteger。 
public BigInteger multiply(BigInteger val) //返回其值為 (this * val) 的 BigInteger。 
public BigInteger divide(BigInteger val) //返回其值為 (this / val) 的 BigInteger。 
public BigInteger remainder(BigInteger val) //返回其值為 (this % val) 的 BigInteger。 
public BigInteger pow(int exponent) //返回其值為 (thisexponent) 的 BigInteger。 
public BigInteger[] divideAndRemainder(BigInteger val)//返回包含 (this / val) 後跟 (this % val) 的兩個 BigInteger 的陣列。

BigDecimal類:

一般的Float類和Double類可以用來做科學計算或工程計算,但在商業計算中,要求數字精度比較高,故用到java.math.BigDecimal類。BigDecimal類支援任何精度的定點數。
構造器
public BigDecimal(double val)
public BigDecimal(String val)
常用方法
public BigDecimal add(BigDecimal augend)//返回一個 BigDecimal,其值為 (this + augend),其標度為 max(this.scale(), augend.scale())。
public BigDecimal subtract(BigDecimal subtrahend)//返回一個 BigDecimal,其值為 (this - subtrahend),其標度為 max(this.scale(), subtrahend.scale())。
public BigDecimal multiply(BigDecimal multiplicand)//返回一個 BigDecimal,其值為 (this × multiplicand),其標度為 (this.scale() + multiplicand.scale())。
public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)  //返回一個 BigDecimal,其值為 (this / divisor),其標度為指定標度。

public void TestBigInteger(){
	BigInteger bi = new BigInteger("12433241123");
	BigDecimal bd = new BigDecimal("12435.351");
	BigDecimal bd2 = new BigDecimal("11");
	System.out.println(bi);
	//System.out.println(bd.divide(bd2));
	System.out.println(bd.divide(bd2,BigDecimal.ROUND_HALF_UP));
	System.out.println(bd.divide(bd2,15,BigDecimal.ROUND_HALF_UP));
}

練習題:

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.junit.Test;

public class CommonTest {
	
	@Test
	public void test1() {
		Calendar calendar = Calendar.getInstance();
		// 1979-8-10
		calendar.set(Calendar.YEAR, 1979);
		calendar.set(Calendar.MONTH, 7);
		calendar.set(Calendar.DAY_OF_MONTH, 10);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println(sdf.format(calendar.getTime()));
		
		calendar.add(Calendar.DAY_OF_MONTH, -100); // 獲取當前物件的日期的100天前的日期
		
		System.out.println(sdf.format(calendar.getTime()));
	}
	
	@Test
	public void test2() {
		System.out.println((int)(Math.random() * 80 + 10));
		System.out.println(Math.round(3.5)); // 4
		System.out.println(Math.round(3.4)); // 3
		System.out.println(Math.round(-3.4)); // -3
		System.out.println(Math.round(-3.5)); // 也是-3
		System.out.println(Math.round(-3.6)); // -4
	}
	
	@Test
	public void test3() {
		BigInteger bi = new BigInteger("2342342342392992929922929292920340489498594954954959459202394234");
		BigInteger rt = bi.add(new BigInteger("2342342342"));
		rt = rt.multiply(new BigInteger("100"));
		System.out.println(rt);
		BigDecimal bd = new BigDecimal("23423423429883284283482934234.2384293482394293482983423984");
		bd = bd.pow(20);
		System.out.println(bd);
		// 求一個數的階乘
		System.out.println(jie(10000));
	}
	
	public String jie(int value) {
		BigInteger result = new BigInteger("1");
		for (int i = value; i > 0; i--) {
			result = result.multiply(new BigInteger(String.valueOf(i)));
		}
		return result.toString();
	}
}