新时间日期API
1.java.time – 包含值对象的基础包
2.java.time.chrono – 提供对不同的日历系统的访问
3.java.time.format – 格式化和解析时间和日期
4.java.time.temporal – 包括底层框架和扩展特性
5.java.time.zone – 包含时区支持的类
常用类LocalDate、LocalTime、LocalDateTime
LocalDate、LocalTime、LocalDateTime 类是其中较重要的几个类,它们的实例 是不可变的对象,分别表示使用 ISO-8601日历系统的日期、时间、日期和时间。 它们提供了简单的本地日期或时间,并不包含当前的时间信息,也不包含与时区 相关的信息。 LocalDate代表IOS格式(yyyy-MM-dd)的日期,可以存储 生日、纪念日等日期。 LocalTime表示一个时间,而不是日期。 LocalDateTime是用来表示日期和时间的,这是一个最常用的类之一。
LocalDate、LocalTime、LocalDateTime类中常用方法:
实例
package com.qwy.datetime;
import org.junit.Test;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
/**
* @author qwy
* @create 2021-03-19 15:36
**/
public class TestNewDate {
@Test
public void test01(){
//返回当前日期
LocalDate localDate = LocalDate.now();
System.out.println("localDate = " + localDate);//localDate = 2021-03-19
//获取当前时间
LocalTime localTime = LocalTime.now();
System.out.println("localTime = " + localTime);//localTime = 15:53:05.773
LocalDateTime localDateTime = LocalDateTime.now();
//获取当前时间和日期
System.out.println("localDateTime = " + localDateTime);//localDateTime = 2021-03-19T15:53:05.773
}
@Test
public void test02(){
//获取指定年,月,日的日期
LocalDate localDate = LocalDate.of(2021, 02, 02);
System.out.println("localDate = " + localDate);//localDate = 2021-02-02
//获取指定时,分,秒的时间
LocalTime localTime = LocalTime.of(23, 12, 5);
System.out.println("localTime = " + localTime);//localTime = 23:12:05
//获取指定年,月,日,时,分,秒的日期时间
LocalDateTime localDateTime = LocalDateTime.of(2020, 9, 23, 12, 30, 34);
System.out.println("localDateTime = " + localDateTime);//localDateTime = 2020-09-23T12:30:34
}
@Test
public void test03(){
LocalDate localDate = LocalDate.now();
int year = localDate.getYear();
System.out.println("year = " + year);//year = 2021
Month month = localDate.getMonth();
System.out.println("month = " + month);//month = MARCH
int monthValue = localDate.getMonthValue();
System.out.println("monthValue = " + monthValue);//monthValue = 3
int dayOfMonth = localDate.getDayOfMonth();
System.out.println("dayOfMonth = " + dayOfMonth);//dayOfMonth = 19
}
@Test//方式一:预定义的标准格式
public void test04(){
DateTimeFormatter basicIsoDate = DateTimeFormatter.ISO_DATE;
String format = basicIsoDate.format(LocalDateTime.now());
System.out.println("format = " + format);//format = 2021-03-19
DateTimeFormatter isoDate = DateTimeFormatter.ISO_LOCAL_DATE;
String format1 = isoDate.format(LocalDateTime.now());
System.out.println("format1 = " + format1);//format1 = 2021-03-19
DateTimeFormatter isoTime = DateTimeFormatter.ISO_TIME;
String format2 = isoTime.format(LocalDateTime.now());
System.out.println("format2 = " + format2);//format2 = 16:18:45.76
}
@Test// 本地化相关的格式。
public void test05(){
DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
String str3 = formatter2.format(LocalDate.now());
System.out.println("str3 = " + str3);//str3 = 2021-3-19
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
String format = dateTimeFormatter.format(LocalDateTime.now());
System.out.println("format = " + format);//format = 2021年3月19日 星期五
}
@Test//自定义的格式
public void test06(){
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss SSS");
String format = dateTimeFormatter.format(LocalDateTime.now());
System.out.println("format = " + format);//format = 2021-03-19 16:25:38 958
}
}
=============================================================================================
如有不妥之处,欢迎大家给予批评指出,如果对您有帮助,给留下个小赞赞哦
==============================================================================================