The new date and time API core located within java.time.
All code can be run:
package com.zicheng.datetime;
import java.time.*;
/**
* 子诚
* Description:JDK8新特性时间日期库DateTimeAPI
* 时间:2020/3/22 16:27
*/
public class MyDataTime {
public static void main(String[] args) {
MyDataTime myDataTime = new MyDataTime();
//1、只获取日期
myDataTime.getDate();
//2、获取当前时间
myDataTime.getTime();
//3、获取时间和日期
myDataTime.getDateAndTime();
}
//3、获取时间和日期
private void getDateAndTime() {
LocalDateTime now = LocalDateTime.now();
System.out.println("当前的日期和时间是:" + now);//当前的日期和时间是:2020-03-22T17:32:29.196
// 拼接日期
LocalDateTime localDateTime = LocalTime.now().atDate(LocalDate.now());
System.out.println("拼接后的日期与时间" + localDateTime);//拼接后的日期与时间2020-03-22T17:32:29.198
}
//1、只获取日期
void getDate() {
LocalDate today = LocalDate.now();
System.out.println(today);//得出结果:2020-03-22
//获取年数
int year = today.getYear();
//获取月份数
int month = today.getMonthValue();
//获取在当前月的多少号
int day = today.getDayOfMonth();
System.out.printf("%d 年; %d 月; %d 日 \t %n", year, month, day);//结果为:2020 年; 3 月; 22 日
DayOfWeek week = today.getDayOfWeek();
System.out.println("今天是星期几:" + week);//结果是:今天是星期几:SUNDAY
int days = today.getDayOfYear();
System.out.println("今天是" + year + "年的第" + days + "天");//今天是2020年的第82天
//自定义时间
LocalDate oneDay = LocalDate.of(2020, 2, 1);
System.out.println(oneDay); // 默认就是2020-02-01
// 定义任意比较
LocalDate anyDay = LocalDate.of(2020, 2, 1);
System.out.println(oneDay.equals(anyDay));//true
}
//2、只获取时间
void getTime() {
LocalTime localTime = LocalTime.now();
System.out.println(localTime);//17:19:20.834,也就是,HH:mm:ss.nnn,这里nnn是纳秒。
// 23:59:59.999999999
LocalTime maxTime = LocalTime.MAX;
// 00:00
LocalTime minTime = LocalTime.MIN;
System.out.println("最大时间:" + maxTime);//最大时间:23:59:59.999999999
System.out.println("最小时间:" + minTime);//最小时间:00:00
}
}
A single code as follows:
1 LOCALDATE
LocalDate class contains only the date and the specific time is not included.
Only need to indicate the date and does not include time, you can use it.
//1、只获取日期
void getDate() {
LocalDate today = LocalDate.now();
System.out.println(today);//得出结果:2020-03-22
//获取年数
int year = today.getYear();
//获取月份数
int month = today.getMonthValue();
//获取在当前月的多少号
int day = today.getDayOfMonth();
System.out.printf("%d 年; %d 月; %d 日 \t %n", year, month, day);//结果为:2020 年; 3 月; 22 日
DayOfWeek week = today.getDayOfWeek();
System.out.println("今天是星期几:"+week);//结果是:今天是星期几:SUNDAY
int days = today.getDayOfYear();
System.out.println("今天是"+year+"年的第"+days+"天");//今天是2020年的第82天
//自定义时间
LocalDate oneDay = LocalDate.of(2020,2,1);
System.out.println(oneDay); // 默认就是2020-02-01
// 定义任意比较
LocalDate anyDay = LocalDate.of(2020,2,1);
System.out.println(oneDay.equals(anyDay));//true
}
2, LocalTime- how to obtain time
//2、只获取时间
void getTime() {
LocalTime localTime = LocalTime.now();
System.out.println(localTime);//17:19:20.834,也就是,HH:mm:ss.nnn,这里nnn是纳秒。
// 23:59:59.999999999
LocalTime maxTime = LocalTime.MAX;
// 00:00
LocalTime minTime = LocalTime.MIN;
System.out.println("最大时间:"+maxTime);//最大时间:23:59:59.999999999
System.out.println("最小时间:"+minTime);//最小时间:00:00
}
3, a combination of date and time LocalDateTime-
//3、获取时间和日期
private void getDateAndTime() {
LocalDateTime now = LocalDateTime.now();
System.out.println("当前的日期和时间是:"+now);//当前的日期和时间是:2020-03-22T17:32:29.196
// 拼接日期
LocalDateTime localDateTime = LocalTime.now().atDate(LocalDate.now());
System.out.println("拼接后的日期与时间"+localDateTime);//拼接后的日期与时间2020-03-22T17:32:29.198
}