黑馬程序員——Java基礎---常見對象2

-----------android培訓java培訓、java學習型技術博客、期待與您交流!------------ 

 

數組高級

1排序,其中心實現就是比較和換位

冒泡排序

相鄰元素兩兩比較,大的往後放,第一次完畢,最大值出現在了最大索引處

 

public static void bubble(int[] arr){

//嵌套循環,實現數組的冒泡排序

for(int x = 0 ;x < arr.length ; x++){

//內層循環,沒次在減少1,-x  ,-1防止越界異常

for(int y = 0 ; y < arr.length-x-1 ; y++){

if(arr[y] > arr[y+1]){

swap(arr,y,y+1);

}

}

}

}

選擇排序

0索引開始,依次和後面元素比較,小的往前放,第一次完畢,最小值出現在了最小索引處

 

public static void select(int[] arr){

//嵌套循環,實現數組的排序

for(int x = 0 ; x < arr.length ; x++){

//內循環,開始循環變量,比外大1

for(int y = x + 1; y < arr.length ; y++){

if(arr[x] > arr[y]){

swap(arr,x,y);

}

}

}

}

private static void swap(int[] arr,int x,int y){

int temp = arr[x];

arr[x] = arr[y];

arr[y] = temp;

}

2查找

基本查找  數組元素無序

public static int search(int[] arr,int key){

//遍歷數組,獲取每個元素,和查找的關鍵字進行比較

for(int x = 0 ; x < arr.length; x++){

if(arr[x] == key)

return x;

}

return -1;

}

二分查找  數組元素有序

 

public static int binarySearch(int[] arr, int key){

//定義三個變量,保存數組中最小,最大,中間索引

int min = 0;

int max = arr.length-1;

int mid = 0;

//循環折半,條件小索引<=大索引

while(min <= max){

//計算中間索引

mid = (min+max)/2;

//關鍵字和中間索引,進行比較

if(key > arr[mid]){

min = mid + 1;

}else if( key < arr[mid]){

max = mid -1;

}else{

//找到了,返回索引

return mid;

}

}

return -1;

}

測試用例:

把字符串中的字符進行排序。

public class ArrayTest {

public static void main(String[] args) {

String s = sortString("ehdtphj");

System.out.println(s);

}

 //定義方法,實現字符串排序,傳遞參數,有返回值

 

public static String sortString(String s){

//轉成字符數組

char[] ch = s.toCharArray();

//數組排序,選擇,冒泡,任意選擇一個

for(int x = 0 ; x < ch.length ; x++){

for(int y = x + 1; y < ch.length ;y++){

if(ch[x] > ch[y]){

char temp = ch[x];

ch[x] = ch[y];

ch[y] = temp;

}

}

}

//將數組變成字符串返回

return new String(ch);

}

}

Arrays

位於java.util包中的,一個專門用於操作數組的工具類,爲我們提供了排序查找等功能。通過查詢API文檔,我們發現Arrays構造方法是私有修飾的,並且Arrays的方法都是靜態的,也就是說我們可以直接通過類名調用。

 

一些常用的Arrays方法:

  sort(數組)   按照升序的方式對數組進行排序 

 

  int binarySearch(數組,查找關鍵字數組折半查找

    返回關鍵字出現的索引值,如果遍歷完數組扔沒有找到關鍵字就返回一個負數,該負數的計算方法是:插入點-1

    

  String toString(數組不是重寫Object類的toString()

    把數組變成字符串

    

  asList() 將數組轉成集合

 

演示用例:

/*

 * 將數組變成字符串

 *   static String toString(數組)

 */

public static void method_2(){

int[] arr = {4,1,6,3,8};

String s = Arrays.toString(arr);

System.out.println(s);

}

/*

 * 數組的折半查找方法 Arrays.binarySearch

 * 傳遞數組,關鍵字,返回索引

 * 數組有序的

 * binarySearch 元素沒有,返回值的計算方法

 * 返回 -插入點-1

 * 插入點,關鍵字放到數組中,保證有序,這個索引就是插入點

 */

public static void method_1(){

int[] arr = {1,4,7,9,11,15,17,19};

int index = Arrays.binarySearch(arr,6);

System.out.println(index);

}

/*

 * 數組的升序排列方法Arrays.sort

 */

public static void method(){

int[] arr = {7,5,-1,6,2,4,0,9,11};

Arrays.sort(arr);

for(int x = 0 ; x < arr.length ; x++){

System.out.println(arr[x]);

}

}

基本類型包裝

將基本數據類型封裝成對象,好處在於可以在對象中定義更多的功能方法來操作該數據。

常用的操作之一就是用於基本數據類型與字符串之間的轉換。

 

   基本數據類型,也被看成是對象,因此,也就出現了對象的描述類,包裝類。

基本數據類型 包裝類

     byte      Byte

     short       Short

     int       Integer

     long      Long

     float      Float

     double    Double

     char      Character

     boolean   Boolean

Integer

Integer 類在對象中包裝了一個基本類型 int 的值。

該類提供了多個方法,能在 int 類型和 String 類型之間互相轉換,還提供了處理 int 類型時非常有用的其他一些常量和方法。

靜態常量

MAX_VALUE 對於類型所能表示的最大值

MIN_VALUE 對應類型所能表示的最小值

Integer類的構造方法

    Integer(int )

    Integer(String s)

    

  創建Integer類的對象,包裝數據。可以是基本類型int,也可以是數字格式字符串。

演示用例:

//創建Integer類的對象,傳遞基本類型

Integer i = new Integer(100);

//創建Integer類的對象,傳遞字符串,數字格式

Integer i2 = new Integer("123");

常用方法:

 1、 十進制轉成二進制,八進制,十六進制 toBinaryStringtoOctalStringtoHexString

 

public static void method_5(){

System.out.println(Integer.toBinaryString(31));

System.out.println(Integer.toOctalString(31));

System.out.println(Integer.toHexString(31));

}

2、將基本類型轉成Integer類的對象

  Integer類靜態方法static Integer valueOf(int)

  Integer類靜態方法static Integer valueOf(String s)

  new Integer(100)

  new Integer("100")

 

public static void method_4(){

Integer i = Integer.valueOf(100);

Integer i2 = Integer.valueOf("100");

}

3、將基本類型變成字符串

  Integer類方法static toString(int i)

 

  static String toString(int i,int radix)

  toString(110, 2);

  第一個參數是十進制的,後面的十進制表示

  將前面的十進制,轉成後面要求的進制數

 

public static void method_3(){

String s = Integer.toString(2);//2+""

System.out.println(s);

String s1 = Integer.toString(15, 2);

System.out.println(s1);

}

4、需要對象調用,字符串轉成基本類型int

 

  Integer類的方法 intValue()非靜態的

  轉的是Integer構造方法中封裝的字符串

 

public static void method_2(){

Integer i = new Integer("123");

int x = i.intValue();

System.out.println(x+1);

}

5、將數字格式字符串,轉成對應進制的基本類型int 

  Integer類靜態方法int parseInt(String s,int radix)

  

  第二個參數,基數--進制

  第二個參數,表明了前面字符串表示的數字的進制

  計算結果十進制

 0-9A-Z

 

public static void method_1(){

int x = Integer.parseInt("110",2);//進制數最大可到36

System.out.println(x);

}

6、將數字格式字符串,轉成基本類型int

 

  Integer類靜態方法int parseInt(String s)

public static void method(){

int x = Integer.parseInt("123");

System.out.println(x+1);

}

自動裝箱和拆箱

JDK1.5版本新特性,

自動裝箱,基本類型自動封裝對象

自動拆箱,將對象變成基本類型

//類類型 變量 基本類型 

Integer i = 1;//自動裝箱,相當於Integer i = new Integer(1)

i = i + 1;// 拆箱 和基本類型計算 i.intValue()+1

          // 計算後的結果,在變成對象 賦值給i

          //相當於i = new Integer( i.intValue()+1 )

System.out.println(i);

Integer j = null;

if(j!=null){

  j=j+1;

  System.out.println(j);//因爲j是一個對象,運算可能會出現會出現NullPointerException

 

}

}

注意:

Integer a = 500;// new Integer(500)

Integer b = 500;// new Integer(500)

System.out.println(a==b);//false,引用類型,比較的是對象的實際地址值

System.out.println(a.equals(b));//true , Integer類重寫equals,比較的對象中具體數值

Integer i = new Integer(100);

Integer j = new Integer(100);

System.out.println(i==j);//false

System.out.println(i.equals(j));//true

Integer aa = -128;

Integer bb = -128;

System.out.println(aa==bb);//true

System.out.println(aa.equals(bb));//true

//特別注意,當取值在byte以內時,不會從新開闢空間,其操作相當於aa  = new Integer(-128)

//bb= aa;

 

Character

Character 類在對象中包裝一個基本類型 char 的值。

此外,該類提供了幾種方法,以確定字符的類別(小寫字母,數字,等等),並將字符從大寫轉換成小寫,反之亦然。

 

 

主要方法:

 判斷:  返回都是布爾,靜態方法

    

      char toUpperCase(char ch)轉成大寫

      char toLowerCase(char ch)轉成小寫

//判斷字符是不是大寫

boolean b = Character.isUpperCase('A');

System.out.println(b);

//判斷字符是不是小寫

b = Character.isLowerCase('a');

System.out.println(b);

//判斷字符是不是數字

b = Character.isDigit('0');

System.out.println(b);

 

轉換:靜態方法

//字符轉成大寫

char c = Character.toUpperCase('f');

System.out.println(c);

//字符轉成小寫

c = Character.toLowerCase('H');

System.out.println(c);

}

演示用例:

統計一個字符串中大寫字母字符,小寫字母字符,數字字符出現的次數

String s = "geHDveSew43Gd32";

//定義三個變量,計數器

int upper=0,lower=0,digit=0;

//轉成字符數組

char[] ch = s.toCharArray();

for(int x = 0 ; x < ch.length ;x++){

if(Character.isUpperCase(ch[x]))

upper++;

else if(Character.isLowerCase(ch[x]))

lower++;

else if(Character.isDigit(ch[x]))

digit++;

}

System.out.println("大寫字母有:"+upper+" ");

System.out.println("小寫字母有:"+lower+" ");

System.out.println("數字有:"+digit+" ");

}

Math

Math 類包含用於執行基本數學運算的方法,如初等指數、對數、平方根和三角函數。

常用的成員方法:

/*  1double pow(int a,int b)

  計算的是ab次冪*/

 

public static void method_5(){

double d = Math.pow(2,3);

System.out.println(d);

//System.out.println(Math.sqrt(-2));

}

/*2double round(double d)

  對參數進行四捨五入,取整*/

 

public static void method_4(){

double d = Math.round(5.58); 

System.out.println(d);

}

/* 3 double floor(double d)

  返回小於或者等於參數的最大整數 */

public static void method_3(){

double d = Math.floor(-11.56);

System.out.println(d);

}

/*  4double ceil(double a)

  返回大於或者等於參數的最小整數*/

 

public static void method_2(){

double d = Math.ceil(11.56);

System.out.println(d);

}

/* 5max傳遞兩個參數,返回較大的一個*/

 

public static void method_1(){

int x = Math.max(15, 19);

System.out.println(x);

}

/* 6 abs計算絕對值*/

 

public static void method(){

int x = Math.abs(3);

System.out.println(x);

}

 

Random

此類用於產生隨機數

如果用相同的種子創建兩個 Random 實例,則對每個實例進行相同的方法調用序列,它們將生成並返回相同的數字序列。

構造方法

public Random()

public Random(long seed)

成員方法

public int nextInt()

返回下一個僞隨機數,它是此隨機數生成器的序列中均勻分佈的 int 值。

public int nextInt(int n)

返回一個僞隨機數,它是取自此隨機數生成器序列的、在 0(包括)和指定值(不包括)之間均勻分佈的 int 值。

Random r = new Random();

for(int x = 0 ; x < 20 ; x++){

   int number = r.nextInt(100);

   System.out.println(number);

}

System

System 類包含一些有用的類字段和方法。它不能被實例化,構造方法私有。

常用方法:

  /*1static void arrayCopy 複製數組

 * (Object srcint srcPos, Object destint destPos, int length)

 * Object src 複製數組數據源

 * int srcPos 複製數組數據源開始下標

 * 

 * Object dest 複製目的數組

 * int destPos 複製目的數組開始索引

 * int length 複製幾個*/

 

public static void method_3(){

//定義數組數據源

int[] src = {11,22,33,44,55};

//定義數據目的

int[] dest = new int[src.length+2];

//複製數組

System.arraycopy(src, 0, dest, 0, src.length);

//目的:  66,77,22,33,0

//輸出目的

System.out.println(Arrays.toString(dest));

}

/* 2 static Properties getProperties

 * 返回當前操作系統的屬性*/

 

public static void method_2(){

System.out.println(System.getProperties());

}

/* 3static long currentTimeMillis()

 * 返回值是一個long參數,int不夠了

 * 返回的是自197011日午夜零時,到升序運行的那一時刻.所經過的毫秒值

 * 1000毫秒=1

 * 197011日午夜零時 JAVA語言的時間零點,毫秒值0*/

 

public static void method_1(){

long time = System.currentTimeMillis();

System.out.println(time);//13796847878412

  //                       14379684952212

}

 /*4static exit(int i)

 * 退出虛擬機,所有的程序全部停止*/

 

public static void method(){

while(true){

System.exit(0);

System.out.println("死循環");

}

}

BigInteger

概述

可以讓超過Integer範圍內的數據進行運算

構造方法

public BigInteger(String val)

常見方法

 /*1 BigInteger類的除法計算

 * divide

 * 兩個BigInteger類的對象進行除法 */

public static void method_3(){

//創建2BigInteger類的對象,封裝字符串,必須數字格式

BigInteger b1 = new BigInteger("34567865432134567865432456754314253635431");

BigInteger b2 = new BigInteger("876543234568754312457654");

//b1對象調用方法multiply,傳遞b2  b1/b2

BigInteger b3 = b1.divide(b2);

System.out.println(b3);

}

/* 2BigInteger類的乘法計算

 * multiply

 * 兩個BigInteger類的對象進行乘法 */

public static void method_2(){

//創建2BigInteger類的對象,封裝字符串,必須數字格式

BigInteger b1 = new BigInteger("34567865432134567865432456754314253635431");

BigInteger b2 = new BigInteger("87654323456875431245765438765439876543544");

//b1對象調用方法multiply,傳遞b2  b1*b2

BigInteger b3 = b1.multiply(b2);

System.out.println(b3);

}

/* 3 BigIntger類的減法計算

 * subtract

 * 兩個BigInteger類的對象進行相減 */

public static void method_1(){

//創建2BigInteger類的對象,封裝字符串,必須數字格式

BigInteger b1 = new BigInteger("34567865432134567865432456754314253635431");

BigInteger b2 = new BigInteger("87654323456875431245765438765439876543544");

//b1對象調用方法subtract,傳遞b2  b1-b2

BigInteger b3 = b1.subtract(b2);

System.out.println(b3);

}

 /*4 BigInteger類的加法計算

 * add方法來實現

 * 兩個BigInteger類的對象進行相加*/

 

public static void method(){

//創建2BigInteger類的對象,封裝字符串,必須數字格式

BigInteger b1 = new BigInteger("34567865432134567865432456754314253635431");

BigInteger b2 = new BigInteger("87654323456875431245765438765439876543544");

//b1對象,調用方法add,傳遞b2

BigInteger b3 = b1.add(b2);

System.out.println(b3);

}

BigDecimal

由於在運算的時候,float類型和double很容易丟失精度。所以,爲了能精確的表示、計算浮點數,Java提供了BigDecimal類。

BigDecimal不可變的、任意精度的有符號十進制數。

 

System.out.println(0.09 + 0.01);//0.09999999999999999

 

System.out.println(1.0 - 0.32);//0.6799999999999999

 

System.out.println(1.015 * 100);//101.49999999999999

 

System.out.println(1.301 / 100);//0.013009999999999999

 

/*

 * BigDeimal的除法運算

 * divide

 * 兩個BigDeimal對象除法

 * Non-terminating decimal expansion; no exact representable decimal result

 * 除法運算,出現了無限不循環的小數結果

 * divide(BigDecimal divisor, int scale, int roundingMode) 

 * 

 * int scale 保留位數

 * int roundingMode 舍入模式

 *   1.412345

                 舍入模式 BigDeimal類的靜態成員變量

          ROUND_UP 0.02 向上+1

          ROUND_DOWN 0.01 直接捨去

          ROUND_HALF_UP  四捨五入

          ROUND_HALF_DOWN 捨去部分>0.5向上

 

 */

public static void method_3(){

//創建2BigDeimal類的對象,封裝小數,寫字符串

BigDecimal b1 = new BigDecimal("1.500");

BigDecimal b2 = new BigDecimal("100");

//b1對象,調用divide,傳遞b2  b1/b2

//保留2位小數  0.01501 0.01500

BigDecimal b3 = b1.divide(b2,2,BigDecimal.ROUND_HALF_DOWN);

System.out.println(b3);

}

/*

 * BigDeimal的乘法運算

 * multiply

 * 兩個BigDeimal對象乘法

 */

public static void method_2(){

//創建2BigDeimal類的對象,封裝小數,寫字符串

BigDecimal b1 = new BigDecimal("1.015");

BigDecimal b2 = new BigDecimal("100");

//b1對象,調用multiply,傳遞b2  b1*b2

BigDecimal b3 = b1.multiply(b2);

System.out.println(b3);

}

/*

 * BigDecimnal的減法運算

 * subtract

 * 兩個BigDeimal對象減法

 */

public static void method_1(){

//創建2BigDeimal類的對象,封裝小數,寫字符串

BigDecimal b1 = new BigDecimal("1.0");

BigDecimal b2 = new BigDecimal("0.32");

//b1對象,調用subtract,傳遞b2  b1-b2

BigDecimal b3 = b1.subtract(b2);

System.out.println(b3);

}

/*

 * BigDecimal對象加法計算

 * add

 * 兩個BigDeimal對象加法

 */

public static void method(){

//創建2BigDeimal類的對象,封裝小數,寫字符串

BigDecimal b1 = new BigDecimal("456.76543245");

BigDecimal b2 = new BigDecimal("543.6788");

//b1對象,調用add,傳遞b2  b1+b2

BigDecimal b3 = b1.add(b2);

System.out.println(b3);

}

Date

類 Date 表示特定的瞬間,精確到毫秒。位於java.util包中

 

構造方法

    Date()空參數

      返回當前操作系統上的時間和日期

      

    Date(long )傳遞long參數

      在時間和日期中,出現的參數類型是long,表示的毫秒值

  將日期設置到毫秒值指定的日期上

 

成員方法

public long getTime()

返回當期Date指示的日期對象毫秒值

public void setTime(long time)

指定毫秒值,轉成日期對象

DateFormat

DateFormat 是日期/時間格式化子類的抽象類,它以與語言無關的方式格式化並解析日期或時間。

是抽象類,所以使用其子類SimpleDateFormat

常用方法:

static DataFormat  getDateInstance()

獲取日期格式器,該格式器具有默認語言環境的默認格式化風格。

  static DateFomrat  getDateTimeInstance()

獲取日期/時間格式器,該格式器具有默認語言環境的默認格式化風格。

 

返回值是類類型的時候,返回的必然子類對象

 DataFormat d = DateFormat.getDateInstance();

 getDateInstance(){return new SimpleDateFormat()}

 

public Date parse(String source)

傳遞字符串,變成日期對象

//將字符串變成日期對象

String s = "2015-1-1";

//獲取DateFormat類的子類對象

DateFormat df = DateFormat.getDateInstance();

//調用方法parse

Date d  = df.parse(s);

System.out.println(d);

SimpleDateFormat

SimpleDateFormat是DateFormat 的子類,它可以使用戶選擇和定義任意的日期-時間

格式。位於java.text包中。

 

格式化日期的實現步驟:

   1. 創建SimpleDateFormat類的對象,傳遞日期的格式(String )

       格式寫法,如下:

        yyyy-MM-dd  HH:mm:ss

     2. 調用SimpleDateFormat類的父類方法 format

       format(Date d)傳遞日期對象,對日期對象進行格式化

       格式化就按照我們在SimpleDateFormat類構造方法中寫的格式進行

       format 返回字符串

模式字母表如下:

字母

日期或時間元素

表示

示例

G

Era 標誌符

Text

AD

y

Year 

1996; 96

M

年中的月份 

Month 

July; Jul; 07

w

年中的週數

Number 

27 

W

月份中的週數

Number 

D

年中的天數

Number

189

d

月份中的天數

Number

10

F

月份中的星期

Number

2

E

星期中的天數

Text

Tuesday ;Tue

a

Am/pm標記

Text

PM

H

一天中的小時數(0-23

Number

0

K

Am/pm中的小時數(0-11

Number

24

k

一天中的小時數(1-24

Number

0

h

Am/pm中的小時數(1-12

Number

12

m

小時中的分鐘數

Number

30

s

分鐘中的秒數

Number

55

S

毫秒數

Number

978

z

時區

General time zone

Pacific Standard Time;PST;GMT-08:00

Z

時區

RFC 822 time zone

-0800

      //1 . 創建SimpleDateFormat類對象,傳遞自己模式

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd日  HHmmss");

  //2. sdf子類對象,調用父類方法format進行格式化

String date = sdf.format(new Date());

System.out.println(date);

 

測試用例:

計算來到世界的天數

//鍵盤輸入接收生日

String birthday = new Scanner(System.in).nextLine();

//將生日字符串轉成日期對象

//DateFormat類的子類對象

DateFormat df = DateFormat.getDateInstance();

//方法parse轉成日期對象

Date birthdayDate = df.parse(birthday);

//獲取當期日期

Date todayDate = new Date();

//將兩個日期轉成毫秒值

long birthdayTime = birthdayDate.getTime();

long todayTime = todayDate.getTime();

if(todayTime < birthdayTime){

System.out.println("您還沒出生呢");

}else{

//相減毫秒值,兩個日期相差毫秒值

//毫秒值轉成天數

System.out.println( (todayTime-birthdayTime)/1000/60/60/24);

}

}

Calendar

Calendar 類是一個抽象類,它爲特定瞬間與一組諸如 YEARMONTHDAY_OF_MONTHHOUR 等 日曆字段之間的轉換提供了一些方法,併爲操作日曆字段(例如獲得下星期的日期)提供了一些方法。

  static getInstance()子類對象,子類對象調用Calendar類中的方法

  Calendar c = Calendar.getInstance()

  sop(c)

  日曆字段就是表示一個日曆的每個組成部分

    年,,,星期,小時,分鐘,,毫秒

 Calendar 靜態成員變量,表示每個日曆字段

 Calendar方法get,非靜態方法

   int get(int )傳遞日曆字段,返回這個日曆字段對應的數字

Calendar c = Calendar.getInstance();//系統日曆

//日曆類的方法get,傳遞日曆字段

/*int x = c.get(Calendar.MONTH);

System.out.println(x);*/

     printCalendar(c);

     

     /*int x = c.getActualMinimum(Calendar.DAY_OF_MONTH);

     System.out.println(x);*/

}

public static void printCalendar(Calendar c){

//輸出日曆 20150729日 星期三  XXXXXX秒  字符串的拼接作用

System.out.println( c.get(Calendar.YEAR)+""+ getDay((c.get(Calendar.MONTH)+1))+"" +

getDay(c.get(Calendar.DAY_OF_MONTH))+"日 "+getWeek((c.get(Calendar.DAY_OF_WEEK)-1))+" "+

c.get(Calendar.HOUR_OF_DAY)+"點 "+c.get(Calendar.MINUTE)+"分 "+c.get(Calendar.SECOND)+"");

}

/*

 * 定義方法,將月份和天數,不足2位數

 * 補齊2位數  07  01 -- String

 */

private static String getDay(int day){

if(day<10)

return "0"+day;

return day+"";

}

/*

 * 定義方法,數組查表法,還原星期

 */

 private static String getWeek(int week){

 String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};

 return weeks[week];

 }

日曆類的設置方法

 1 Calendar類的方法set

   void set(int field, int value)

   設置日曆的字段,被設置具體數  Calendar.YEAR, 2016 

   

/*

 * set方法設置日曆

 *  指定設置的字段,指定具體數

 */

public static void method(){

Calendar c = Calendar.getInstance();

//日曆設置到,月份,11

c.set(Calendar.MONTH,11);

CalendarDemo.printCalendar(c);

}

 2 void set(int year, int month, int date) 

   傳遞參數,int類型的,年月日

 

/*

 * set方法設置日曆

 *  同時指定年月日

 */

public static void method_1(){

Calendar c = Calendar.getInstance();

//設置到2008年的88

c.set(2008, 7, 8);

CalendarDemo.printCalendar(c);

}

   

  3 void setTime(Date date) 

           傳遞Date對象

 

/*

 * setTime方法設置日曆

 * 傳遞Date對象(系統日期)

 */

public static void method_2(){

Calendar c = Calendar.getInstance();

//設置日曆,Date對象是一樣的

c.setTime(new Date());

CalendarDemo.printCalendar(c);

}

 

  4 void setTimeInMillis(long millis

           設置日曆,傳遞毫秒值

/*

 * setTimeInMillis(毫秒值)

 * 設置日曆到指定毫秒值上

 */

public static void method_3(){

Calendar c = Calendar.getInstance();

c.setTimeInMillis(0);

CalendarDemo.printCalendar(c);

}

 

   Calendar類的抽象方法 add

   運行子類的重寫

   add方法作用,日曆的偏移量

   add(int field, int amount) 

     要偏移的日曆字段,偏移具體數據,正數向後偏移,負數向前偏移

Calendar c = Calendar.getInstance();

//日曆進行偏移,年份,向後偏移5

c.add(Calendar.YEAR, -5);

CalendarDemo.printCalendar(c);

測試用例:

  計算保質期

       生產日期,保質期,計算哪天到期

//根據生產日期,保質期,計算哪一天到期

public static void test()throws Exception{

String dateStr = "2015-6-28";

//轉成Date對象

DateFormat df = DateFormat.getDateInstance();

Date date =df.parse(dateStr);

//將日曆對象,設置到Date

Calendar c = Calendar.getInstance();

c.setTime(date);

CalendarDemo.printCalendar(c);

//保質期180

//日曆偏移180

c.add(Calendar.DAY_OF_MONTH, 280);

CalendarDemo.printCalendar(c);

}

   計算出閏年

      利用29,2月份

       2000 1990 日曆這個年,設置到31

       向前偏移1,獲取天數

//計算閏年,31,偏移1

public static void test1(String year){

Calendar c = Calendar.getInstance();

int y = Integer.parseInt(year);

//日曆設置到這一年的31

c.set(y, 2, 1);

CalendarDemo.printCalendar(c);

c.add(Calendar.DAY_OF_MONTH, -1);

CalendarDemo.printCalendar(c);

int day = c.get(Calendar.DAY_OF_MONTH);

if(day==29)

System.out.println("閏年");

else

System.out.println("平年");

}

 

 

-----------android培訓java培訓、java學習型技術博客、期待與您交流!------------

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