package com.zhangwl.pg0924;

import org.junit.Test;

import javax.swing.text.DateFormatter;
import java.sql.Date;
import java.text.DateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

/**
* @ClassName DateApiDemo
* @Description
* @Author zhangwl
* @Date 2019/9/24 22:43
* @Version 1.0
**/
public class DateApiDemo {

/*日期*/
@Test
public void test_local_date() {
LocalDate nowDate = LocalDate.now();
System.out.println("今天的日期:" + nowDate);
int year = nowDate.getYear();
System.out.println("年:" + year);
int month = nowDate.getMonthValue();
System.out.println("月:" + month);
int day = nowDate.getDayOfMonth();
System.out.println("日:" + day);
}

/*时间*/
@Test
public void test_local_time() {
LocalTime nowTime = LocalTime.now();
System.out.println("现在的时间:" + nowTime);
int hour = nowTime.getHour();
System.out.println("时:" + hour);
int minute = nowTime.getMinute();
System.out.println("分:" + minute);
int second = nowTime.getSecond();
System.out.println("秒:" + second);
int nano = nowTime.getNano();
System.out.println("毫秒:" + nano);
}

/*日期时间*/
@Test
public void test_local_date_time() {
LocalDateTime nowDateTime = LocalDateTime.now();
System.out.println("今天日期时间:" + nowDateTime);
System.out.println("年:" + nowDateTime.getYear());
System.out.println("月:" + nowDateTime.getMonthValue());
System.out.println("日:" + nowDateTime.getDayOfMonth());
System.out.println("**********************************");
System.out.println("时:" + nowDateTime.getHour());
System.out.println("分:" + nowDateTime.getMinute());
System.out.println("秒:" + nowDateTime.getSecond());
System.out.println("毫秒:" + nowDateTime.getNano());
}

/*获取指定日期的日期*/
@Test
public void test_cust_local_date() {
LocalDate localDate = LocalDate.of(1991, 10, 12);
System.out.println(LocalDate.ofYearDay(1991, localDate.getDayOfYear()));
System.out.println(LocalDate.ofEpochDay(localDate.toEpochDay()));//参数为距离1970-01-01的天数
System.out.println("*******************************************");
System.out.println(LocalDate.parse("1991-10-12"));
System.out.println(LocalDate.parse("19911012", DateTimeFormatter.ofPattern("yyyyMMdd")));
}

@Test
public void test_cust_local_time() {
System.out.println(LocalTime.of(8, 20));
System.out.println(LocalTime.of(8, 20, 30));
System.out.println(LocalTime.of(8, 20, 30, 40));
System.out.println("*************************************************************");
LocalTime tTime = LocalTime.of(8, 20, 30, 40);
System.out.println(LocalTime.ofSecondOfDay(tTime.toSecondOfDay()));//参数为距离当天零时的秒数
System.out.println(LocalTime.ofNanoOfDay(tTime.toNanoOfDay()));
System.out.println("*************************************************************");
System.out.println(LocalTime.parse("08:20:30"));
System.out.println(LocalTime.parse("082030", DateTimeFormatter.ofPattern("HHmmss")));
}

@Test
public void test_cust_local_date_time() {
System.out.println(LocalDateTime.of(1991, 10, 12, 8, 20, 30));
System.out.println(LocalDateTime.of(1991, Month.OCTOBER, 8, 20, 30, 150));
System.out.println(LocalDateTime.parse("1991-10-12 08:20:30", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}

/*日期的比较*/
@Test
public void test_comp_local_date() {
LocalDate myDate = LocalDate.of(1991, 10, 12);
LocalDate nowDate = LocalDate.now();
/*比较两个日期*/
// System.out.println("今天是1991-10-12吗?" + nowDate.equals(myDate));
// System.out.println(myDate + "是否在" + nowDate + "之前?=>" + myDate.isBefore(nowDate));
// System.out.println(myDate + "是否在" + nowDate + "之后?=>" + myDate.isAfter(nowDate));

MonthDay nowDay = MonthDay.from(nowDate);
MonthDay myDay = MonthDay.from(myDate);
// System.out.println(nowDay + " " + myDay);
System.out.println("今天是你的生日吗?" + myDay.equals(nowDay));
}

@Test
public void test_year_month() {
YearMonth yearMonth = YearMonth.of(2014, 2);
System.out.println("YearMonth:" + yearMonth);
System.out.println(yearMonth + "有多少天?" + yearMonth.lengthOfMonth() + "天");
System.out.println(yearMonth + "有多少天?" + yearMonth.lengthOfYear() + "天");
System.out.println(yearMonth + "是闰年?" + (yearMonth.isLeapYear() ? "是" : "否"));
}

/*修改日期*/
@Test
public void test_modfity_date() {
LocalDate nowDate = LocalDate.now();
System.out.println("当前日期:" + nowDate);
System.out.println("去年的日期:" + nowDate.minusYears(1));
System.out.println("一个月前的日期:" + nowDate.minusMonths(1));
System.out.println("一周前:" + nowDate.minusWeeks(1));
System.out.println("一天前:" + nowDate.minusDays(1));

}

/*改变时间*/
@Test
public void test_judge_repeat_event() {
LocalTime nowTime = LocalTime.now();
System.out.println("当前时间:" + nowTime);
System.out.println("1小时前:" + nowTime.minusHours(1));
System.out.println("1分钟前:" + nowTime.minusMinutes(1));
System.out.println("1秒前:" + nowTime.minusSeconds(1));
System.out.println("**********************************");
System.out.println("1小时后:" + nowTime.plusHours(1));
System.out.println("1分钟后:" + nowTime.plusMinutes(1));
System.out.println("1秒钟后:" + nowTime.plusSeconds(1));
}

/*使用通用方法增减日期\时间*/
@Test
public void test_use_commom_minus_plus_unit_to_work() {
LocalDate nowDate = LocalDate.now();
System.out.println("当前日期:" + nowDate);
System.out.println("一年后:" + nowDate.plus(1, ChronoUnit.YEARS));
System.out.println("一月后:" + nowDate.plus(1, ChronoUnit.MONTHS));
System.out.println("************************************************************");
System.out.println("一年前:" + nowDate.minus(1, ChronoUnit.YEARS));
System.out.println("一月前:" + nowDate.minus(1, ChronoUnit.MONTHS));

System.out.println("###############################################################");
LocalTime nowTime = LocalTime.now();
System.out.println("当前时间:" + nowTime);
System.out.println("一小时后:" + nowTime.plus(1, ChronoUnit.HOURS));
System.out.println("一分钟后:" + nowTime.plus(1, ChronoUnit.MINUTES));
System.out.println("**************************************************************");
System.out.println("一小时前:" + nowTime.minus(1, ChronoUnit.HOURS));
System.out.println("一分钟前:" + nowTime.minus(1, ChronoUnit.MINUTES));
}

/*时间戳*/
@Test
public void test_timestamp() {
Instant timestamp = Instant.now();
System.out.println("时间戳:" + timestamp);
java.util.Date date = Date.from(timestamp);
System.out.println("时间戳转换为日期:" + date);
System.out.println("还原时间戳:" + date.toInstant());
}

/*格式化日期、时间*/
@Test
public void test_format_date_time() {
LocalDateTime nowDateTime = LocalDateTime.now();
System.out.println("格式化前的当前日期:" + nowDateTime);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formatDateTime1 = formatter.format(nowDateTime);
System.out.println("格式化后的当前日期1:" + formatDateTime1);
String formatDateTime2 = nowDateTime.format(formatter);
System.out.println("格式化后的当前日期1:" + formatDateTime2);

}

/*计算日期差*/
@Test
public void test_compute_date_period() {
LocalDate today = LocalDate.now();
System.out.println("当前日期:" + today);
LocalDate birthday = LocalDate.of(1991, 11, 17);
System.out.println("生日日期:" + birthday);
/*第二个参数减去第一个参数*/
Period period = Period.between(birthday, today);
/*这块得注意下,不是println()方法*/
System.out.printf("年龄:%d 年 %d 月 %d 日", period.getYears(), period.getMonths(), period.getDays());
}

/*提供了使用基于时间的值测量时间量的方法,也可用于 Instant 之间的比较*/
@Test
public void test_compute_date_time_duration() {
LocalDateTime today = LocalDateTime.now();
System.out.println("当前日期时间:" + today);
LocalDateTime birthday = LocalDateTime.of(1991, 11, 17, 10, 10, 10);
Duration between = Duration.between(birthday, today);
System.out.println("相差的天数 :" + between.toDays());
System.out.println("相差的小时数:" + between.toHours());
System.out.println("相差的分钟数:" + between.toMinutes());
System.out.println("相差的秒数:" + between.toMillis());
}

/*ChronoUnit类可用于在单个时间单位内测量一段时间,这个工具类是最全的了,可以用于比较所有的时间单位*/
@Test
public void test_compute_date_time_by_common_api() {
LocalDateTime today = LocalDateTime.now();
System.out.println(today);
LocalDateTime birthday = LocalDateTime.of(1991, 10, 17, 10, 50, 30);
System.out.println(birthday);
System.out.println("开始计算...");
System.out.println("相差的年数:" + ChronoUnit.YEARS.between(birthday, today));
System.out.println("相差的月数:" + ChronoUnit.MONTHS.between(birthday, today));
System.out.println("相差的天数:" + ChronoUnit.DAYS.between(birthday, today));
System.out.println("相差的时数:" + ChronoUnit.HOURS.between(birthday, today));
System.out.println("相差的分数:" + ChronoUnit.MINUTES.between(birthday, today));

System.out.println("*************************秒...单位计算如上所示,这种api是一种通用的做法*************************");
}

}
posted on 2019-09-25 23:27  it_zhangwl  阅读(186)  评论(0编辑  收藏  举报