day14-----------常見對象(傳智視頻)

A:字符
	x 字符 x。舉例:'a'表示字符a
	\\ 反斜線字符。
	\n 新行(換行)符 ('\u000A') 
	\r 回車符 ('\u000D')
	
B:字符類
	[abc] a、b 或 c(簡單類) 
	[^abc] 任何字符,除了 a、b 或 c(否認) 
	[a-zA-Z] a到 z 或 A到 Z,兩頭的字母包括在內(範圍) 
	[0-9] 0到9的字符都包括
	
C:預約義字符類
	. 任何字符。個人就是.字符自己,怎麼表示呢? \.
	\d 數字:[0-9]
	\w 單詞字符:[a-zA-Z_0-9]
		在正則表達式裏面組成單詞的東西必須有這些東西組成

D:邊界匹配器
	^ 行的開頭 
	$ 行的結尾 
	\b 單詞邊界
		就是否是單詞字符的地方。
		舉例:hello world?haha;xixi
	
E:Greedy 數量詞 
	X? X,一次或一次也沒有
	X* X,零次或屢次
	X+ X,一次或屢次
	X{n} X,剛好 n 次 
	X{n,} X,至少 n 次 
	X{n,m} X,至少 n 次,可是不超過 m 次
 正則表達式的應用
   判斷功能  •public boolean matches(String regex)  
   分割功能  •public String[] split(String regex)  
   替換功能  •public String replaceAll(String regex,String replacement)  
   獲取功能  •Pattern和Matcher類的使用
public class RegexTest {
	public static void main(String[] args) {
		//鍵盤錄入郵箱
		Scanner sc = new Scanner(System.in);
		System.out.println("請輸入郵箱:");
		String email = sc.nextLine();
		
		//定義郵箱的規則
		//String regex = "[a-zA-Z_0-9]+@[a-zA-Z_0-9]{2,6}(\\.[a-zA-Z_0-9]{2,3})+";
		String regex = "\\w+@\\w{2,6}(\\.\\w{2,3})+";
		
		//調用功能,判斷便可
		boolean flag = email.matches(regex);
		
		//輸出結果
		System.out.println("flag:"+flag);
	}
}
package regex;

import java.util.Scanner;

public class Split {

	public static void main(String[] args) {
		String s1 = "18-24";
		String[] str = s1.split("-");
		int startAge = Integer.parseInt(str[0]);
		int endAge = Integer.parseInt(str[1]);
		System.out.println("請輸入年齡:");
		Scanner s = new Scanner(System.in);
		int age = s.nextInt();
		if(age >startAge && age <endAge){
			System.out.println("你就是我要找的人");
		}else{
			System.out.println("滾");
		}
		
	}

}
package cn.itcast_03;

import java.util.Arrays;

/*
 * 我有以下一個字符串:"91 27 46 38 50"
 * 請寫代碼實現最終輸出結果是:"27 38 46 50 91"
 * 
 * 分析:
 * 		A:定義一個字符串
 * 		B:把字符串進行分割,獲得一個字符串數組
 * 		C:把字符串數組變換成int數組
 * 		D:對int數組排序
 * 		E:把排序後的int數組在組裝成一個字符串
 * 		F:輸出字符串
 */
public class RegexTest {
	public static void main(String[] args) {
		// 定義一個字符串
		String s = "91 27 46 38 50";

		// 把字符串進行分割,獲得一個字符串數組
		String[] strArray = s.split(" ");

		// 把字符串數組變換成int數組
		int[] arr = new int[strArray.length];

		for (int x = 0; x < arr.length; x++) {
			arr[x] = Integer.parseInt(strArray[x]);
		}

		// 對int數組排序
		Arrays.sort(arr);

		// 把排序後的int數組在組裝成一個字符串
		StringBuilder sb = new StringBuilder();
		for (int x = 0; x < arr.length; x++) {
			sb.append(arr[x]).append(" ");
		}
		//轉化爲字符串
		String result = sb.toString().trim();
		
		//輸出字符串
		System.out.println("result:"+result);
	}
}
package cn.itcast_04;

/*
 * 替換功能
 *  	String類的public String replaceAll(String regex,String replacement)
 *  	使用給定的 replacement 替換此字符串全部匹配給定的正則表達式的子字符串。 
 */
public class RegexDemo {
	public static void main(String[] args) {
		// 定義一個字符串
		String s = "helloqq12345worldkh622112345678java";

		// 我要去除全部的數字,用*給替換掉
		// String regex = "\\d+";
		// String regex = "\\d";
		//String ss = "*";
		
		
		// 直接把數字幹掉
		String regex = "\\d+";
		String ss = "";

		String result = s.replaceAll(regex, ss);
		System.out.println(result);
	}
}
package cn.itcast_05;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
 * 獲取功能:
 * 獲取下面這個字符串中由三個字符組成的單詞
 * da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?
 */
public class RegexDemo2 {
	public static void main(String[] args) {
		// 定義字符串
		String s = "da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?";
		// 規則
		String regex = "\\b\\w{3}\\b";

		// 把規則編譯成模式對象
		Pattern p = Pattern.compile(regex);
		// 經過模式對象獲得匹配器對象
		Matcher m = p.matcher(s);
		// 調用匹配器對象的功能
		// 經過find方法就是查找有沒有知足條件的子串
		// public boolean find()
		// boolean flag = m.find();
		// System.out.println(flag);
		// // 如何獲得值呢?
		// // public String group()
		// String ss = m.group();
		// System.out.println(ss);
		//
		// // 再來一次
		// flag = m.find();
		// System.out.println(flag);
		// ss = m.group();
		// System.out.println(ss);

		while (m.find()) {
			System.out.println(m.group());
		}

		// 注意:必定要先find(),而後才能group()
		// IllegalStateException: No match found
		// String ss = m.group();
		// System.out.println(ss);
	}
}
package cn.itcast_01;

/*
 * Math:用於數學運算的類。
 * 成員變量:
 * 		public static final double PI
 * 		public static final double E
 * 成員方法:
 * 		public static int abs(int a):絕對值
 *		public static double ceil(double a):向上取整
 *		public static double floor(double a):向下取整
 *		public static int max(int a,int b):最大值 (min自學)
 *		public static double pow(double a,double b):a的b次冪
 *		public static double random():隨機數 [0.0,1.0)
 *		public static int round(float a) 四捨五入(參數爲double的自學)(全部數,加0.5,取整)
 *		public static double sqrt(double a):正平方根
 */
public class MathDemo {
	public static void main(String[] args) {
		// public static final double PI
		System.out.println("PI:" + Math.PI);
		// public static final double E
		System.out.println("E:" + Math.E);
		System.out.println("--------------");

		// public static int abs(int a):絕對值
		System.out.println("abs:" + Math.abs(10));
		System.out.println("abs:" + Math.abs(-10));
		System.out.println("--------------");

		// public static double ceil(double a):向上取整
		System.out.println("ceil:" + Math.ceil(12.34));
		System.out.println("ceil:" + Math.ceil(12.56));
		System.out.println("--------------");

		// public static double floor(double a):向下取整
		System.out.println("floor:" + Math.floor(12.34));
		System.out.println("floor:" + Math.floor(12.56));
		System.out.println("--------------");

		// public static int max(int a,int b):最大值
		System.out.println("max:" + Math.max(12, 23));
		// 需求:我要獲取三個數據中的最大值
		// 方法的嵌套調用
		System.out.println("max:" + Math.max(Math.max(12, 23), 18));
		// 需求:我要獲取四個數據中的最大值
		System.out.println("max:"
				+ Math.max(Math.max(12, 78), Math.max(34, 56)));
		System.out.println("--------------");

		// public static double pow(double a,double b):a的b次冪
		System.out.println("pow:" + Math.pow(2, 3));
		System.out.println("--------------");

		// public static double random():隨機數 [0.0,1.0)
		System.out.println("random:" + Math.random());
		// 獲取一個1-100之間的隨機數
		System.out.println("random:" + ((int) (Math.random() * 100) + 1));
		System.out.println("--------------");

		// public static int round(float a) 四捨五入(參數爲double的自學)
		System.out.println("round:" + Math.round(12.34f));
		System.out.println("round:" + Math.round(12.56f));
		System.out.println("--------------");
		
		//public static double sqrt(double a):正平方根
		System.out.println("sqrt:"+Math.sqrt(4));
	}
}
package cn.itcast_02;

import java.util.Scanner;

/*
 * 需求:請設計一個方法,能夠實現獲取任意範圍內的隨機數。
 * 
 * 分析:
 * 		A:鍵盤錄入兩個數據。
 * 			int strat;
 * 			int end;
 * 		B:想辦法獲取在start到end之間的隨機數
 * 			我寫一個功能實現這個效果,獲得一個隨機數。(int)
 * 		C:輸出這個隨機數
 */
public class MathDemo {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("請輸入開始數:");
		int start = sc.nextInt();
		System.out.println("請輸入結束數:");
		int end = sc.nextInt();

		for (int x = 0; x < 100; x++) {
			// 調用功能
			int num = getRandom(start, end);
			// 輸出結果
			System.out.println(num);
		}
	}

	/*
	 * 寫一個功能 兩個明確: 返回值類型:int 參數列表:int start,int end
	 */
	public static int getRandom(int start, int end) {
		// 回想咱們講過的1-100之間的隨機數
		// int number = (int) (Math.random() * 100) + 1;
		// int number = (int) (Math.random() * end) + start;
		// 發現有問題了,怎麼辦呢?
		int number = (int) (Math.random() * (end - start + 1)) + start;
		//end - start+1是最小和最大值之間的範圍,start是最開始的值
		return number;
	}
}
package cn.itcast_01;

import java.util.Random;

/*
 * Random:產生隨機數的類
 * 
 * 構造方法:
 * 		public Random():沒有給種子,用的是默認種子,是當前時間的毫秒值
 *		public Random(long seed):給出指定的種子
 *
 *		給定種子後,每次獲得的隨機數是相同的。
 *
 * 成員方法:
 * 		public int nextInt():返回的是int範圍內的隨機數
 *		public int nextInt(int n):返回的是[0,n)範圍的內隨機數
 */
public class RandomDemo {
	public static void main(String[] args) {
		// 建立對象
		// Random r = new Random();
		Random r = new Random(1111);

		for (int x = 0; x < 10; x++) {
			// int num = r.nextInt();
			int num = r.nextInt(100) + 1;
			System.out.println(num);
		}
	}
}
/*
 * System類包含一些有用的類字段和方法。它不能被實例化。 
 * 
 * 方法:
 * 	public static void gc():運行垃圾回收器。 
 *	public static void exit(int status):終止當前正在運行的 Java 虛擬機。參數用做狀態碼;根據慣例,非 0 的狀態碼錶示異常終止。通常給0. 
 *	public static long currentTimeMillis():返回以毫秒爲單位的當前時間
 *	public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
 */
package regex;

public class Person {
	
	private String name;
	private int age;
	
	public Person() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}

	@Override
	protected void finalize() throws Throwable {
		System.out.println("當前的對象被回收了:"+this);
		super.finalize();
	}
	
}
package regex;

public class SystemDemo {

	public static void main(String[] args) {
		Person p = new Person("趙雅芝",60);
		System.out.println(p);
		p = null;
		System.gc();
	}

}
public class SystemDemo {
	public static void main(String[] args) {
		// System.out.println("咱們喜歡林青霞(東方不敗)");
		// System.exit(0);
		// System.out.println("咱們也喜歡趙雅芝(白娘子)");

		// System.out.println(System.currentTimeMillis());

		// 單獨獲得這樣的實際目前對咱們來講意義不大
		// 那麼,它到底有什麼做用呢?
		// 要求:請你們給我統計這段程序的運行時間
		long start = System.currentTimeMillis();
		for (int x = 0; x < 100000; x++) {
			System.out.println("hello" + x);
		}
		long end = System.currentTimeMillis();
		System.out.println("共耗時:" + (end - start) + "毫秒");
	}
}
public class SystemDemo {
	public static void main(String[] args) {
		// 定義數組
		int[] arr = { 11, 22, 33, 44, 55 };
		int[] arr2 = { 6, 7, 8, 9, 10 };

		// 請你們看這個代碼的意思
		System.arraycopy(arr, 1, arr2, 2, 2);

		System.out.println(Arrays.toString(arr));
		System.out.println(Arrays.toString(arr2));
	}
}
package cn.itcast_01;

import java.math.BigInteger;

/*
 * BigInteger:可讓超過Integer範圍內的數據進行運算
 * 
 * 構造方法:
 * BigInteger(String val) 
 */
public class BigIntegerDemo {
	public static void main(String[] args) {
		// 這幾個測試,是爲了簡單超過int範圍內,Integer就不能再表示,因此就更談不上計算了。
		// Integer i = new Integer(100);
		// System.out.println(i);
		// // System.out.println(Integer.MAX_VALUE);
		// Integer ii = new Integer("2147483647");
		// System.out.println(ii);
		// // NumberFormatException
		// Integer iii = new Integer("2147483648");
		// System.out.println(iii);

		// 經過大整數來建立對象
		BigInteger bi = new BigInteger("2147483648");
		System.out.println("bi:" + bi);
	}
}
package cn.itcast_02;

import java.math.BigInteger;

/*
 * public BigInteger add(BigInteger val):加
 * public BigInteger subtract(BigInteger val):減
 * public BigInteger multiply(BigInteger val):乘
 * public BigInteger divide(BigInteger val):除
 * public BigInteger[] divideAndRemainder(BigInteger val):返回商和餘數的數組
 */
public class BigIntegerDemo {
	public static void main(String[] args) {
		BigInteger bi1 = new BigInteger("100");
		BigInteger bi2 = new BigInteger("50");

		// public BigInteger add(BigInteger val):加
		System.out.println("add:" + bi1.add(bi2));
		// public BigInteger subtract(BigInteger val):加
		System.out.println("subtract:" + bi1.subtract(bi2));
		// public BigInteger multiply(BigInteger val):加
		System.out.println("multiply:" + bi1.multiply(bi2));
		// public BigInteger divide(BigInteger val):加
		System.out.println("divide:" + bi1.divide(bi2));

		// public BigInteger[] divideAndRemainder(BigInteger val):返回商和餘數的數組
		BigInteger[] bis = bi1.divideAndRemainder(bi2);
		System.out.println("商:" + bis[0]);
		System.out.println("餘數:" + bis[1]);
	}
}
package cn.itcast_01;

/*
 * 看程序寫結果:結果和咱們想的有一點點不同,這是由於float類型的數據存儲和整數不同致使的。它們大部分的時候,都是帶有有效數字位。
 * 
 * 因爲在運算的時候,float類型和double很容易丟失精度,演示案例。因此,爲了能精確的表示、計算浮點數,Java提供了BigDecimal
 * 
 * BigDecimal類:不可變的、任意精度的有符號十進制數,能夠解決數據丟失問題。
 */
public class BigDecimalDemo {
	public static void main(String[] args) {
		System.out.println(0.09 + 0.01);
		System.out.println(1.0 - 0.32);
		System.out.println(1.015 * 100);
		System.out.println(1.301 / 100);

		System.out.println(1.0 - 0.12);
	}
}
package cn.itcast_02;

import java.math.BigDecimal;

/*
 * 構造方法:
 *     public BigDecimal(String val)
 * 
 * public BigDecimal add(BigDecimal augend)
 * public BigDecimal subtract(BigDecimal subtrahend)
 * public BigDecimal multiply(BigDecimal multiplicand)
 * public BigDecimal divide(BigDecimal divisor)
 * public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode):商,幾位小數,如何舍取
 */
public class BigDecimalDemo {
	public static void main(String[] args) {
		// System.out.println(0.09 + 0.01);
		// System.out.println(1.0 - 0.32);
		// System.out.println(1.015 * 100);
		// System.out.println(1.301 / 100);

		BigDecimal bd1 = new BigDecimal("0.09");
		BigDecimal bd2 = new BigDecimal("0.01");
		System.out.println("add:" + bd1.add(bd2));
		System.out.println("-------------------");

		BigDecimal bd3 = new BigDecimal("1.0");
		BigDecimal bd4 = new BigDecimal("0.32");
		System.out.println("subtract:" + bd3.subtract(bd4));
		System.out.println("-------------------");

		BigDecimal bd5 = new BigDecimal("1.015");
		BigDecimal bd6 = new BigDecimal("100");
		System.out.println("multiply:" + bd5.multiply(bd6));
		System.out.println("-------------------");

		BigDecimal bd7 = new BigDecimal("1.301");
		BigDecimal bd8 = new BigDecimal("100");
		System.out.println("divide:" + bd7.divide(bd8));
		System.out.println("divide:"
				+ bd7.divide(bd8, 3, BigDecimal.ROUND_HALF_UP));
		System.out.println("divide:"
				+ bd7.divide(bd8, 8, BigDecimal.ROUND_HALF_UP));
	}
}
package cn.itcast_01;

import java.util.Date;

/*
 * Date:表示特定的瞬間,精確到毫秒。 
 * 
 * 構造方法:
 * 		Date():根據當前的默認毫秒值建立日期對象
 * 		Date(long date):根據給定的毫秒值建立日期對象
 */
public class DateDemo {
	public static void main(String[] args) {
		// 建立對象
		Date d = new Date();
		System.out.println("d:" + d);

		// 建立對象
		// long time = System.currentTimeMillis();
		long time = 1000 * 60 * 60; // 1小時
		Date d2 = new Date(time);
		System.out.println("d2:" + d2);
	}
}
package cn.itcast_02;

import java.util.Date;

/*
 * public long getTime():獲取時間,以毫秒爲單位 //時間和毫秒的互換
 * public void setTime(long time):設置時間
 * 
 * 從Date獲得一個毫秒值
 * 		getTime()
 * 把一個毫秒值轉換爲Date
 * 		構造方法
 * 		setTime(long time)
 */
public class DateDemo {
	public static void main(String[] args) {
		// 建立對象
		Date d = new Date();

		// 獲取時間
		long time = d.getTime();
		System.out.println(time);
		// System.out.println(System.currentTimeMillis());

		System.out.println("d:" + d);
		// 設置時間
		d.setTime(1000);
		System.out.println("d:" + d);
	}
}
package cn.itcast_03;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 * Date	 --	 String(格式化)
 * 		public final String format(Date date)
 * 
 * String -- Date(解析)
 * 		public Date parse(String source)
 * 
 * DateForamt:能夠進行日期和字符串的格式化和解析,可是因爲是抽象類,因此使用具體子類SimpleDateFormat。
 * 
 * SimpleDateFormat的構造方法:
 * 		SimpleDateFormat():默認模式
 * 		SimpleDateFormat(String pattern):給定的模式
 * 			這個模式字符串該如何寫呢?
 * 			經過查看API,咱們就找到了對應的模式
 * 			年 y
 * 			月 M	
 * 			日 d
 * 			時 H
 * 			分 m
 * 			秒 s
 * 
 * 			2014年12月12日 12:12:12
 */
public class DateFormatDemo {
	public static void main(String[] args) throws ParseException {
		// Date -- String  日期轉化成字符串
		// 建立日期對象
		Date d = new Date();
		// 建立格式化對象
		// SimpleDateFormat sdf = new SimpleDateFormat();
		// 給定模式
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
		// public final String format(Date date)
		String s = sdf.format(d);
		System.out.println(s);
		
		
		//String -- Date  字符串轉化成日期
		String str = "2008-08-08 12:12:12";
		//在把一個字符串解析爲日期的時候,請注意格式必須和給定的字符串格式匹配
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date dd = sdf2.parse(str);
		System.out.println(dd);
	}
}

工具類的建立:
java

package cn.itcast_04;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 這是日期和字符串相互轉換的工具類
 * 
 * @author 風清揚
 */
public class DateUtil {
	private DateUtil() {
	}

	/**
	 * 這個方法的做用就是把日期轉成一個字符串
	 * 
	 * @param d
	 *            被轉換的日期對象
	 * @param format
	 *            傳遞過來的要被轉換的格式
	 * @return 格式化後的字符串
	 */
	public static String dateToString(Date d, String format) {
		// SimpleDateFormat sdf = new SimpleDateFormat(format);
		// return sdf.format(d);
		return new SimpleDateFormat(format).format(d);
	}

	/**
	 * 這個方法的做用就是把一個字符串解析成一個日期對象
	 * 
	 * @param s
	 *            被解析的字符串
	 * @param format
	 *            傳遞過來的要被轉換的格式
	 * @return 解析後的日期對象
	 * @throws ParseException
	 */
	public static Date stringToDate(String s, String format)
			throws ParseException {
		return new SimpleDateFormat(format).parse(s);
	}
}
package cn.itcast_04;

import java.text.ParseException;
import java.util.Date;

/*
 * 工具類的測試
 */
public class DateUtilDemo {
	public static void main(String[] args) throws ParseException {
		Date d = new Date();
		// yyyy-MM-dd HH:mm:ss
		String s = DateUtil.dateToString(d, "yyyy年MM月dd日 HH:mm:ss");
		System.out.println(s);

		String s2 = DateUtil.dateToString(d, "yyyy年MM月dd日");
		System.out.println(s2);

		String s3 = DateUtil.dateToString(d, "HH:mm:ss");
		System.out.println(s3);

		String str = "2014-10-14";
		Date dd = DateUtil.stringToDate(str, "yyyy-MM-dd");
		System.out.println(dd);
	}
}
package cn.itcast_05;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/*
 * 算一下你來到這個世界多少天?
 * 
 * 分析:
 * 		A:鍵盤錄入你的出生的年月日
 * 		B:把該字符串轉換爲一個日期
 * 		C:經過該日期獲得一個毫秒值
 * 		D:獲取當前時間的毫秒值
 * 		E:用D-C獲得一個毫秒值
 * 		F:把E的毫秒值轉換爲年
 * 			/1000/60/60/24
 */
public class MyYearOldDemo {
	public static void main(String[] args) throws ParseException {
		// 鍵盤錄入你的出生的年月日
		Scanner sc = new Scanner(System.in);
		System.out.println("請輸入你的出生年月日:");
		String line = sc.nextLine();

		// 把該字符串轉換爲一個日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date d = sdf.parse(line);

		// 經過該日期獲得一個毫秒值
		long myTime = d.getTime();

		// 獲取當前時間的毫秒值
		long nowTime = System.currentTimeMillis();

		// 用D-C獲得一個毫秒值
		long time = nowTime - myTime;

		// 把E的毫秒值轉換爲年
		long day = time / 1000 / 60 / 60 / 24;

		System.out.println("你來到這個世界:" + day + "天");
	}
}
package cn.itcast_01;

import java.util.Calendar;

/*
 * Calendar:它爲特定瞬間與一組諸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日曆字段之間的轉換提供了一些方法,併爲操做日曆字段(例如得到下星期的日期)提供了一些方法。
 * 
 * public int get(int field):返回給定日曆字段的值。日曆類中的每一個日曆字段都是靜態的成員變量,而且是int類型。
 */
public class CalendarDemo {
	public static void main(String[] args) {
		// 其日曆字段已由當前日期和時間初始化:
		Calendar rightNow = Calendar.getInstance(); // 子類對象

		// 獲取年
		int year = rightNow.get(Calendar.YEAR);
		// 獲取月
		int month = rightNow.get(Calendar.MONTH);
		// 獲取日
		int date = rightNow.get(Calendar.DATE);

		System.out.println(year + "年" + (month + 1) + "月" + date + "日");
	}
}

/*
 * abstract class Person { public static Person getPerson() { return new
 * Student(); } }
 * 
 * class Student extends Person {
 * 
 * }
 */

Calendar類的add()和set()方法
正則表達式

package util;

import java.util.Calendar;

public class CaledndarDemo {
	public static void main(String[] args) {
		Calendar rightNow = Calendar.getInstance();
		shuChu(rightNow);
		rightNow.add(Calendar.YEAR,-5);
		shuChu(rightNow);
		rightNow.add(Calendar.MONTH,+5);
		shuChu(rightNow);
		rightNow.add(Calendar.DATE,-5);
		shuChu(rightNow);
		rightNow.set(2020, 9, 12);
		shuChu(rightNow);
	}

	private static void shuChu(Calendar rightNow) {
		
		int year = rightNow.get(Calendar.YEAR);
		int mouth = rightNow.get(Calendar.MONTH);
		int date = rightNow.get(Calendar.DATE);
		
		System.out.println(year+"年"+(mouth+1)+"月"+date+"日");//mouth從0開始
		
	}
}

如何獲取任意年份的2月份有多少天
數組

package cn.itcast_03;

import java.util.Calendar;
import java.util.Scanner;

/*
 * 獲取任意一年的二月有多少天
 * 
 * 分析:
 * 		A:鍵盤錄入任意的年份
 * 		B:設置日曆對象的年月日
 * 			年就是A輸入的數據
 * 			月是2
 * 			日是1
 * 		C:把時間往前推一天,就是2月的最後一天
 * 		D:獲取這一天輸出便可
 */
public class CalendarTest {
	public static void main(String[] args) {
		// 鍵盤錄入任意的年份
		Scanner sc = new Scanner(System.in);
		System.out.println("請輸入年份:");
		int year = sc.nextInt();

		// 設置日曆對象的年月日
		Calendar c = Calendar.getInstance();
		c.set(year, 2, 1); // 實際上是這一年的3月1日
		// 把時間往前推一天,就是2月的最後一天
		c.add(Calendar.DATE, -1);

		// 獲取這一天輸出便可
		System.out.println(c.get(Calendar.DATE));
	}
}
相關文章
相關標籤/搜索