這輩子沒(méi)辦法做太多事情佩谣,所以每一件都要做到精彩絕倫盖灸。 --------史蒂夫.喬布斯
Math
(1)針對(duì)數(shù)學(xué)運(yùn)算進(jìn)行操作的類
(2)常見(jiàn)方法(自己補(bǔ)齊)
A:絕對(duì)值
B:向上取整
C:向下取整
D:兩個(gè)數(shù)據(jù)中的大值
E:a的b次冪
F:隨機(jī)數(shù)
G:四舍五入
H:正平方根
package cn.yuan;
/*
* Math:用于數(shù)學(xué)運(yùn)算的類均驶。
* 成員變量:
* public static final double PI
* public static final double E
* 成員方法:
* public static int abs(int a):絕對(duì)值
* public static double ceil(double a):向上取整
* public static double floor(double a):向下取整
* public static int max(int a,int b):最大值 (min自學(xué))
* public static double pow(double a,double b):a的b次冪
* public static double random():隨機(jī)數(shù) [0.0,1.0)
* public static int round(float a) 四舍五入(參數(shù)為double的自學(xué))
* 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):絕對(duì)值
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));
// 需求:我要獲取三個(gè)數(shù)據(jù)中的最大值
// 方法的嵌套調(diào)用
System.out.println("max:" + Math.max(Math.max(12, 23), 18));
// 需求:我要獲取四個(gè)數(shù)據(jù)中的最大值
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():隨機(jī)數(shù) [0.0,1.0)
System.out.println("random:" + Math.random());
// 獲取一個(gè)1-100之間的隨機(jī)數(shù)
System.out.println("random:" + ((int) (Math.random() * 100) + 1));
System.out.println("--------------");
// public static int round(float a) 四舍五入(參數(shù)為double的自學(xué))
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;
/*
* 需求:請(qǐng)?jiān)O(shè)計(jì)一個(gè)方法郑口,可以實(shí)現(xiàn)獲取任意范圍內(nèi)的隨機(jī)數(shù)颓影。
*
* 分析:
* A:鍵盤錄入兩個(gè)數(shù)據(jù)各淀。
* int strat;
* int end;
* B:想辦法獲取在start到end之間的隨機(jī)數(shù)
* 我寫(xiě)一個(gè)功能實(shí)現(xiàn)這個(gè)效果,得到一個(gè)隨機(jī)數(shù)诡挂。(int)
* C:輸出這個(gè)隨機(jī)數(shù)
*/
public class MathDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請(qǐng)輸入開(kāi)始數(shù):");
int start = sc.nextInt();
System.out.println("請(qǐng)輸入結(jié)束數(shù):");
int end = sc.nextInt();
for (int x = 0; x < 100; x++) {
// 調(diào)用功能
int num = getRandom(start, end);
// 輸出結(jié)果
System.out.println(num);
}
}
/*
* 寫(xiě)一個(gè)功能 兩個(gè)明確: 返回值類型:int 參數(shù)列表:int start,int end
*/
public static int getRandom(int start, int end) {
// 回想我們講過(guò)的1-100之間的隨機(jī)數(shù)
// int number = (int) (Math.random() * 100) + 1;
// int number = (int) (Math.random() * end) + start;
// 發(fā)現(xiàn)有問(wèn)題了碎浇,怎么辦呢?
int number = (int) (Math.random() * (end - start + 1)) + start;
return number;
}
}
Random
(1)用于產(chǎn)生隨機(jī)數(shù)的類
(2)構(gòu)造方法:
A:Random() 默認(rèn)種子,每次產(chǎn)生的隨機(jī)數(shù)不同
B:Random(long seed) 指定種子璃俗,每次種子相同奴璃,隨機(jī)數(shù)就相同
(3)成員方法:
A:int nextInt() 返回int范圍內(nèi)的隨機(jī)數(shù)
B:int nextInt(int n) 返回[0,n)范圍內(nèi)的隨機(jī)數(shù)
package cn.yuan;
import java.util.Random;
/*
* Random:產(chǎn)生隨機(jī)數(shù)的類
*
* 構(gòu)造方法:
* public Random():沒(méi)有給種子,用的是默認(rèn)種子城豁,是當(dāng)前時(shí)間的毫秒值
* public Random(long seed):給出指定的種子
*
* 給定種子后苟穆,每次得到的隨機(jī)數(shù)是相同的。
*
* 成員方法:
* public int nextInt():返回的是int范圍內(nèi)的隨機(jī)數(shù)
* public int nextInt(int n):返回的是[0,n)范圍的內(nèi)隨機(jī)數(shù)
*/
public class RandomDemo {
public static void main(String[] args) {
// 創(chuàng)建對(duì)象
// 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
(1)系統(tǒng)類,提供了一些有用的字段和方法
(2)成員方法
A:運(yùn)行垃圾回收器
B:退出jvm
C:獲取當(dāng)前時(shí)間的毫秒值
D:數(shù)組復(fù)制
package cn.yuan;
public class Person {
private String name;
private int age;
public Person() {
super();
}
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("當(dāng)前的對(duì)象被回收了" + this);
super.finalize();
}
}
/*
* System類包含一些有用的類字段和方法。它不能被實(shí)例化雳旅。
*
* 方法:
* public static void gc():運(yùn)行垃圾回收器跟磨。
* public static void exit(int status):終止當(dāng)前正在運(yùn)行的 Java 虛擬機(jī)。參數(shù)用作狀態(tài)碼攒盈;根據(jù)慣例抵拘,非 0 的狀態(tài)碼表示異常終止。
* public static long currentTimeMillis():返回以毫秒為單位的當(dāng)前時(shí)間
* public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
*/
class SystemDemo {
public static void main(String[] args) {
Person p = new Person("趙雅芝", 60);
System.out.println(p);
p = null; // 讓p不再指定堆內(nèi)存
System.gc();
// System.out.println("我們喜歡林青霞(東方不敗)");
// System.exit(0);
// System.out.println("我們也喜歡趙雅芝(白娘子)");
// System.out.println(System.currentTimeMillis());
// 單獨(dú)得到這樣的實(shí)際目前對(duì)我們來(lái)說(shuō)意義不大
// 那么沦童,它到底有什么作用呢?
// 要求:請(qǐng)大家給我統(tǒng)計(jì)這段程序的運(yùn)行時(shí)間
long start = System.currentTimeMillis();
for (int x = 0; x < 100000; x++) {
System.out.println("hello" + x);
}
long end = System.currentTimeMillis();
System.out.println("共耗時(shí):" + (end - start) + "毫秒");
// 定義數(shù)組
int[] arr = { 11, 22, 33, 44, 55 };
int[] arr2 = { 6, 7, 8, 9, 10 };
// 請(qǐng)大家看這個(gè)代碼的意思
System.arraycopy(arr, 1, arr2, 2, 2);
System.out.println(Arrays.toString(arr));
System.out.println(Arrays.toString(arr2));
}
}
BigInteger
(1)針對(duì)大整數(shù)的運(yùn)算
(2)構(gòu)造方法
A:BigInteger(String s)
(3)成員方法(自己補(bǔ)齊)
A:加
B:減
C:乘
D:除
E:商和余數(shù)
package cn.yuan;
import java.math.BigInteger;
/*
* BigInteger:可以讓超過(guò)Integer范圍內(nèi)的數(shù)據(jù)進(jìn)行運(yùn)算
*
* 構(gòu)造方法:
* BigInteger(String val)
*/
public class BigIntegerDemo {
public static void main(String[] args) {
// 這幾個(gè)測(cè)試,是為了簡(jiǎn)單超過(guò)int范圍內(nèi)叹话,Integer就不能再表示偷遗,所以就更談不上計(jì)算了。
// 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);
// 通過(guò)大整數(shù)來(lái)創(chuàng)建對(duì)象
BigInteger bi = new BigInteger("2147483648");
System.out.println("bi:" + bi);
}
}
package cn.yuan;
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):返回商和余數(shù)的數(shù)組
*/
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):返回商和余數(shù)的數(shù)組
BigInteger[] bis = bi1.divideAndRemainder(bi2);
System.out.println("商:" + bis[0]);
System.out.println("余數(shù):" + bis[1]);
}
}
BigDecimal
(1)浮點(diǎn)數(shù)據(jù)做運(yùn)算驼壶,會(huì)丟失精度氏豌。所以,針對(duì)浮點(diǎn)數(shù)據(jù)的操作建議采用BigDecimal热凹。(金融相關(guān)的項(xiàng)目)
(2)構(gòu)造方法
A:BigDecimal(String s)
(3)成員方法:
A:加
B:減
C:乘
D:除
E:自己保留小數(shù)幾位
package cn.yuan;
/*
* 看程序?qū)懡Y(jié)果:結(jié)果和我們想的有一點(diǎn)點(diǎn)不一樣泵喘,這是因?yàn)閒loat類型的數(shù)據(jù)存儲(chǔ)和整數(shù)不一樣導(dǎo)致的。它們大部分的時(shí)候般妙,都是帶有有效數(shù)字位纪铺。
*
* 由于在運(yùn)算的時(shí)候,float類型和double很容易丟失精度碟渺,演示案例鲜锚。所以,為了能精確的表示苫拍、計(jì)算浮點(diǎn)數(shù)芜繁,Java提供了BigDecimal
*
* BigDecimal類:不可變的、任意精度的有符號(hào)十進(jìn)制數(shù),可以解決數(shù)據(jù)丟失問(wèn)題绒极。
*/
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.yuan;
import java.math.BigDecimal;
/*
* 構(gòu)造方法:
* 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):商骏令,幾位小數(shù),如何舍取
*/
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));
}
}
Date/DateFormat
(1)Date是日期類垄提,可以精確到毫秒榔袋。
A:構(gòu)造方法
Date()
Date(long time)
B:成員方法
getTime()
setTime(long time)
C:日期和毫秒值的相互轉(zhuǎn)換
案例:你來(lái)到這個(gè)世界多少天了?
(2)DateFormat針對(duì)日期進(jìn)行格式化和針對(duì)字符串進(jìn)行解析的類,但是是抽象類铡俐,所以使用其子類SimpleDateFormat
A:SimpleDateFormat(String pattern) 給定模式
yyyy-MM-dd HH:mm:ss
B:日期和字符串的轉(zhuǎn)換
a:Date -- String
format()
b:String -- Date
parse()
C:案例:
制作了一個(gè)針對(duì)日期操作的工具類摘昌。
package cn.yuan;
import java.util.Date;
/*
* Date:表示特定的瞬間,精確到毫秒高蜂。
*
* 構(gòu)造方法:
* Date():根據(jù)當(dāng)前的默認(rèn)毫秒值創(chuàng)建日期對(duì)象
* Date(long date):根據(jù)給定的毫秒值創(chuàng)建日期對(duì)象
* public long getTime():獲取時(shí)間聪黎,以毫秒為單位
* public void setTime(long time):設(shè)置時(shí)間
*
* 從Date得到一個(gè)毫秒值
* getTime()
* 把一個(gè)毫秒值轉(zhuǎn)換為Date
* 構(gòu)造方法
* setTime(long time)
*/
public class DateDemo {
public static void main(String[] args) {
// 創(chuàng)建對(duì)象
Date d = new Date();
System.out.println("d:" + d);
// 創(chuàng)建對(duì)象
// long time = System.currentTimeMillis();
long time = 1000 * 60 * 60; // 1小時(shí)
Date d2 = new Date(time);
System.out.println("d2:" + d2);
//---------------------------------
// 創(chuàng)建對(duì)象
Date d = new Date();
// 獲取時(shí)間
long time = d.getTime();
System.out.println(time);
// System.out.println(System.currentTimeMillis());
System.out.println("d:" + d);
// 設(shè)置時(shí)間
d.setTime(1000);
System.out.println("d:" + d);
}
}
package cn.yuan;
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:可以進(jìn)行日期和字符串的格式化和解析,但是由于是抽象類,所以使用具體子類SimpleDateFormat稿饰。
*
* SimpleDateFormat的構(gòu)造方法:
* SimpleDateFormat():默認(rèn)模式
* SimpleDateFormat(String pattern):給定的模式
* 這個(gè)模式字符串該如何寫(xiě)呢?
* 通過(guò)查看API锦秒,我們就找到了對(duì)應(yīng)的模式
* 年 y
* 月 M
* 日 d
* 時(shí) H
* 分 m
* 秒 s
*
* 2021年10月09日 12:12:12
*/
public class DateFormatDemo {
public static void main(String[] args) throws ParseException {
// Date -- String
// 創(chuàng)建日期對(duì)象
Date d = new Date();
// 創(chuàng)建格式化對(duì)象
// 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";
//在把一個(gè)字符串解析為日期的時(shí)候,請(qǐng)注意格式必須和給定的字符串格式匹配
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dd = sdf2.parse(str);
System.out.println(dd);
}
}
package cn.yuan;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 這是日期和字符串相互轉(zhuǎn)換的工具類
*
* @author 小源同學(xué)
*/
public class DateUtil {
private DateUtil() {
}
/**
* 這個(gè)方法的作用就是把日期轉(zhuǎn)成一個(gè)字符串
*
* @param d
* 被轉(zhuǎn)換的日期對(duì)象
* @param format
* 傳遞過(guò)來(lái)的要被轉(zhuǎn)換的格式
* @return 格式化后的字符串
*/
public static String dateToString(Date d, String format) {
// SimpleDateFormat sdf = new SimpleDateFormat(format);
// return sdf.format(d);
return new SimpleDateFormat(format).format(d);
}
/**
* 這個(gè)方法的作用就是把一個(gè)字符串解析成一個(gè)日期對(duì)象
*
* @param s
* 被解析的字符串
* @param format
* 傳遞過(guò)來(lái)的要被轉(zhuǎn)換的格式
* @return 解析后的日期對(duì)象
* @throws ParseException
*/
public static Date stringToDate(String s, String format)
throws ParseException {
return new SimpleDateFormat(format).parse(s);
}
}
package cn.yuan;
import java.text.ParseException;
import java.util.Date;
/*
* 工具類的測(cè)試
*/
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.yuan;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
/*
* 算一下你來(lái)到這個(gè)世界多少天?
*
* 分析:
* A:鍵盤錄入你的出生的年月日
* B:把該字符串轉(zhuǎn)換為一個(gè)日期
* C:通過(guò)該日期得到一個(gè)毫秒值
* D:獲取當(dāng)前時(shí)間的毫秒值
* E:用D-C得到一個(gè)毫秒值
* F:把E的毫秒值轉(zhuǎn)換為年
* /1000/60/60/24
*/
public class MyYearOldDemo {
public static void main(String[] args) throws ParseException {
// 鍵盤錄入你的出生的年月日
Scanner sc = new Scanner(System.in);
System.out.println("請(qǐng)輸入你的出生年月日:");
String line = sc.nextLine();
// 把該字符串轉(zhuǎn)換為一個(gè)日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse(line);
// 通過(guò)該日期得到一個(gè)毫秒值
long myTime = d.getTime();
// 獲取當(dāng)前時(shí)間的毫秒值
long nowTime = System.currentTimeMillis();
// 用D-C得到一個(gè)毫秒值
long time = nowTime - myTime;
// 把E的毫秒值轉(zhuǎn)換為年
long day = time / 1000 / 60 / 60 / 24;
System.out.println("你來(lái)到這個(gè)世界:" + day + "天");
}
}
Calendar
(1)日歷類喉镰,封裝了所有的日歷字段值旅择,通過(guò)統(tǒng)一的方法根據(jù)傳入不同的日歷字段可以獲取值。
(2)如何得到一個(gè)日歷對(duì)象呢?
Calendar rightNow = Calendar.getInstance();
本質(zhì)返回的是子類對(duì)象
(3)成員方法
A:根據(jù)日歷字段得到對(duì)應(yīng)的值
B:根據(jù)日歷字段和一個(gè)正負(fù)數(shù)確定是添加還是減去對(duì)應(yīng)日歷字段的值
C:設(shè)置日歷對(duì)象的年月日
(4)案例:
計(jì)算任意一年的2月份有多少天?
package cn.yuan;
import java.util.Calendar;
/*
* Calendar:它為特定瞬間與一組諸如 YEAR侣姆、MONTH生真、DAY_OF_MONTH、HOUR 等 日歷字段之間的轉(zhuǎn)換提供了一些方法捺宗,并為操作日歷字段(例如獲得下星期的日期)提供了一些方法柱蟀。
*
* public int get(int field):返回給定日歷字段的值。日歷類中的每個(gè)日歷字段都是靜態(tài)的成員變量蚜厉,并且是int類型长已。
*/
public class CalendarDemo {
public static void main(String[] args) {
// 其日歷字段已由當(dāng)前日期和時(shí)間初始化:
Calendar rightNow = Calendar.getInstance(); // 子類對(duì)象
// 獲取年
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 {
*
* }
*/
package cn.yuan;
import java.util.Calendar;
/*
* public void add(int field,int amount):根據(jù)給定的日歷字段和對(duì)應(yīng)的時(shí)間,來(lái)對(duì)當(dāng)前的日歷進(jìn)行操作昼牛。
* public final void set(int year,int month,int date):設(shè)置當(dāng)前日歷的年月日
*/
public class CalendarDemo {
public static void main(String[] args) {
// 獲取當(dāng)前的日歷時(shí)間
Calendar c = Calendar.getInstance();
// 獲取年
int year = c.get(Calendar.YEAR);
// 獲取月
int month = c.get(Calendar.MONTH);
// 獲取日
int date = c.get(Calendar.DATE);
System.out.println(year + "年" + (month + 1) + "月" + date + "日");
// // 三年前的今天
// c.add(Calendar.YEAR, -3);
// // 獲取年
// year = c.get(Calendar.YEAR);
// // 獲取月
// month = c.get(Calendar.MONTH);
// // 獲取日
// date = c.get(Calendar.DATE);
// System.out.println(year + "年" + (month + 1) + "月" + date + "日");
// 5年后的10天前
c.add(Calendar.YEAR, 5);
c.add(Calendar.DATE, -10);
// 獲取年
year = c.get(Calendar.YEAR);
// 獲取月
month = c.get(Calendar.MONTH);
// 獲取日
date = c.get(Calendar.DATE);
System.out.println(year + "年" + (month + 1) + "月" + date + "日");
System.out.println("--------------");
c.set(2011, 11, 11);
// 獲取年
year = c.get(Calendar.YEAR);
// 獲取月
month = c.get(Calendar.MONTH);
// 獲取日
date = c.get(Calendar.DATE);
System.out.println(year + "年" + (month + 1) + "月" + date + "日");
}
}
案例
package cn.itcast_03;
import java.util.Calendar;
import java.util.Scanner;
/*
* 獲取任意一年的二月有多少天
*
* 分析:
* A:鍵盤錄入任意的年份
* B:設(shè)置日歷對(duì)象的年月日
* 年就是A輸入的數(shù)據(jù)
* 月是2
* 日是1
* C:把時(shí)間往前推一天术瓮,就是2月的最后一天
* D:獲取這一天輸出即可
*/
public class CalendarTest {
public static void main(String[] args) {
// 鍵盤錄入任意的年份
Scanner sc = new Scanner(System.in);
System.out.println("請(qǐng)輸入年份:");
int year = sc.nextInt();
// 設(shè)置日歷對(duì)象的年月日
Calendar c = Calendar.getInstance();
c.set(year, 2, 1); // 其實(shí)是這一年的3月1日
// 把時(shí)間往前推一天,就是2月的最后一天
c.add(Calendar.DATE, -1);
// 獲取這一天輸出即可
System.out.println(c.get(Calendar.DATE));
}
}
正則表達(dá)式
(1)就是符合一定規(guī)則的字符串
(2)常見(jiàn)規(guī)則
A:字符
x 字符 x贰健。舉例:'a'表示字符a
\ 反斜線字符胞四。
\n 新行(換行)符 ('\u000A')
\r 回車符 ('\u000D')
B:字符類
[abc] a、b 或 c(簡(jiǎn)單類)
[^abc] 任何字符伶椿,除了 a撬讽、b 或 c(否定)
[a-zA-Z] a到 z 或 A到 Z,兩頭的字母包括在內(nèi)(范圍)
[0-9] 0到9的字符都包括
C:預(yù)定義字符類
. 任何字符悬垃。我的就是.字符本身游昼,怎么表示呢? .
\d 數(shù)字:[0-9]
\w 單詞字符:[a-zA-Z_0-9]
在正則表達(dá)式里面組成單詞的東西必須有這些東西組成
D:邊界匹配器
^ 行的開(kāi)頭
$ 行的結(jié)尾
\b 單詞邊界
就是不是單詞字符的地方。
舉例:hello world?haha;xixi
E:Greedy 數(shù)量詞
X? X尝蠕,一次或一次也沒(méi)有
X* X烘豌,零次或多次
X+ X,一次或多次
X{n} X看彼,恰好 n 次
X{n,} X廊佩,至少 n 次
X{n,m} X,至少 n 次靖榕,但是不超過(guò) m 次
(3)案例
A:判斷電話號(hào)碼和郵箱
B:按照不同的規(guī)則分割數(shù)據(jù)
C:把論壇中的數(shù)字替換為*
D:獲取字符串中由3個(gè)字符組成的單詞
校驗(yàn)qq號(hào)碼
package cn.yuan;
import java.util.Scanner;
/*
* 校驗(yàn)qq號(hào)碼.
* 1:要求必須是5-15位數(shù)字
* 2:0不能開(kāi)頭
*
* 分析:
* A:鍵盤錄入一個(gè)QQ號(hào)碼
* B:寫(xiě)一個(gè)功能實(shí)現(xiàn)校驗(yàn)
* C:調(diào)用功能标锄,輸出結(jié)果。
*/
public class RegexDemo {
public static void main(String[] args) {
// 創(chuàng)建鍵盤錄入對(duì)象
Scanner sc = new Scanner(System.in);
System.out.println("請(qǐng)輸入你的QQ號(hào)碼:");
String qq = sc.nextLine();
System.out.println("checkQQ:"+checkQQ(qq));
}
/*
* 寫(xiě)一個(gè)功能實(shí)現(xiàn)校驗(yàn) 兩個(gè)明確: 明確返回值類型:boolean 明確參數(shù)列表:String qq
*/
public static boolean checkQQ(String qq) {
boolean flag = true;
// 校驗(yàn)長(zhǎng)度
if (qq.length() >= 5 && qq.length() <= 15) {
// 0不能開(kāi)頭
if (!qq.startsWith("0")) {
// 必須是數(shù)字
char[] chs = qq.toCharArray();
for (int x = 0; x < chs.length; x++) {
char ch = chs[x];
if (!Character.isDigit(ch)) {
flag = false;
break;
}
}
} else {
flag = false;
}
} else {
flag = false;
}
return flag;
}
//正則
public static boolean checkQQ(String qq) {
// String regex ="[1-9][0-9]{4,14}";
// //public boolean matches(String regex)告知此字符串是否匹配給定的正則表達(dá)式
// boolean flag = qq.matches(regex);
// return flag;
//return qq.matches("[1-9][0-9]{4,14}");
return qq.matches("[1-9]\\d{4,14}");
}
}
校驗(yàn)手機(jī)號(hào)
package cn.yuan;
import java.util.Scanner;
/*
* 判斷功能
* String類的public boolean matches(String regex)
*
* 需求:
* 判斷手機(jī)號(hào)碼是否滿足要求?
*
* 分析:
* A:鍵盤錄入手機(jī)號(hào)碼
* B:定義手機(jī)號(hào)碼的規(guī)則
* 13436975980
* 13688886868
* 13866668888
* 13456789012
* 13123456789
* 18912345678
* 18886867878
* 18638833883
* C:調(diào)用功能茁计,判斷即可
* D:輸出結(jié)果
*/
public class RegexDemo {
public static void main(String[] args) {
//鍵盤錄入手機(jī)號(hào)碼
Scanner sc = new Scanner(System.in);
System.out.println("請(qǐng)輸入你的手機(jī)號(hào)碼:");
String phone = sc.nextLine();
//定義手機(jī)號(hào)碼的規(guī)則
String regex = "1[38]\\d{9}";
//調(diào)用功能料皇,判斷即可
boolean flag = phone.matches(regex);
//輸出結(jié)果
System.out.println("flag:"+flag);
}
}
校驗(yàn)郵箱
package cn.yuan;
import java.util.Scanner;
/*
* 校驗(yàn)郵箱
*
* 分析:
* A:鍵盤錄入郵箱
* B:定義郵箱的規(guī)則
* 1517806580@qq.com
* liuyi@163.com
* linqingxia@126.com
* fengqingyang@sina.com.cn
* fqy@itcast.cn
* C:調(diào)用功能,判斷即可
* D:輸出結(jié)果
*/
public class RegexTest {
public static void main(String[] args) {
//鍵盤錄入郵箱
Scanner sc = new Scanner(System.in);
System.out.println("請(qǐng)輸入郵箱:");
String email = sc.nextLine();
//定義郵箱的規(guī)則
//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})+";
//調(diào)用功能,判斷即可
boolean flag = email.matches(regex);
//輸出結(jié)果
System.out.println("flag:"+flag);
}
}
分割功能
package cn.yuan;
import java.util.Scanner;
/*
* 分割功能
* String類的public String[] split(String regex)
* 根據(jù)給定正則表達(dá)式的匹配拆分此字符串践剂。
*
* 舉例:
* 百合網(wǎng)鬼譬,世紀(jì)佳緣,珍愛(ài)網(wǎng),QQ
* 搜索好友
* 性別:女
* 范圍:"18-24"
*
* age>=18 && age<=24
*/
public class RegexDemo {
public static void main(String[] args) {
//定義一個(gè)年齡搜索范圍
String ages = "18-24";
//定義規(guī)則
String regex = "-";
//調(diào)用方法
String[] strArray = ages.split(regex);
// //遍歷
// for(int x=0; x<strArray.length; x++){
// System.out.println(strArray[x]);
// }
//如何得到int類型的呢?
int startAge = Integer.parseInt(strArray[0]);
int endAge = Integer.parseInt(strArray[1]);
//鍵盤錄入年齡
Scanner sc = new Scanner(System.in);
System.out.println("請(qǐng)輸入你的年齡:");
int age = sc.nextInt();
if(age>=startAge && age<=endAge) {
System.out.println("你就是我想找的");
}else {
System.out.println("不符合我的要求,gun");
}
}
}
練習(xí)
package cn.yuan;
import java.util.Arrays;
/*
* 我有如下一個(gè)字符串:"91 27 46 38 50"
* 請(qǐng)寫(xiě)代碼實(shí)現(xiàn)最終輸出結(jié)果是:"27 38 46 50 91"
*
* 分析:
* A:定義一個(gè)字符串
* B:把字符串進(jìn)行分割逊脯,得到一個(gè)字符串?dāng)?shù)組
* C:把字符串?dāng)?shù)組變換成int數(shù)組
* D:對(duì)int數(shù)組排序
* E:把排序后的int數(shù)組在組裝成一個(gè)字符串
* F:輸出字符串
*/
public class RegexTest {
public static void main(String[] args) {
// 定義一個(gè)字符串
String s = "91 27 46 38 50";
// 把字符串進(jìn)行分割优质,得到一個(gè)字符串?dāng)?shù)組
String[] strArray = s.split(" ");
// 把字符串?dāng)?shù)組變換成int數(shù)組
int[] arr = new int[strArray.length];
for (int x = 0; x < arr.length; x++) {
arr[x] = Integer.parseInt(strArray[x]);
}
// 對(duì)int數(shù)組排序
Arrays.sort(arr);
// 把排序后的int數(shù)組在組裝成一個(gè)字符串
StringBuilder sb = new StringBuilder();
for (int x = 0; x < arr.length; x++) {
sb.append(arr[x]).append(" ");
}
//轉(zhuǎn)化為字符串
String result = sb.toString().trim();
//輸出字符串
System.out.println("result:"+result);
}
}
替換功能
package cn.yuan;
/*
* 替換功能
* String類的public String replaceAll(String regex,String replacement)
* 使用給定的 replacement 替換此字符串所有匹配給定的正則表達(dá)式的子字符串。
*/
public class RegexDemo {
public static void main(String[] args) {
// 定義一個(gè)字符串
String s = "helloqq12345worldkh622112345678java";
// 我要去除所有的數(shù)字,用*給替換掉
// String regex = "\\d+";
// String regex = "\\d";
//String ss = "*";
// 直接把數(shù)字干掉
String regex = "\\d+";
String ss = "";
String result = s.replaceAll(regex, ss);
System.out.println(result);
}
}
獲取功能
package cn.yuan;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*
* 獲取功能
* Pattern和Matcher類的使用
*
* 模式和匹配器的基本使用順序
*/
public class RegexDemo {
public static void main(String[] args) {
// 模式和匹配器的典型調(diào)用順序
// 把正則表達(dá)式編譯成模式對(duì)象
Pattern p = Pattern.compile("a*b");
// 通過(guò)模式對(duì)象得到匹配器對(duì)象军洼,這個(gè)時(shí)候需要的是被匹配的字符串
Matcher m = p.matcher("aaaaab");
// 調(diào)用匹配器對(duì)象的功能
boolean b = m.matches();
System.out.println(b);
//這個(gè)是判斷功能巩螃,但是如果做判斷,這樣做就有點(diǎn)麻煩了匕争,我們直接用字符串的方法做
String s = "aaaaab";
String regex = "a*b";
boolean bb = s.matches(regex);
System.out.println(bb);
}
}
package cn.yuan;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*
* 獲取功能:
* 獲取下面這個(gè)字符串中由三個(gè)字符組成的單詞
* 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?";
// 規(guī)則
String regex = "\\b\\w{3}\\b";
// 把規(guī)則編譯成模式對(duì)象
Pattern p = Pattern.compile(regex);
// 通過(guò)模式對(duì)象得到匹配器對(duì)象
Matcher m = p.matcher(s);
// 調(diào)用匹配器對(duì)象的功能
// 通過(guò)find方法就是查找有沒(méi)有滿足條件的子串
// public boolean find()
// boolean flag = m.find();
// System.out.println(flag);
// // 如何得到值呢?
// // public String group()
// String ss = m.group();
// System.out.println(ss);
//
// // 再來(lái)一次
// flag = m.find();
// System.out.println(flag);
// ss = m.group();
// System.out.println(ss);
while (m.find()) {
System.out.println(m.group());
}
// 注意:一定要先f(wàn)ind()避乏,然后才能group()
// IllegalStateException: No match found
// String ss = m.group();
// System.out.println(ss);
}
}