之前有粉絲后臺(tái)跟我說爸舒,作為一個(gè)初學(xué)者蟋字,真的是不清楚該如何去進(jìn)行學(xué)習(xí),直接上ssm框架也看不明白扭勉,那我作為這么一個(gè)寵粉的人鹊奖,怎么可能讓粉絲有這樣的顧慮啊,今天真的是基礎(chǔ)到極點(diǎn)了涂炎,分享我這邊的相應(yīng)的一些代碼實(shí)例忠聚,因?yàn)樵诖a的備注中已經(jīng)寫的很清楚了,所以基本不會(huì)再通過文字進(jìn)行講解
文章首發(fā)公眾號(hào):Java架構(gòu)師聯(lián)盟唱捣,每日更新技術(shù)好文两蟀,后面也會(huì)開源我的代碼倉(cāng)庫,畢竟現(xiàn)在還比較單薄
注釋+源碼+結(jié)果震缭,這邊應(yīng)該展示的很清楚赂毯,也比較好理解,不過拣宰,一定要自己去實(shí)踐一下党涕,不實(shí)踐再簡(jiǎn)單的技術(shù)理解起來也不容易
而向多線程什么的學(xué)習(xí)實(shí)例膛堤,我這邊之前的文章也整理過,大家可以去查看重贺,每一個(gè)都帶著相應(yīng)的源碼展示
好了骑祟,話不多說回懦,看正題
Java基礎(chǔ)之:OOP——抽象類
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n9" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">package com.biws.testabstract;
?
/**
- @author :biws
- @date :Created in 2020/12/22 21:14
- @description:抽象類測(cè)試
*/
public class Abstract_Test {
?
public static void main(String[] args) {
?
Cat cat = new Cat("小花貓");
cat.eat();
}
?
}
?
?
abstract class Animal { //抽象類
private String name;
?
public Animal(String name) {
super();
this.name = name;
}
?
public String getName() {
return name;
}
?
public void setName(String name) {
this.name = name;
}
?
//eat , 抽象方法
public abstract void eat();
}
?
//解讀
//1. 當(dāng)一個(gè)類繼承了抽象類,就要把抽象類的所有抽象方法實(shí)現(xiàn)
//2. 所謂方法實(shí)現(xiàn),指的是 把方法體寫出, 方法體是空次企,也可以.
class Cat extends Animal {
public Cat(String name) {
super(name);
// TODO Auto-generated constructor stub
}
?
public void eat() {
System.out.println(getName() + " 愛吃 <?)))><<");
}
}</pre>
然后呢怯晕,我們看一下多態(tài)的實(shí)現(xiàn)
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n11" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">package com.biws.testabstract;
?
/**
- @author :biws
- @date :Created in 2020/12/22 21:17
- @description:多態(tài)在抽象類中的實(shí)現(xiàn)
*/
public class AbstractPolArray {
?
public static void main(String[] args) {
//抽象類不可以實(shí)例化,但可以使用多態(tài)數(shù)組
Animal1[] animal1 = new Animal1[2];
?
animal1[0] = new Dog1("小黑狗");
animal1[1] = new Cat1("小花貓");
?
//多態(tài)數(shù)組的使用
for (int i = 0; i < animal1.length; i++) {
show(animal1[i]);
}
}
?
//這里不用擔(dān)心會(huì)傳入一個(gè)Animal類型的實(shí)例缸棵,因?yàn)锳nimal不能實(shí)例化
//編譯器不會(huì)通過舟茶,所以只會(huì)傳入Animal的子類實(shí)例
public static void show(Animal1 a) {
a.eat(); //多態(tài)的使用
?
if(a instanceof Dog1) {
((Dog1)a).watch();
}else if(a instanceof Cat1) {
((Cat1)a).catchMouse();
}
}
}
?
abstract class Animal1{
private String name;
?
public Animal1(String name) {
super();
this.name = name;
}
?
public String getName() {
return name;
}
?
public void setName(String name) {
this.name = name;
}
?
//動(dòng)物都有eat的動(dòng)作,但我們并不知道每一個(gè)動(dòng)物具體怎么樣eat
//所以這里通過抽象提供了eat方法堵第,需要子類來實(shí)現(xiàn)
public abstract void eat();
}
?
class Dog1 extends Animal1{
public Dog1(String name) {
super(name);
}
?
@Override
public void eat() {
System.out.println(getName() + "啃骨頭......");
}
?
public void watch() {
System.out.println(getName() + "守家.....");
}
}
?
class Cat1 extends Animal1{
public Cat1(String name) {
super(name);
}
?
@Override
public void eat() {
System.out.println(getName() + "吃魚......");
}
?
public void catchMouse(){
System.out.println(getName() + "抓老鼠.....");
}
}</pre>
最后舉個(gè)小例子總結(jié)一下
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n13" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">package com.biws.testabstract;
?
/**
- @author :biws
- @date :Created in 2020/12/22 21:24
- @description:模板設(shè)計(jì)模式
*/
public class Abstract_Template {
public static void main(String[] args) {
Template sub = new Sub();
sub.caleTimes(); //實(shí)際是調(diào)用了Template中的caleTimes方法
?
Template subStringB = new SubStringB();
subStringB.caleTimes();
?
//這里可以看到 StringBuffer在拼接字符串時(shí)吧凉,遠(yuǎn)遠(yuǎn)優(yōu)于String拼接的效率
}
}
?
abstract class Template{ //抽象類
public abstract void code(); //抽象方法
public void caleTimes(){ // 統(tǒng)計(jì)耗時(shí)多久是確定
//統(tǒng)計(jì)當(dāng)前時(shí)間距離 1970-1-1 0:0:0 的時(shí)間差,單位ms
long start = System.currentTimeMillis();
code(); //這里的code在調(diào)用時(shí)踏志,就是指向子類中已經(jīng)重寫實(shí)現(xiàn)了的code
long end = System.currentTimeMillis();
System.out.println("耗時(shí):"+(end-start));
}
}
?
class Sub extends Template{
?
@Override
public void code() {
String x = "";
for(int i = 0;i < 10000 ; i++) { //拼接1W個(gè)hello 看處理時(shí)間
x += "hello" + i;
}
}
}
?
class SubStringB extends Template{
@Override
public void code() {
StringBuffer stringBuffer = new StringBuffer();
for(int i = 0;i < 10000 ; i++) { //拼接1W個(gè)hello 看處理時(shí)間
stringBuffer.append("hello" + i);
}
}
}</pre>
Java基礎(chǔ)之:StringBuffer與StringBuilder
簡(jiǎn)單直白點(diǎn)阀捅,直接一套代碼走起
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n16" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">package com.biws.string;
?
/**
- @author :biws
- @date :Created in 2020/12/22 21:27
- @description:String_StringBuffer_StringBuilder對(duì)比
*/
public class String_StringBuffer_StringBuilder {
public static void main(String[] args) {
// TODO Auto-generated method stub
?
String text = ""; //字符串
long startTime = 0L;
long endTime = 0L;
StringBuffer buffer = new StringBuffer("");//StringBuffer
StringBuilder builder = new StringBuilder("");//StringBuilder
?
startTime = System.currentTimeMillis();
for (int i = 0; i < 80000; i++) {
buffer.append(String.valueOf(i));
}
endTime = System.currentTimeMillis();
System.out.println("StringBuffer的執(zhí)行時(shí)間:" + (endTime - startTime));
?
?
?
startTime = System.currentTimeMillis();
for (int i = 0; i < 80000; i++) {
builder.append(String.valueOf(i));
}
endTime = System.currentTimeMillis();
System.out.println("StringBuilder的執(zhí)行時(shí)間:" + (endTime - startTime));
?
?
?
startTime = System.currentTimeMillis();
for (int i = 0; i < 80000; i++) {
text = text + i;
}
endTime = System.currentTimeMillis();
System.out.println("String的執(zhí)行時(shí)間:" + (endTime - startTime));
?
}
}</pre>
查看一下執(zhí)行結(jié)果
Java基礎(chǔ)之:Math & Arrays
Math
簡(jiǎn)單粗暴饲鄙,直接進(jìn)行代碼展示
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n23" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">package com.biws.testmath;
?
/**
- @author :biws
- @date :Created in 2020/12/22 21:42
- @description:測(cè)試math類
/
public class ClassTest {
public static void main(String[] args) {
//1.abs 絕對(duì)值
int abs = Math.abs(9);
System.out.println(abs);
//2.pow 求冪
double pow = Math.pow(-3.5, 4);
System.out.println(pow);
//3.ceil 向上取整,返回>=該參數(shù)的最小整數(shù);
double ceil = Math.ceil(-3.0001);
System.out.println(ceil);
//4.floor 向下取整,返回<=該參數(shù)的最大整數(shù)
double floor = Math.floor(-4.999);
System.out.println(floor);
//5.round 四舍五入 Math.floor(該參數(shù)+0.5)
long round = Math.round(-5.001);
System.out.println(round);
//6.sqrt 求開方
double sqrt = Math.sqrt(-9.0);
System.out.println(sqrt);
//7.random 返回隨機(jī)數(shù)【0——1)
//[a-b]:int num = (int)(Math.random()(b-a+1)+a)
double random = Math.random();
System.out.println(random);
?
//小技巧:獲取一個(gè) a-b 之間的一個(gè)隨機(jī)整數(shù)
int a = (int)(Math.random()(15-7+1)+7);
System.out.println(a);
/ - 理解:
- 1.Math.random() 是 [0圆雁,1)的隨機(jī)數(shù)
- 2.(Math.random()*(15-7+1) 就是[0忍级,9)
- 3.Math.random()*(15-7+1)+7 就是[7,16)
- 4.(int)取整就是 [7,15] 伪朽,即[a,b]之間的隨機(jī)整數(shù)
*/
}
}</pre>
執(zhí)行結(jié)果
Arrays
正常的執(zhí)行
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n29" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">package com.biws.testarray;
?
import java.util.Arrays;
import java.util.Comparator;
?
/**
- @author :biws
- @date :Created in 2020/12/22 21:44
- @description:Array中常用排序方法
*/
public class TestArray1 {
public static void main(String[] args) {
Integer[] arr = { 25, 35, 11, 32, 98, 22 };
// Arrays.sort(arr); //默認(rèn)小到大排序
System.out.println(Arrays.toString(arr));
?
// 使用匿名內(nèi)部類重寫compare方法烈涮,實(shí)現(xiàn)從大到小排序
Arrays.sort(arr, new Comparator<Integer>() {
?
@Override
public int compare(Integer o1, Integer o2) {
if (o1 > o2) {
return -1;
} else if (o1 < o2) {
return 1;
} else {
return 0;
}
}
});
System.out.println(Arrays.toString(arr));
}
}</pre>
結(jié)果查看
重寫之后的執(zhí)行結(jié)果
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n34" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">package com.biws.testarray;
?
import java.util.Arrays;
import java.util.Comparator;
?
/**
- @author :biws
- @date :Created in 2020/12/22 21:46
- @description:重寫array中的排序方法
*/
public class overwriteArraySort {
@SuppressWarnings("unchecked")
public static void sort(Integer[] arr, Comparator c) {
Integer temp = 0;// 自動(dòng)裝箱
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (c.compare(arr[j] , arr[j + 1]) > 0) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
?
public static void main(String[] args) {
Integer[] arr = { 35, 25, 11, 32, 98, 22 };
// MyArrays.sort(arr);//不添加 Comparator接口對(duì)象為參數(shù)谴麦,就是簡(jiǎn)單的冒泡排序方法
System.out.println(Arrays.toString(arr));
//添加匿名內(nèi)部類對(duì)象為參數(shù),就可以改變排序方式吹泡。用此方法可以很靈活的使用排序酪术。
overwriteArraySort.sort(arr, new Comparator<Integer>() {
?
@Override
public int compare(Integer o1, Integer o2) {
//通過返回值的正負(fù)來控制器瘪,升序還是降序
//只有當(dāng)返回值為1時(shí),才會(huì)發(fā)生交換绘雁。例如這里橡疼,o1 < o2時(shí)返回1 ,進(jìn)行交換
//也就是需要前面的數(shù)庐舟,比后面的數(shù)大欣除,即降序
if (o1 < o2) {
return -1;
} else if (o1 > o2) {
return 1;
} else {
return 0;
}
}
});
System.out.println(Arrays.toString(arr));
}
?
}</pre>
結(jié)果
Java基礎(chǔ)之:大數(shù)
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n39" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">package com.biws.bignum;
?
import java.math.BigDecimal;
import java.math.BigInteger;
?
/**
- @author :biws
- @date :Created in 2020/12/22 22:03
- @description:
*/
public class test {
public static void main(String[] args) {
BigInteger i1 = new BigInteger("1234567890");
BigInteger i2 = new BigInteger("200");
// 2.調(diào)用常見的運(yùn)算方法
// System.out.println(b1+b2); 不能使用 這樣的 + 方法運(yùn)行
// 并且历帚,add 這些方法只能是大數(shù)于大數(shù)相加滔岳,BigInteger.add(BigInteger);
System.out.println(i1.add(i2));// 加
System.out.println(i1.subtract(i2));// 減
System.out.println(i1.multiply(i2));// 乘
System.out.println(i1.divide(i2));// 除
?
BigDecimal b1 = new BigDecimal("1234567890.567");
BigDecimal b2 = new BigDecimal("123");
// 2.調(diào)用常見的運(yùn)算方法
// System.out.println(b1+b2); 不能使用 + 號(hào)運(yùn)算..
// 并且,add 這些方法只能是大數(shù)于大數(shù)相加挽牢,BigDecimal.add(BigDecimal);
System.out.println(b1.add(b2));// 加
System.out.println(b1.subtract(b2));// 減
System.out.println(b1.multiply(b2));// 乘
//后面這個(gè) BigDecimal.ROUND_CEILING 需要指定谱煤,是精度
//沒有這個(gè)參數(shù),則會(huì)提示:錯(cuò)誤
System.out.println(b1.divide(b2, BigDecimal.ROUND_CEILING));// 除
}
}</pre>
查看一下結(jié)果
Java基礎(chǔ)之:日期類
日期類中所包含的方法有點(diǎn)多,所以在這里我用junit方法進(jìn)行測(cè)試睹栖,那是真的香 啊硫惕,節(jié)省了大量的代碼編寫時(shí)間
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n45" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">package com.biws.TestDate;
?
import org.junit.jupiter.api.Test;
?
import java.time.;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
?
/*
- @author :biws
- @date :Created in 2020/12/22 22:06
- @description:測(cè)試全部date類方法
*/
public class allDateFunction {
public static void main(String[] args) {
// TODO Auto-generated method stub
new allDateFunction().hi();
new allDateFunction().hello();
}
?
// JUnit 測(cè)試單元
// 1. 配置快捷鍵 alt + J
// 2. 如果要運(yùn)行某個(gè) 測(cè)試單元,就選中方法名或光標(biāo)定位在方法名野来,在運(yùn)行 Junit
// 3. 如果不選恼除,就運(yùn)行,就把所有的測(cè)試單元都運(yùn)行
// 4.@Test,代表此方法是測(cè)試單元梁只,可以單獨(dú)運(yùn)行測(cè)試
?
?
@Test
public void hi() {
System.out.println("hi ");
}
?
@Test
public void hello() {
System.out.println("hello");
}
?
@Test
public void testLocalDate() {
// 獲取當(dāng)前日期(只包含日期缚柳,不包含時(shí)間)
LocalDate date = LocalDate.now();
System.out.println(date);
?
// 獲取日期的指定部分
System.out.println("year:" + date.getYear());
System.out.println("month:" + date.getMonth());
System.out.println("day:" + date.getDayOfMonth());
System.out.println("week:" + date.getDayOfWeek());
?
// 根據(jù)指定的日期參數(shù),創(chuàng)建LocalDate對(duì)象
LocalDate of = LocalDate.of(2010, 3, 2);
System.out.println(of);
?
}
?
// 測(cè)試LocalTime類
@Test
public void testLocalTime() {
// 獲取當(dāng)前時(shí)間(只包含時(shí)間搪锣,不包含日期)
LocalTime time = LocalTime.now();
System.out.println(time);
?
// 獲取時(shí)間的指定部分
System.out.println("hour:" + time.getHour());
System.out.println("minute:" + time.getMinute());
?
System.out.println("second:" + time.getSecond());
System.out.println("nano:" + time.getNano());
?
// 根據(jù)指定的時(shí)間參數(shù),創(chuàng)建LocalTime對(duì)象
LocalTime of = LocalTime.of(10, 20, 55);
System.out.println(of);
?
}
?
// 測(cè)試LocalDateTime類
?
@Test
public void testLocalDateTime() { // 獲取當(dāng)前時(shí)間(包含時(shí)間+日期)
?
LocalDateTime time = LocalDateTime.now();
?
// 獲取時(shí)間的指定部分 System.out.println("year:" + time.getYear());
System.out.println("month:" + time.getMonthValue());
System.out.println("day:" + time.getMonth());
System.out.println("day:" + time.getDayOfMonth());
System.out.println("hour:" + time.getHour());
System.out.println("minute:" + time.getMinute());
?
System.out.println("second:" + time.getSecond());
System.out.println("nano:" + time.getNano());
?
// 根據(jù)指定的時(shí)間參數(shù)彩掐,創(chuàng)建LocalTime對(duì)象
LocalDateTime of = LocalDateTime.of(2020, 2, 2, 10, 20, 55);
System.out.println(of);
?
}
?
@Test
public void testMonthDay() {
?
LocalDate birth = LocalDate.of(1994, 7, 14); // 生日
MonthDay birthMonthDay = MonthDay.of(birth.getMonthValue(), birth.getDayOfMonth());
?
LocalDate now = LocalDate.now(); // 當(dāng)前日期
MonthDay current = MonthDay.from(now);
?
if (birthMonthDay.equals(current)) {
System.out.println("今天生日");
} else {
System.out.println("今天不生日");
}
?
}
?
// 判斷是否為閏年
@Test
public void testIsLeapYear() {
?
LocalDate now = LocalDate.now();
?
System.out.println(now.isLeapYear());
?
}
// 測(cè)試增加日期的某個(gè)部分
?
@Test
public void testPlusDate() {
?
LocalDate now = LocalDate.now(); // 日期
// 3年前 的日期
LocalDate plusYears = now.plusDays(-1);
System.out.println(plusYears);
?
}
?
// 使用plus方法測(cè)試增加時(shí)間的某個(gè)部分
// 時(shí)間范圍判斷
@Test
public void testPlusTime() {
?
LocalTime now = LocalTime.now();// 時(shí)間
?
LocalTime plusHours = now.plusSeconds(-500);
?
System.out.println(plusHours);
?
}
?
// 使用minus方法測(cè)試查看一年前和一年后的日期
?
@Test
public void testMinusTime() {
LocalDate now = LocalDate.now();
?
LocalDate minus = now.minus(1, ChronoUnit.YEARS);
?
// LocalDate minus2 = now.minusYears(1);
System.out.println(minus);
?
}
?
// 測(cè)試時(shí)間戳類:Instant 构舟,相當(dāng)于以前的Date類
@Test
public void testInstant() {
Instant now = Instant.now();
System.out.println(now);
?
// 與Date類的轉(zhuǎn)換
Date date = Date.from(now);
System.out.println(date);
?
Instant instant = date.toInstant();
?
System.out.println(instant);
}
?
// 格式轉(zhuǎn)換
@Test
public void testDateTimeFormatter() {
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("MM-dd yyyy HH:mm:ss");
?
// 將字符串轉(zhuǎn)換成日期
LocalDateTime parse = LocalDateTime.parse("03-03 2017 08:40:50", pattern);
System.out.println(parse);
?
// 將日期轉(zhuǎn)換成字符串
//LocalDateTime parse = LocalDateTime.now();
?
String format = pattern.format(parse);
System.out.println(format);
}
}</pre>