JDK7前時間相關類:
時間的相關知識:
Data時間類:
?
//1.創建對象表示一個時間
Date d1 = new Date();
//System.out.println(d1);//2.創建對象表示一個指定的時間
Date d2 = new Date(0L);
System.out.println(d2);//3.setTime修改時間
//1000毫秒=1秒
d2.setTime(1000L);
System.out.println(d2);//4.getTime獲取當前時間的毫秒值
long time = d2.getTime();
System.out.println(time);
?
SimpleDateFormat 類作用:
?
SimpleDateFormat 類:
?
//1.利用空參構造創建SimpleDateFormat對象,默認格式
SimpleDateFormat sdf1 = new SimpleDateFormat();
Date d1 = new Date(OL);
String str1 = sdf1.format(d1);
System.out.println(str1);//1970/1/1上午8:00//2.利用帶參構造創建SimpleDateFormat對象,指定格式
SimpleDateFormat sdf2 = new SimpleDateFormat(pattern:"yyyy年MM月dd日 HH:mm:ss EE");
String str2 = sdf2.format(d1);
System.out.println(str2);//1970年01月e1日08:00:00//課堂練習:yyyy年MM月dd日時:分:秒星期E
//1.定義一個字符串表示時間
String str = "2023-11-11 11:11:11";//2.利用空參構造創建SimpleDateFormat對象
//細節:
//創建對象的格式要跟字符串的格式完全一致
SimpleDateFormat sdf = new SimpleDateFormat(pattern:"yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(str);//3.打印結果
System.out.println(date.getTime());//1699672271000
?Calendar概述:
?Calendar常用方法:
?細節1:Calendar是一個抽象類,不能直接new,而是通過一個靜態方法獲取到子類對象
?底層原理:會根據系統的不同時區來獲取不同的日歷對象. 把會把時間中的紀元,年,月,日,時,分,秒,星期,等等的都放到一個數組當中.
?細節2:月份:范圍0~11 如果獲取出來的是0.那么實際上是1月。
? 星期:在老外的眼里,星期日是一周中的第一天
1(星期日)? ?2(星期一)?3(星期二)4(星期三)5(星期四)6(星期五)7(星期六)
?JDK8:
ZoneID時區:
//1.獲取所有的時區名稱
Set<String> zoneIds = ZoneId.getAvailableZoneIds();
System.out.println(zoneIds.size());//600
System.out.println(zoneIds);// Asia/Shanghai//2.獲取當前系統的默認時區
ZoneId zoneId = ZoneId.systemDefault();
System.out.println(zoneId);//Asia/Shanghai//3.獲取指定的時區
ZoneId zoneId1 = ZoneId.of("Asia/Pontianak");
System.out.println(zoneId1);//Asia/Pontianak
Instant時間戳:
//1.獲取當前時間的Instant對象(標準時間)
//Instant now = Instant.now();
//System.out.println(now);//2.根據(秒/毫秒/納秒)獲取Instant對象
Instant instant1 = Instant.ofEpochMilli(0L);
System.out.println(instant1);//1970-01-01T00:00:00zInstant instant2 = Instant.ofEpochSecond(1L);
System.out.println(instant2);//1970-01-01T00:00:01ZInstant instant3 = Instant.ofEpochSecond(epochSecond:1L,nanoAdjustment:1eeeeeeeeeL);
System.out.println(instant3);//1970-01-01T00:00:02z//3.指定時區
ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
System.out.println(time);//4.isXxx判斷
Instant instant4 = Instant.ofEpochMilli(0L);
Instant instant5 = Instant.ofEpochMilli(1000L);//5.用于時間的判斷
//isBefore:判斷調用者代表的時間是否在參數表示時間的前面
boolean result1 = instant4.isBefore(instant5);
System.out.println(result1);//true//isAfter:判斷調用者代表的時間是否在參數表示時間的后面
boolean result2 = instant4.isAfter(instant5);
System.out.println(result2);//false//6.Instant minusXxx(long millisToSubtract)減少時間系列的方法
Instant instant6 = Instant.ofEpochMilli(3000L);
System.out.println(instant6);//1970-01-01T00:00:03Z
Instant instant7 = instant6.minusSeconds(1);
System.out.println(instant7);//1970-01-01T00:00:02z
ZoneDateTime帶時區的時間:
//1.獲取當前時間對象(帶時區)
ZonedDateTime now = ZonedDateTime.now();
System.out.println(now);//2.獲取指定的時間對象(帶時區)
//年月日時分秒納秒方式指定
ZonedDateTime time1 = ZonedDateTime.of(year:2023, month: 10, dayOfMonth:1,
hour: 11,minute:12, second: 12, nanoOfSecond: 0 ,ZoneId.of("Asia/Shanghai");
System.out.println(time1);//通過Instant+時區的方式指定獲取時間對象
Instant instant = Instant.ofEpochMilli(OL);
ZoneId zoneId = ZoneId.of("Asia/Shanghai");
ZonedDateTime time2 = ZonedDateTime.ofInstant(instant, zoneId);
System.out.println(time2);//3.withXxx修改時間系列的方法
ZonedDateTime time3 = time2.withYear(2000);
System.out.println(time3);//4.減少時間
ZonedDateTime time4 = time3.minusYears(1);
System.out.println(time4);//5.增加時間
ZonedDateTime time5 = time4.plusYears(1);
System.out.println(time5);
? 細節:JDK8新增的時間對象都是不可變的.? 如果我們修改了,減少了,增加了時間.? 那么調用者是不會發生改變的,產生一個新的時間.
DateTimeFormatter用于時間的格式化和解析:
//獲取時間對象
ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
//解析/格式化器
DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss EE a");
//格式化
System.out.println(dtf1.format(time));
LocalDate、LocalTime、LocalDateTime:
//1.獲取當前時間的日歷對象(包含年月日)
LocalDate nowDate = LocalDate.now();
System.out.println("今天的日期:"+nowDate);//2.獲取指定的時間的日歷對象
LocalDate 1dDate = LocalDate.of(year: 2023,month:1,dayOfMonth:1);
System.out.println("指定日期:"+1dDate);
System.out.println("=========");//3.get系列方法獲取日歷中的每一個屬性值
//獲取年
int year = ldDate.getYear();
System.out.println("year: " + year);//獲取月
//方式一:
Month m = ldDate.getMonth();
System.out.println(m);
System.out.println(m.getValue());
//方式二:
int month = ldDate.getMonthValue();
System.out.println("month: " + month);//獲取日
int day = 1dDate.getDayOfMonth();
System.out.println("day: " + day);//獲取一年的第幾天
int dayOfYear = ldDate.getDayOfYear();
System.out.println("dayOfYear:" + dayOfYear);///獲取星期
DayOfWeek dayOfWeek = 1dDate.getDayOfWeek();
System.out.println(dayOfWeek);
System.out.println(dayOfWeek.getValue());//is開頭的方法表示判斷
System.out.println(ldDate.isBefore(1dDate));
System.out.println(1dDate.isAfter(ldDate));//with開頭的方法表示修改,只能修改年月日
LocalDate withLocalDate = ldDate.withYear(2000);
System.out.println(withLocalDate);//minus開頭的方法表示減少,只能減少年月日
LocalDate minusLocalDate = ldDate.minusYears(1);
System.out.println(minusLocalDate);//plus開頭的方法表示增加,只能增加年月日
LocalDate plusLocalDate = 1dDate.plusDays(1);
System.out.println(plusLocalDate);
Duration、Period、ChronoUnit:
//當前本地年月日
LocalDate today = LocalDate.now();
System.out.println(today);//生日的年月日
LocalDate birthDate = LocalDate.of(year:2000,month:1,dayOfMonth:1);
System.out.println(birthDate);
Periodperiod=Period.between(birthDate,today);//第二個參數減第一個參數
System.out.println("相差的時間間隔對象:"+ period);//P22Y6M17D
System.out.println(period.getYears());
System.out.println(period.getMonths());
System.out.println(period.getDays());
System.out.println(period.toTotalMonths());
//本地日期時間對象。
LocalDateTime today = LocalDateTime.now();
System.out.println(today);//出生的日期時間對象
LocalDateTime birthDate= LocalDateTime.of(year:2000,month:1,dayOfMonth:1,hour:0, minute:00,second:00);
System.out.println(birthDate);
Duration duratior = Duration.between(birthDate,today);//第二個參數減第一個參數
System.out.println("相差的時間間隔對象:" + duration);System.out.println(duration.toDays());//兩個時間差的天數
System.out.println(duration.toHours());//兩個時間差的小時數
System.out.println(duration.toMinutes());//兩個時間差的分鐘數
System.out.println(duration.toMillis());//兩個時間差的毫秒數
System.out.println(duration.toNanos());//兩個時間差的納秒數
//當前時間
LocalDateTime tōday = LocalDateTime.now();
System.out.println(today);//生日時間
LocalDateTime birthDate = LocalDateTime.of(year:2eee,month:1,dayOfMonth:1,
hour: 0,minute:0,second: 0);
System.out.println(birthDate);System.out.printIn("相差的年數:"+ ChronoUnit.YEARS.between(birthDate,today));
System.out.printIn("相差的月數: "+ ChronoUnit.MONTHS.between(birthDate,today));
System.out.printIn("相差的周數: "+ ChronoUnit.WEEKS.between(birthDate,today));
System.out.println("相差的天數: "+ ChronoUnit.DAYs.between(birthDate,today));
System.out.println("相差的時數:"+ ChronoUnit.HOuRs.between(birthDate,today));
System.out.printIn("相差的分數:"+ ChronoUnit.MINUTES.between(birthDate,today));
System.out.println("相差的秒數: "+ ChronoUnit.SECoNDS.between(birthDate,today));
System.out.printIn("相差的毫秒數:"+ ChronoUnit.MILLIS.between(birthDate,today));
System.out.printIn("相差的微秒數: "+ ChronoUnit.MICRos.between(birthDate,today));
System.out.println("相差的納秒數:"+ ChronoUnit.NANOs.between(birthDate,today));
System.out.println("相差的半天數: "+ ChronoUnit.HALF_DAYS.between(birthDate,today));
System.out.println("相差的十年數:"+ ChronoUnit.DECADES.between(birthDate,today));
System.out.println("相差的世紀(百年)數:"+ChronoUnit.CENTURIES.between(birthDate,today));
System.out.println("相差的千年數:"+ ChronoUnit.MILLENNIA.between(birthDate,today));
System.out.println("相差的紀元數: "+ ChronoUnit.ERAS.between(birthDate,today));
包裝類:
//1.利用構造方法獲取Integer的對象(JDK5以前的方式)
Integer i1 =r
new Integer(value:1);
Integer i2 = new Integer(s:"1");
System.out.println(i1);
System.out.println(i2);//2.利用靜態方法獲取Integer的對象(JDK5以前的方式)
Integer i3 = Integer.valueOf(123);
Integer i4 = Integer.valueOf("123");
Integer i5 = Integer.valueOf(s:"123",radix:8);System.out.println(i3);
System.out.println(i4);
System.out.println(i5);//83
//3.這兩種方式獲取對象的區別(掌握)
Integer i6 =Integer.valueOf(127);
Integer i7 =Integer.valueOf(127);
System.out.println(i6==i7);//falseInteger i8 = Integer.valueOf(128)
Integer i9 = Integer.valueof(128);
System.out.println(i8==i9);Integer i10 = new Integer(value:127);
Integer i11 = new Integer(value:127);
System.out.println(i10==i11);Integer i12 = new Integer(value:128)
Integer i13 = new Integer(value:128);
System.out.println(i12==i13);
? 底層原理:因為在實際開發中,-128~127之間的數據,用的比較多.? 如果每次使用都是new對象,那么太浪費內存了.? 所以,提前把這個范圍之內的每一個數據都創建好對象.? 如果要用到了不會創建新的,而是返回已經創建好的對象.
//1.把整數轉成二進制,十六進制
String str1 = Integer.toBinaryString(i:100);
System.out.println(str1);//1100100//2.把整數轉成八進制
String str2 = Integer.toOctalString(i:100);
System.out.println(str2);//144//3.把整數轉成十六進制
String str3 = Integer.toHexString(i:100);
System.out.println(str3);//64//4.將字符串類型的整數轉成int類型的整數
//強類型語言:每種數據在java中都有各自的數據類型
//在計算的時候,如果不是同一種數據類型,是無法直接計算的。
int i = Integer.parseInt(s:"123");
System.out.println(i);
System.out.println(i + 1);//124
?細節1:在類型轉換的時候,括號中的參數只能是數字不能是其他,否則代碼會報錯
?細節2:8種包裝類當中,除了character都有對應的parseXxx的方法,進行類型轉換
//鍵盤錄入
Scanner sc = new Scanner(System.in);
System.out.println("請輸入一個字符串");
String str = sc.next();
System.out.println(str);String line = sc.nextLine();
System.out.println(line);double V = Double.parseDouble(1ine);
System.out.println(v + 1);
?弊端:當我們在使用next,nextInt,nextDouble在接收數據的時候,遇到空格,回車,制表符的時候就停止了 ,鍵盤錄入的是123123那么此時只能接收到空格前面的數據 , 我想要的是接收一整行數據.
?約定:以后我們如果想要鍵盤錄入,不管什么類型,統一使用nextLine , 特點遇到回車才停止.
Arrays:
//toString:將數組變成字符串
int[] arr = {1,2, 3, 4, 5,6, 7,8,9,10};
System.out.println(Arrays.toString(arr));//[1,2,3,4,5,6,7,8,9,10]//binarySearch:二分查找法查找元素
//細節1:二分查找的前提:數組中的元素必須是有序,數組中的元素必須是升序的
//細節2:如果要查找的元素是存在的,那么返回的是真實的索引
//但是,如果要查找的元素是不存在的,返回的是插入點-1
//疑問:為什么要減1呢?
//解釋:如果此時,我現在要查找數字0,那么如果返回的值是-插入點,就會出現問題了。
//如果要查找數字0,此時θ是不存在的,但是按照上面的規則-插入點,應該就是-0
//為了避免這樣的情況,Java在這個基礎上又減一。
System.out.println(Arrays.binarySearch(arr,key:10));//9
System.out.println(Arrays.binarySearch(arr,key:2));//1
System.out.printIn(Arrays.binarySearch(arr,key:20));//-11//copyof:拷貝數組
//參數一:老數組
//參數二:新數組的長度
//方法的底層會根據第二個參數來創建新的數組
//如果新數組的長度是小于老數組的長度,會部分拷貝
//如果新數組的長度是等于老數組的長度,會完全拷貝
//如果新數組的長度是大于老數組的長度,會補上默認初始值
int[] newArr1 = Arrays.copyof(arr,newLength:20);
System.out.println(Arrays.toString(newArr1));//[1,2, 3,4, 5,6, 7,8, 9,10]//copyOfRange:拷貝數組(指定范圍)
//細節:包頭不包尾,包左不包右
int[] newArr2 = Arrays.copyOfRange(arr,from:0,to:9);
System.out.println(Arrays.toString(newArr2));//[1,2,3,4,5,6,7,8,9]//fi1l:填充數組
Arrays.fill(arr,val:100);
System.out.println(Arrays.toString(arr));//sort:排序。默認情況下,給基本數據類型進行升序排列。底層使用的是快速排序。
int[] arr2 = {10, 2, 3, 5,6,1,7,8, 4,9};
Arrays.sort(arr2);
System.out.println(Arrays.toString(arr2));
? 底層原理:利用插入排序+二分查找的方式進行排序的。默認把θ索引的數據當做是有序的序列,1索引到最后認為是無序的序列。遍歷無序的序列得到里面的每一個元素,假設當前遍歷得到的元素是A元素? 把A往有序序列中進行插入,在插入的時候,是利用二分查找確定A元素的插入點。拿著A元素,跟插入點的元素進行比較,比較的規則就是compare方法的方法體
? 如果方法的返回值是負數,拿著A繼續跟前面的數據進行比較
? 如果方法的返回值是正數,拿著A繼續跟后面的數據進行比較
? 如果方法的返回值懸0,也拿著A跟后面的數據進行比較
? 直到能確定A的最終位置為止。