1. Date 日期
????????千萬別寫錯: Data 數(shù)據(jù)
????????主要是在 java.util.Date 包
????????另外還有一個 java.sql.Date 包,暫時不用。
???????? 初始化的時候:new Date() 可以獲取當(dāng)下的時間。
????????getTime() 獲取從 1970年1月1日到現(xiàn)在為止的毫秒值,類型 long
????????toString() 主要是將 Date 對象轉(zhuǎn)換為字符串。
2. DateFormat 日期格式轉(zhuǎn)換器
????????java.text.Format
????????constant 常量
????????final 最終值
3. SimpleDateFormat
???????? java.text.SimpleDateFormat
@Test
public void test(){
// dateFn();
// dateFormatFn();
// dateFormatFn2();
dateFormatFn3();
}
public void dateFormatFn3(){
String str = DateUtil.getDate(new Date());
System.out.println(str);
}
public void dateFormatFn2(){
// 1. 設(shè)置格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH點mm分ss秒");
// 2. 創(chuàng)建 Date
Date date = new Date();
// 3. 如果子類中沒有對應(yīng)可用的方法,可以直接去父類中查找使用
String dateStr = sdf.format(date);
System.out.println(dateStr);
}
public void dateFormatFn(){
// 1. 創(chuàng)建 Date
Date date = new Date();
// 2. 創(chuàng)建 DateFomat
// 不支持直接 new
// DateFormat df = new DateFormat();
DateFormat df = DateFormat.getInstance();
String dateStr = df.format(date);
// 18-9-12 上午11:26
System.out.println(dateStr);
}
public void dateFn(){
Date date = new Date();
// Wed Sep 12 11:15:20 CST 2018
System.out.println(date);
// 獲取從 1970-1-1 到現(xiàn)在的毫秒值
long date2 = date.getTime();
// 15367 2221 2946
System.out.println(date2);
}
}
封裝類:將日期轉(zhuǎn)換成字符串
public class DateUtil {
public static String getDate(Date date){
if(date != null){
// 1. 設(shè)置格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH點mm分ss秒");
// 2. 如果子類中沒有對應(yīng)可用的方法,可以直接去父類中查找使用
return sdf.format(date);
}
return null;
}
}
求自己活了多少天了
public class Demo2 {
// add?(int field, int amount):根據(jù)日歷規(guī)則,為已給定的日歷字段增加或者減少時間量。
// Adds or subtracts 增加或者減少 the specified 指定的 amount of time 時間量
// to the given calendar field, 已經(jīng)給出來的日歷字段
// based on the calendar's rules. 基于日歷規(guī)則
// 注意點中重點: 西方月份定義:0-11,星期的定義:日-六
// 1. 求自己已經(jīng)活了多少天了? XX 歲 XX 個月大 零 XX 天
// 2. 求距離 2020-8-8日還有多少天? 高考
@Test
public void test(){
// CalendarFn1();
// CalendarFn2();
// CalendarFn3();
tiFn();
}
// 1. 求自己已經(jīng)活了多少天了? XX 歲 XX 個月大 零 XX 天
public void tiFn(){
// 1. 獲取當(dāng)前的時間對應(yīng)的天數(shù)
Calendar currentDate = Calendar.getInstance();
Calendar birthDate = Calendar.getInstance();
// 2. 獲取出生時對應(yīng)的天數(shù)
// 設(shè)置出生日期:1999-8-8 2018-4-12 月份的公式:(12 + 當(dāng)前月份 - 出生時月份) yyyy-MM-dd
birthDate.set(Calendar.YEAR, 1999);
birthDate.set(Calendar.MONTH, 8);
birthDate.set(Calendar.DATE, 8);
// 3. 減法
// 年份
int cYear = currentDate.get(Calendar.YEAR);
int mYear = birthDate.get(Calendar.YEAR);
// 總年份
int totalYear = cYear - mYear;
// 月份
int cMonth = currentDate.get(Calendar.MONTH);
int mMonth = birthDate.get(Calendar.MONTH);
// 總月份
// int totalMonth = (12 + cMonth - mMonth) + (cYear - mYear)*12 - 12;
int totalMonth = (12 + cMonth - mMonth) + totalYear * 12 - 12;
// 獲取當(dāng)前的天數(shù)
int cDate = currentDate.get(Calendar.DATE);
int mDate = birthDate.get(Calendar.DATE);
// 總天數(shù)
// int totalDay = ((12 + cMonth - mMonth) + (cYear - mYear)*12 - 12) * 30;
int totalDay = totalMonth * 30;
System.out.println("到現(xiàn)在為止,活了"+ totalDay +"天了");
}
public void CalendarFn3(){
// 創(chuàng)建 calendar 實例
Calendar c = Calendar.getInstance();
// 2088-10-12
c.set(Calendar.YEAR, 2088);
c.set(Calendar.MONTH, 10);
c.set(Calendar.DATE, 12);
System.out.println(c);
}
public void CalendarFn2(){
// 創(chuàng)建 calendar 實例
Calendar c = Calendar.getInstance();
// 2018-9-12 --> 17
c.add(Calendar.DATE, 5);
// 單獨獲取 c 則是全部的數(shù)據(jù)
// 可以通過指定的 DAY_OF_MONTH 字段來獲取日的數(shù)據(jù)。
System.out.println(c.get(Calendar.DAY_OF_MONTH));
}
public void CalendarFn1(){
// 創(chuàng)建 calendar 實例
Calendar c = Calendar.getInstance();
// 獲取年份
int year = c.get(Calendar.YEAR);
System.out.println(year);
// 這個月的第幾周
System.out.println("這個月:" + c.get(Calendar.WEEK_OF_MONTH));
// 今年的第幾周
System.out.println("今年:" + c.get(Calendar.WEEK_OF_YEAR));
}
public void hello() throws Exception{
// 1. 設(shè)置一個日期格式
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
// 2. 設(shè)置出生日期和當(dāng)前日期
Date firstDate = format.parse("1999-8-8");
Date endDate = format.parse("2018-9-12");
// 3. 初始化 Calendar 對象
Calendar calendar = Calendar.getInstance();
// 4. 將出生日期加入到 Calendar 實例中
calendar.setTime(firstDate);
// 5. 設(shè)置旗標(biāo)值,用于循環(huán)迭加
int cnt = 0;
// 6. compareTo() 接收兩個 Calendar 對象值
// 將傳入的時間,直接轉(zhuǎn)換成毫秒值(從 1970-1-1開始) 然后可以進行比較
// 如果兩個值相減時得到 0 則時同一時間,否則就是兩者之差
// 如果直接采用這種方式,性能非常不好
while(calendar.getTime().compareTo(endDate) != 0) {
// 每次給開始時間去加1天(下面的1單位是天),直到天數(shù)加上來等于給定的結(jié)束時間為止,
// 循環(huán)將不再成立,結(jié)束循環(huán)
calendar.add(Calendar.DATE, 1);
cnt++;
}
System.out.println("您活了:" + cnt + "天");
}
}
4.正則表達式
????????下面出現(xiàn)的 X 代表的是隨便一個字符。
????????1. X:要求必須匹配指定的字符。 a --> "a"
????????2. \:代表 \ 斜杠。
????????3. \t:相當(dāng)于 tab 鍵,制表符。
????????4. \n:相當(dāng)于回車鍵,換行。
????????5. \r:相當(dāng)于回車鍵,換行。
????????6. [abc]:接收的內(nèi)容,只能是指定字符中之一。
????????7. [^abc]:接收的內(nèi)容,是除了指定的其他所有值。
????????8. [a-zA-Z]:指的是全部大小寫字母。
????????9. [0-9]:指所有的數(shù)字。
????????10.[a-zA-Z0-9]:指的是所有的字母和數(shù)字。
????????1. 用小實心圓點 . 來表示所有的字符。 . 但書寫的時候需要 \.
????????12. \d:指 0-9 的數(shù)字。
????????13. \w:指的是 [a-zA-Z0-9_]中的值。
????????14. ^ 開頭,
????????15. \b:單詞邊界。 \b[abc]\b 指的是 a、b、c 三個字母左右兩邊的值都是 [a-zA-Z0-9] 范圍的。
????????16. X?:出現(xiàn)一次或者一次都沒有。
????????17. X*:出現(xiàn)零次或者多次。
????????18. X+:出現(xiàn)一次或者多次。
????????19. X{n}:恰好出現(xiàn) n 次。
???????? 20. X{n,}:至少出現(xiàn) n 次。
???????? 21. X{n, m}:至少出現(xiàn) n 次,但不超過 m 次。
驗證郵箱1:
public void email(){
// String email = "zsfz_053@126.com";
// String regrex = "[a-zA-Z_0-9]+@[a-zA-Z0-9]+(\\.[a-zA-Z_0-9]+)";
String email = "hello@sina.com.cn";
String regrex = "\\w+@\\w+(\\.\\w+)+";
System.out.println(email.matches(regrex));
}
驗證郵箱2:
public void regrex(){
String email = "12345-6@qq.com";
String regrex = "[\\w-_]{6,18}@[\\w]*\\.[comn]*";
boolean b = email.matches(regrex);
System.out.println(b);
};
5.包裝類
????????包裝類
????????我們以前用的 int、char、double、float、boolean、short、byte 等都是基本數(shù)據(jù)類型。
????????在 Java 開發(fā)中盡量少用基本數(shù)據(jù)類型,多用包裝類型。
????????針對基本數(shù)據(jù)類型進行升級,Java 提供多個包裝類:
????????Integer、Character、Double、Float、Boolean、Short、Byte,引用數(shù)據(jù)類型,有內(nèi)存地址。
????????包裝類型,可以讓開發(fā)者以面向?qū)ο蟮乃季S來操作,而且提供了很多的方法來實現(xiàn)功能。
????????轉(zhuǎn)換方法:
????????1. compareTo?(Integer anotherInteger):將兩個 Integer 值來做運算(減法)
????????2. parseInt?(String s):將 String 轉(zhuǎn) int,字符串中只能是數(shù)值才能轉(zhuǎn)。
????????3. intValue():將 Integer 以 int 類型返回。
????????4. valueOf(int):將 int 轉(zhuǎn)成 Integer。
????????5. valueOf(String):將 String 轉(zhuǎn)成 Integer。
????????比較:
????????max?(int a, int b):返回兩者中最大值。
????????min?(int a, int b):返回兩者中最小值。
6.math()方法
public class Demo {
@Test
public void test(){
// 1. 四舍五入
System.out.println(Math.round(3.17));
// 2. 向上取整
System.out.println(Math.ceil(3.24));
// 3. 向下取整
System.out.println(Math.floor(3.84));
// 4. 最大值
System.out.println(Math.max(5, 6));
// 5. 最小值
System.out.println(Math.min(5, 6));
// 6. 絕對值
System.out.println(Math.abs(-7));
System.out.println(Math.abs(-7.34));
// 7. xx的多少次冪
System.out.println(Math.pow(5, 6));
// 8. 隨機數(shù):0.0-1.0
System.out.println(Math.random());
}
}
7.Arrays數(shù)組
public class Demo {
@Test
public void test() {
// 找到班里考試分?jǐn)?shù)最低的三個
int[] aa = {73, 97, 24, 33, 65, 45, 99};
// 排序
Arrays.sort(aa);
// 新建一個數(shù)組存儲值
int[] i = new int[3];
for (int j = 0; j < i.length; j++) {
i[j] = aa[j];
}
System.out.println(Arrays.toString(i));
}
public void ArraysFn() {
// sort:排序(小到大)
int[] aa = { 23, 33, 5, 12, 88, 34, 25 };
// Arrays.sort(aa);
for (int i : aa) {
System.out.print(i + ",");
}
// toString() 將數(shù)據(jù)以字符串返回
String str = Arrays.toString(aa);
System.out.println(str);
// 二分法: return index of the search key, 返回要找的內(nèi)容對應(yīng)下標(biāo)值
int index = Arrays.binarySearch(aa, 12);
System.out.println(index);
}
}
8.BigDecimal
public class BigDecimalTest {
@Test
public void test(){
System.out.println(0.09 + 0.02222222); // 0.11
System.out.println(1.0 - 0.44); // 0.56
System.out.println(1.304 * 100); // 130.4
System.out.println(1.304 / 100); // 0.013040000000000001
BigDecimal a = new BigDecimal(1.0);
BigDecimal b = new BigDecimal(0.44);
// BigDecimal c = a.add(b);
BigDecimal c = a.subtract(b);
System.out.println(1.0 + 0.44);
System.out.println(c);
}
}
9.list集合
public class Demo {
// List 集合
// 相當(dāng)于一個容器,跟數(shù)組是類似的,用于存儲數(shù)據(jù)。
// 數(shù)組不能動態(tài)變化長度,來添加數(shù)據(jù),但集合可以。
// List 接口
// 1. 存儲的值是有序排列的。
// 2. 值是允許重復(fù)的。
// 3. 可以通過索引尋找。
// 需要掌握的實現(xiàn)類:ArrayList、LinkedList
// 增加數(shù)據(jù):
// add() 在尾部追加
// add(index, element) 指定位置加
// 刪除刪除
// remove?(index) 通過下標(biāo)值來刪除
// remove(Object) 直接刪掉指定對象
// 替換數(shù)據(jù)
// set?(index, element) 在指定的位置設(shè)置內(nèi)容
// 查找數(shù)據(jù)
// get?(index) 根據(jù)下標(biāo)值獲取對象
@Test
public void test() {
listIteratorFn();
}
public void listIteratorFn(){
// 創(chuàng)建一個 ArrayList 對象
ArrayList<String> arrList = new ArrayList<>();
// 添加值
arrList.add("cuihua");
arrList.add("chunhua");
arrList.add("hehua");
// 獲取 ListIterator 迭加器,訪問數(shù)據(jù)。
ListIterator<String> itStr = arrList.listIterator();
// hasNext() 判斷下面是否有數(shù)據(jù),指針還沒動的時候
while (itStr.hasNext()) {
// 就讓指針移動,然后獲取對應(yīng)的值到 迭加器 中。
String aa = itStr.next();
// 看是否最后一個值,如果是的話,我想追加一個新值
if ("chunhua".equals(aa)) {
// 將一個新值,加到 疊加器 中
itStr.add("hello");
}
}
System.out.println(arrList.toString());
}
}