目录
1、时间类
/*Instant:瞬时,得到毫秒数
DateTimeFormatter:格式化或解析日期、时间 类似于SimpleDateFormat
*/
public class InstantTest {
@Test
public void test(){
//now():获取本初子午线
Instant instant=Instant.now();
System.out.println(instant);
//添加时间偏移量
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);
//获取对应的毫秒数,从1970,0时,0分到现在
long mi = instant.toEpochMilli();
System.out.println(mi);
//把毫秒数转化成date时间
Instant instant1 = Instant.ofEpochMilli(1660653845274l);
System.out.println(instant);
}
@Test
public void test2(){
// 方式一:预定义的标准格式ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
DateTimeFormatter isoLocalDate = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
LocalDateTime localDateTime=LocalDateTime.now();
String str1 = isoLocalDate.format(localDateTime);
System.out.println(localDateTime);
System.out.println(str1);//2022-08-16T20:57:18.185
//解析:字符串--->日期
TemporalAccessor parse = isoLocalDate.parse("2022-08-16T20:57:18.185");
System.out.println(parse);
// 方式二:本地化相关的格式。如:ofLocalizedDateTime(FormatStyle.LONG)
// 方式三:自定义的格式。如:ofPattern(“yyyy-MM-dd hh:mm:ss”)
}
}
2、jdk8中设置时间
/*
本地日期:LocalDate
本地时间:LocalTime
时间如期:LocalDateTime
说明:
1、LocalDateTime相较于另外两个使用的多一点
2、类似于Calendar
*/
public class JDK8DataTest {
@Test
public void test(){
//偏移量,1.8之前
Date data=new Date(2020,9,8);
System.out.println(data);//Fri Oct 08 00:00:00 CST 3920
}
@Test
public void test1(){
//now()获取当前的时间,日期
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,20,23,45,12);
System.out.println(localDateTime1);
//getxxx():
System.out.println(localDateTime.getDayOfMonth());
System.out.println(localDateTime.getDayOfWeek());
System.out.println(localDateTime.getDayOfYear());
System.out.println(localDateTime.getMonth());
//不可变性
//withxxx():设置相关属性,原数据不变
LocalDate localDate1=localDate.withDayOfMonth(22);
System.out.println(localDate);
System.out.println(localDate1);
//plus加操作
LocalDateTime localDateTime2=localDateTime.plusMinutes(3);
System.out.println(localDateTime);
System.out.println(localDateTime2);
//minus减操作
LocalDateTime localDateTime3=localDateTime.minusDays(10);
System.out.println(localDateTime);
System.out.println(localDateTime3);
}
}
3、JDK1.8之前日期时间测试
/** * JDK1.8之前日期时间的API测试 * Calendar 日历类(抽象类)的使用 * 获取月份时:一月是0 * 获取星期:周日是1 * */
public class CalendarTest {
@Test
public void test(){
//1、实例化
//方式一:创建静态其子类(GregorianCalendar)的对象
//方式二:调用其静态方法getInstance()
Calendar calendar = new GregorianCalendar();
System.out.println(calendar);
//、常用方法
//get()
int days = calendar.get(Calendar.DAY_OF_MONTH);//月份
System.out.println(days);
System.out.println(calendar.get(Calendar.DAY_OF_WEEK));
//set()
//把当前对象的calendar本省改掉
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 = calendar.getTime();
System.out.println(date);
//setTime()设置当前时间
Date date1=new Date();
calendar.setTime(date1);//把当前calendar时间修改
days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
}
}