之前有粉丝后台跟我说,作为一个初学者,真的是不清楚该如何去进行学习,直接上ssm框架也看不明白,那我作为这么一个宠粉的人,怎么可能让粉丝有这样的顾虑啊,今天真的是基础到极点了,分享我这边的相应的一些代码实例,因为在代码的备注中已经写的很清楚了,所以基本不会再通过文字进行讲解
文章首发公众号:Java架构师联盟,每日更新技术好文,后面也会开源我的代码仓库,毕竟现在还比较单薄
注释+源码+结果,这边应该展示的很清楚,也比较好理解,不过,一定要自己去实践一下,不实践再简单的技术理解起来也不容易
我敢说这是全网最详细的基础讲解,附源码实例,没人学不明白而向多线程什么的学习实例,我这边之前的文章也整理过,大家可以去查看,每一个都带着相应的源码展示
好了,话不多说,看正题
Java基础之: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:抽象类测试
*/
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. 当一个类继承了抽象类,就要把抽象类的所有抽象方法实现
//2. 所谓方法实现,指的是 把方法体写出, 方法体是空,也可以.
class Cat extends Animal {
public Cat(String name) {
super(name);
// TODO Auto-generated constructor stub
}
public void eat() {
System.out.println(getName() + " 爱吃 <・)))><<");
}
}</pre>
然后呢,我们看一下多态的实现
<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:多态在抽象类中的实现
*/
public class AbstractPolArray {
public static void main(String[] args) {
//抽象类不可以实例化,但可以使用多态数组
Animal1[] animal1 = new Animal1[2];
animal1[0] = new Dog1("小黑狗");
animal1[1] = new Cat1("小花猫");
//多态数组的使用
for (int i = 0; i < animal1.length; i++) {
show(animal1[i]);
}
}
//这里不用担心会传入一个Animal类型的实例,因为Animal不能实例化
//编译器不会通过,所以只会传入Animal的子类实例
public static void show(Animal1 a) {
a.eat(); //多态的使用
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;
}
//动物都有eat的动作,但我们并不知道每一个动物具体怎么样eat
//所以这里通过抽象提供了eat方法,需要子类来实现
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>
最后举个小例子总结一下
<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:模板设计模式
*/
public class Abstract_Template {
public static void main(String[] args) {
Template sub = new Sub();
sub.caleTimes(); //实际是调用了Template中的caleTimes方法
Template subStringB = new SubStringB();
subStringB.caleTimes();
//这里可以看到 StringBuffer在拼接字符串时,远远优于String拼接的效率
}
}
abstract class Template{ //抽象类
public abstract void code(); //抽象方法
public void caleTimes(){ // 统计耗时多久是确定
//统计当前时间距离 1970-1-1 0:0:0 的时间差,单位ms
long start = System.currentTimeMillis();
code(); //这里的code在调用时,就是指向子类中已经重写实现了的code
long end = System.currentTimeMillis();
System.out.println("耗时:"+(end-start));
}
}
class Sub extends Template{
@Override
public void code() {
String x = "";
for(int i = 0;i < 10000 ; i++) { //拼接1W个hello 看处理时间
x += "hello" + i;
}
}
}
class SubStringB extends Template{
@Override
public void code() {
StringBuffer stringBuffer = new StringBuffer();
for(int i = 0;i < 10000 ; i++) { //拼接1W个hello 看处理时间
stringBuffer.append("hello" + i);
}
}
}</pre>
Java基础之:StringBuffer与StringBuilder
简单直白点,直接一套代码走起
<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对比
*/
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的执行时间:" + (endTime - startTime));
startTime = System.currentTimeMillis();
for (int i = 0; i < 80000; i++) {
builder.append(String.valueOf(i));
}
endTime = System.currentTimeMillis();
System.out.println("StringBuilder的执行时间:" + (endTime - startTime));
startTime = System.currentTimeMillis();
for (int i = 0; i < 80000; i++) {
text = text + i;
}
endTime = System.currentTimeMillis();
System.out.println("String的执行时间:" + (endTime - startTime));
}
}</pre>
查看一下执行结果
我敢说这是全网最详细的基础讲解,附源码实例,没人学不明白Java基础之:Math & Arrays
Math
简单粗暴,直接进行代码展示
<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:测试math类
/
public class ClassTest {
public static void main(String[] args) {
//1.abs 绝对值
int abs = Math.abs(9);
System.out.println(abs);
//2.pow 求幂
double pow = Math.pow(-3.5, 4);
System.out.println(pow);
//3.ceil 向上取整,返回>=该参数的最小整数;
double ceil = Math.ceil(-3.0001);
System.out.println(ceil);
//4.floor 向下取整,返回<=该参数的最大整数
double floor = Math.floor(-4.999);
System.out.println(floor);
//5.round 四舍五入 Math.floor(该参数+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 返回随机数【0——1)
//[a-b]:int num = (int)(Math.random()(b-a+1)+a)
double random = Math.random();
System.out.println(random);
//小技巧:获取一个 a-b 之间的一个随机整数
int a = (int)(Math.random()(15-7+1)+7);
System.out.println(a);
/ - 理解:
- 1.Math.random() 是 [0,1)的随机数
- 2.(Math.random()*(15-7+1) 就是[0,9)
- 3.Math.random()*(15-7+1)+7 就是[7,16)
- 4.(int)取整就是 [7,15] ,即[a,b]之间的随机整数
*/
}
}</pre>
执行结果
我敢说这是全网最详细的基础讲解,附源码实例,没人学不明白Arrays
正常的执行
<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); //默认小到大排序
System.out.println(Arrays.toString(arr));
// 使用匿名内部类重写compare方法,实现从大到小排序
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>
结果查看
我敢说这是全网最详细的基础讲解,附源码实例,没人学不明白重写之后的执行结果
<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;// 自动装箱
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接口对象为参数,就是简单的冒泡排序方法
System.out.println(Arrays.toString(arr));
//添加匿名内部类对象为参数,就可以改变排序方式。用此方法可以很灵活的使用排序。
overwriteArraySort.sort(arr, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
//通过返回值的正负来控制,升序还是降序
//只有当返回值为1时,才会发生交换。例如这里,o1 < o2时返回1 ,进行交换
//也就是需要前面的数,比后面的数大,即降序
if (o1 < o2) {
return -1;
} else if (o1 > o2) {
return 1;
} else {
return 0;
}
}
});
System.out.println(Arrays.toString(arr));
}
}</pre>
结果
我敢说这是全网最详细的基础讲解,附源码实例,没人学不明白Java基础之:大数
<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.调用常见的运算方法
// System.out.println(b1+b2); 不能使用 这样的 + 方法运行
// 并且,add 这些方法只能是大数于大数相加,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.调用常见的运算方法
// System.out.println(b1+b2); 不能使用 + 号运算..
// 并且,add 这些方法只能是大数于大数相加,BigDecimal.add(BigDecimal);
System.out.println(b1.add(b2));// 加
System.out.println(b1.subtract(b2));// 减
System.out.println(b1.multiply(b2));// 乘
//后面这个 BigDecimal.ROUND_CEILING 需要指定,是精度
//没有这个参数,则会提示:错误
System.out.println(b1.divide(b2, BigDecimal.ROUND_CEILING));// 除
}
}</pre>
查看一下结果
我敢说这是全网最详细的基础讲解,附源码实例,没人学不明白Java基础之:日期类
日期类中所包含的方法有点多,所以在这里我用junit方法进行测试,那是真的香 啊,节省了大量的代码编写时间
<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:测试全部date类方法
*/
public class allDateFunction {
public static void main(String[] args) {
// TODO Auto-generated method stub
new allDateFunction().hi();
new allDateFunction().hello();
}
// JUnit 测试单元
// 1. 配置快捷键 alt + J
// 2. 如果要运行某个 测试单元,就选中方法名或光标定位在方法名,在运行 Junit
// 3. 如果不选,就运行,就把所有的测试单元都运行
// 4.@Test,代表此方法是测试单元,可以单独运行测试
@Test
public void hi() {
System.out.println("hi ");
}
@Test
public void hello() {
System.out.println("hello");
}
@Test
public void testLocalDate() {
// 获取当前日期(只包含日期,不包含时间)
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());
// 根据指定的日期参数,创建LocalDate对象
LocalDate of = LocalDate.of(2010, 3, 2);
System.out.println(of);
}
// 测试LocalTime类
@Test
public void testLocalTime() {
// 获取当前时间(只包含时间,不包含日期)
LocalTime time = LocalTime.now();
System.out.println(time);
// 获取时间的指定部分
System.out.println("hour:" + time.getHour());
System.out.println("minute:" + time.getMinute());
System.out.println("second:" + time.getSecond());
System.out.println("nano:" + time.getNano());
// 根据指定的时间参数,创建LocalTime对象
LocalTime of = LocalTime.of(10, 20, 55);
System.out.println(of);
}
// 测试LocalDateTime类
@Test
public void testLocalDateTime() { // 获取当前时间(包含时间+日期)
LocalDateTime time = LocalDateTime.now();
// 获取时间的指定部分 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());
// 根据指定的时间参数,创建LocalTime对象
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(); // 当前日期
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());
}
// 测试增加日期的某个部分
@Test
public void testPlusDate() {
LocalDate now = LocalDate.now(); // 日期
// 3年前 的日期
LocalDate plusYears = now.plusDays(-1);
System.out.println(plusYears);
}
// 使用plus方法测试增加时间的某个部分
// 时间范围判断
@Test
public void testPlusTime() {
LocalTime now = LocalTime.now();// 时间
LocalTime plusHours = now.plusSeconds(-500);
System.out.println(plusHours);
}
// 使用minus方法测试查看一年前和一年后的日期
@Test
public void testMinusTime() {
LocalDate now = LocalDate.now();
LocalDate minus = now.minus(1, ChronoUnit.YEARS);
// LocalDate minus2 = now.minusYears(1);
System.out.println(minus);
}
// 测试时间戳类:Instant ,相当于以前的Date类
@Test
public void testInstant() {
Instant now = Instant.now();
System.out.println(now);
// 与Date类的转换
Date date = Date.from(now);
System.out.println(date);
Instant instant = date.toInstant();
System.out.println(instant);
}
// 格式转换
@Test
public void testDateTimeFormatter() {
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("MM-dd yyyy HH:mm:ss");
// 将字符串转换成日期
LocalDateTime parse = LocalDateTime.parse("03-03 2017 08:40:50", pattern);
System.out.println(parse);
// 将日期转换成字符串
//LocalDateTime parse = LocalDateTime.now();
String format = pattern.format(parse);
System.out.println(format);
}
}</pre>
网友评论