Java时间类API
直接上代码一目了然
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;
import java.util.Calendar;
import java.util.Date;
public class DateTimeTest {
/**
* SimpleDateFormat的使用:SimpleDateFormat对日期Date类的格式化和解析
* 1.两种操作
* 1.1 格式化:日期--->字符串
* 1.2 解析:格式化的逆过程,字符串--->日期
*
* 2. SimpleDateFormat的实例化
*/
@Test
public void testSimpleDateFormat() throws ParseException {
//实例化SimpleDateFormat
SimpleDateFormat sdf= new SimpleDateFormat();
//格式化 日期--->字符串
Date date = new Date();
System.out.println(date);
String format = sdf.format(date);
// System.out.println(format.getClass());
System.out.println(format);
//解析:格式化的逆过程,字符串---->日期
Date date1 = sdf.parse("21-2-15 下午8:26");
System.out.println(date1);
//-------------指定格式的格式化-----------------
// SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String format1 = sdf1.format(date);
System.out.println(format1);
Date date2 = sdf1.parse("2021-02-15 08:33:48");
System.out.println(date2);
//-------------------将字符串转化为java.sql.Date-----------------------
String Birth = "2020-09-08";
SimpleDateFormat sdf2= new SimpleDateFormat("yyyy-MM-dd");
Date date3 = sdf2.parse(Birth);
java.sql.Date birthDate = new java.sql.Date(date3.getTime());
System.out.println(birthDate);
}
/**
* Calendar日历类的使用
* 是一个抽象类
* 方式一:创建其子类(GregorianCalendar)的对象
* 方式二:调用其静态方法getInstance()
*
*/
@Test
public void testCalendar(){
//1.实例化
Calendar calendar = Calendar.getInstance();
//2.常用方法
//get()
int days=calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
//set()
calendar.set(Calendar.DAY_OF_MONTH,22);
days=calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
//add()
calendar.add(Calendar.DAY_OF_MONTH,3);
days=calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
//getTime() 日历类--->Date
Date date=calendar.getTime();
System.out.println(date);
//setTime()Date---->日历类
Date date1=new Date();
calendar.setTime(date1);
days=calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
}
/**
* LocalDate、LocalTime、LocalDateTime(使用的较多)的使用
*/
@Test
public void JDK8DateTest1(){
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate);
System.out.println(localTime);
System.out.println(localDateTime);
//of():设置指定的年月日时分秒。没有偏移量
LocalDateTime localDateTime1 = LocalDateTime.of(2020, 10, 6, 13, 23, 43);
System.out.println(localDateTime1);
//getXXX()
System.out.println(localDateTime.getDayOfMonth());
System.out.println(localDateTime.getDayOfWeek());
System.out.println(localDateTime.getDayOfYear());
System.out.println(localDateTime.getHour());
//With操作 体现不可变性 设置相关属性
LocalDateTime localDateTime2 = localDateTime.withDayOfMonth(22);
System.out.println(localDateTime);
System.out.println(localDateTime2);
LocalDateTime localDateTime3 = localDateTime.withHour(4);
System.out.println(localDateTime3);
//plus不可变性,增加指定时间
LocalDateTime localDateTime4 = localDateTime.plusDays(13);
System.out.println(localDateTime4);
//minus 减去指定时间
LocalDateTime localDateTime5 = localDateTime.minusDays(6);
System.out.println(localDateTime5);
}
/**
* Instant的使用
*/
@Test
public void instatTest(){
//格林尼治时间
Instant instant = Instant.now();
System.out.println(instant);//2021-02-16T05:08:35.203Z
//在格林尼治时间基础上加8小时
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);//2021-02-16T13:15:25.903+08:00
//获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数
long milli = instant.toEpochMilli();
System.out.println(milli);
//ofEpochMilli():通过给定的毫秒数,获取Instant实例 -->Date(long millis)
Instant instant1 = Instant.ofEpochMilli(1613452644332L);
System.out.println(instant1);
}
/**
* DateTimeFormatter:格式化或解析日期、时间
* 类似于SimpleDateFormat
*/
@Test
public void DateTimeFormatterTest(){
//方式一:用预定义的标准格式
//日期--->字符串
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
LocalDateTime now = LocalDateTime.now();
String str1 = formatter.format(now);
System.out.println(now);
System.out.println(str1);
//解析 字符串--->日期
TemporalAccessor parse = formatter.parse("2021-02-16T13:25:40.853");
System.out.println(parse);
//方式二
//本地格式化格式
// DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
// DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
String str2 = formatter1.format(now);
System.out.println(str2);
//方式三 (常用)自定义的格式 如:ofPattern("yyyy-MM-dd hh:mm:ss")
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
//格式化
String str3 = formatter2.format(now);
System.out.println(str3);
//解析
TemporalAccessor parse1 = formatter2.parse("2021-02-16 01:35:27");
System.out.println(parse1);
}
}