package dateOpera;
import org.junit.Test;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.TemporalUnit;
public class DateFormat {
/**
* localDate类常用操作
*/
@Test
public void test(){
//获取当前日期
LocalDate now = LocalDate.now();
System.out.println(now); //2021-08-02
//获取指定日期
LocalDate of = LocalDate.of(2020, 1, 1);
System.out.println(of); //2020-01-01
//将日期类型格式化成指定格式得字符串
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
String format = now.format(dtf);
System.out.println(format);
int year = now.getYear();
int monthValue = now.getMonthValue();
int dayOfMonth = now.getDayOfMonth();
System.out.println("年 : " + year); //年 : 2021
System.out.println("月 : " + monthValue); //月 : 8
System.out.println("日 : " + dayOfMonth); //日 : 2
System.out.println("月中的第几天 : " + dayOfMonth); //月中的第几天 : 2
DayOfWeek dayOfWeek = now.getDayOfWeek();
int value = dayOfWeek.getValue();
System.out.println("周中的第几天 : " + value); //周中的第几天 : 1
//年增加1
LocalDate plus = now.plus(1, ChronoUnit.YEARS);
System.out.println(plus); //2022-08-02
//月增加1
LocalDate plus1 = now.plus(1, ChronoUnit.MONTHS);
System.out.println(plus1); //2021-09-02
//日增加1
LocalDate plus2 = now.plus(1, ChronoUnit.DAYS);
System.out.println(plus2); //2021-08-03
//指定日期到当月的最后一天
LocalDate with = now.with(TemporalAdjusters.lastDayOfMonth());
System.out.println(with); //2021-08-31
//指定日期到本周的第五天
LocalDate with1 = now.with(TemporalAdjusters.next(DayOfWeek.of(5)));
System.out.println(with1); //2021-08-06
//指定日期到下个月的第一天
LocalDate with2 = now.with(TemporalAdjusters.firstDayOfNextMonth());
System.out.println(with2); //2021-09-01
//指定月份到六月
LocalDate localDate = now.withMonth(6);
System.out.println(localDate); //2021-06-02
//指定年份到1998年
LocalDate localDate1 = now.withYear(1998);
System.out.println(localDate1); //1998-08-02
//判断当前日期是否在1998-03-11日期之后
boolean after = now.isAfter(LocalDate.of(1998, 3, 11));
System.out.println(after); //true
//判断当前日期是否在1998-03-11日期之前
boolean before = now.isBefore(LocalDate.of(1998, 3, 11));
System.out.println(before);
//判断是否是闰年
boolean leapYear = now.isLeapYear();
System.out.println(leapYear);
}
@Test
public void test1(){
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime nowTime = LocalTime.now();
System.out.println(nowTime);
//将日期类型转化为指定的字符串类型
String hhmmss = nowTime.format(dtf);
System.out.println(hhmmss);
//将字符串类型转化为指定的日期类型
LocalTime parse = LocalTime.parse("11:11:11",dtf);
System.out.println(parse);
}
@Test
public void test2(){
Instant instant = Instant.now();
System.out.println(instant);
//获取毫秒数
long l = instant.toEpochMilli();
System.out.println(l);
}
@Test
public void test3(){
//将localDateTime转化为时间戳
LocalDateTime ldt = LocalDateTime.now();
ZonedDateTime zonedDateTime = ldt.atZone(ZoneOffset.of("+8"));
long l = zonedDateTime.toInstant().toEpochMilli();
System.out.println(l);
//将时间戳转化为localDateTime
Instant instant = Instant.now();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
System.out.println(localDateTime);
}
@Test
public void test4(){
LocalDateTime ldt = LocalDateTime.now();
//获取当天的最大时间
LocalDateTime with = ldt.with(LocalTime.MAX);
System.out.println(with); //2021-08-02T23:59:59.999999999
//获取当天的最小时间
LocalDateTime with1 = ldt.with(LocalTime.MIN);
System.out.println(with1); //2021-08-02T00:00
//获取当月的最大时间
LocalDateTime with2 = ldt.with(TemporalAdjusters.lastDayOfMonth());
LocalDateTime with3 = with2.with(LocalTime.MAX);
System.out.println(with3);
//获取当月的最小时间
LocalDateTime with4 = ldt.with(TemporalAdjusters.firstDayOfMonth());
LocalDateTime with5 = with4.with(LocalTime.MIN);
System.out.println(with5);
}
@Test
public void test5(){
LocalDate now = LocalDate.now();
LocalDate of = LocalDate.of(2020, 9, 19);
System.out.println(now);
System.out.println(of);
Period between = Period.between(of, now);
//获取两个日期相差多少日
int days = between.getDays();
System.out.println(days); //14
//获取两个日期相差多少月
int months = between.getMonths();
System.out.println(months); //10
//获取两个日期相差多少年
int years = between.getYears();
System.out.println(years); //0
//获取两个日期相差多少天
long l = now.toEpochDay() - of.toEpochDay();
System.out.println(l); //317
}
@Test
public void test6(){
LocalTime now = LocalTime.now();
LocalTime of = LocalTime.of(16, 0, 0);
Duration between = Duration.between(now, of);
//相差多少秒
long seconds = between.getSeconds();
System.out.println(seconds);
//相差多少小时
long l = between.toHours();
System.out.println(l);
//相差多少天
long l1 = between.toDays();
System.out.println(l1);
}
}
java8 时间类操作
最新推荐文章于 2024-07-01 08:58:12 发布