2020-07-27 java8日期类型&springboot中
java8LocalDate\LocalDateTime\LocalTime简介
/** LocalDate只会获取年月日 */
/** 创建LocalDate */
//获取当前年月日
LocalDate localDate = LocalDate.now();
//构造指定的年月日
LocalDate localDate1 = LocalDate.of(2019, 9, 10);
/**获取年、月、日、星期几*/
int year = localDate.getYear();
int year1 = localDate.get(ChronoField.YEAR);
Month month = localDate.getMonth();
int month1 = localDate.get(ChronoField.MONTH_OF_YEAR);
int day = localDate.getDayOfMonth();
int day1 = localDate.get(ChronoField.DAY_OF_MONTH);
DayOfWeek dayOfWeek = localDate.getDayOfWeek();
int dayOfWeek1 = localDate.get(ChronoField.DAY_OF_WEEK);
/** LocalTime只会获取几点几分几秒 */
/**创建LocalTime*/
LocalTime localTime = LocalTime.of(13, 51, 10);
LocalTime localTime1 = LocalTime.now();
/**获取时分秒*/
//获取小时
int hour = localTime.getHour();
int hour1 = localTime.get(ChronoField.HOUR_OF_DAY);
//获取分
int minute = localTime.getMinute();
int minute1 = localTime.get(ChronoField.MINUTE_OF_HOUR);
//获取秒
int second = localTime.getSecond();
int second1 = localTime.get(ChronoField.SECOND_OF_MINUTE);
/**LocalDateTime获取年月日时分秒,等于LocalDate+LocalTime*/
/**创建LocalDateTime*/
LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime localDateTime1 = LocalDateTime.of(2019, Month.SEPTEMBER, 10, 14, 46, 56);
LocalDateTime localDateTime2 = LocalDateTime.of(localDate, localTime);
LocalDateTime localDateTime3 = localDate.atTime(localTime);
LocalDateTime localDateTime4 = localTime.atDate(localDate);
/**获取LocalDate*/
LocalDate localDate2 = localDateTime.toLocalDate();
/**获取LocalTime*/
LocalTime localTime2 = localDateTime.toLocalTime();
/**获取秒数 创建Instant对象*/
Instant instant = Instant.now();
/**获取秒数*/
long currentSecond = instant.getEpochSecond();
/**获取毫秒数*/
long currentMilli = instant.toEpochMilli();
// 个人觉得如果只是为了获取秒数或者毫秒数,使用System.currentTimeMillis() 来得更为方便
// 修改LocalDate、LocalTime、LocalDateTime、Instant
// LocalDate、LocalTime、LocalDateTime、Instant为不可变对象,修改这些对象对象会返回一个副本
// 增加、减少年数、月数、天数等 以LocalDateTime为例
LocalDateTime localDateTime5 = LocalDateTime.of(2019, Month.SEPTEMBER, 10,
14, 46, 56);
//增加一年
localDateTime = localDateTime.plusYears(1);
localDateTime = localDateTime.plus(1, ChronoUnit.YEARS);
//减少一个月
localDateTime = localDateTime.minusMonths(1);
localDateTime = localDateTime.minus(1, ChronoUnit.MONTHS);
/**通过with修改某些值*/
//修改年为2019
localDateTime = localDateTime.withYear(2020);
//修改为2022
localDateTime = localDateTime.with(ChronoField.YEAR, 2022);
/**还可以修改月、日
时间计算
比如有些时候想知道这个月的最后一天是几号、下个周末是几号,通过提供的时间和日期API可以很快得到答案*/
LocalDate localDate3 = LocalDate.now();
LocalDate localDate4 = localDate.with(firstDayOfYear());
// 比如通过firstDayOfYear() 返回了当前日期的第一天日期,还有很多方法这里不在举例说明格式化时间
LocalDate localDate5 = LocalDate.of(2019, 9, 10);
String s1 = localDate.format(DateTimeFormatter.BASIC_ISO_DATE);
String s2 = localDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
//自定义格式化
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String s3 = localDate.format(dateTimeFormatter);
// DateTimeFormatter默认提供了多种格式化方式,如果默认提供的不能满足要求,可以通过DateTimeFormatter的
// ofPattern方法创建自定义格式化方式解析时间
LocalDate localDate6 = LocalDate.parse("20190910", DateTimeFormatter.BASIC_ISO_DATE);
LocalDate localDate7 = LocalDate.parse("2019-09-10", DateTimeFormatter.ISO_LOCAL_DATE);
// 和SimpleDateFormat相比,DateTimeFormatter是线程安全的
// 两个日期相差多长时间
System.out.println("计算两个时间的差:");
LocalDateTime now = LocalDateTime.now();
LocalDateTime end = LocalDateTime.now();
Duration duration = Duration.between(now, end);
long days = duration.toDays(); //相差的天数
long hours = duration.toHours();//相差的小时数
long minutes = duration.toMinutes();//相差的分钟数
long millis = duration.toMillis();//相差毫秒数
long nanos = duration.toNanos();//相差的纳秒数
System.out.println(now);
System.out.println(end);
System.out.println("发送短信耗时【 " + days +"天:" + hours +" 小时:" + minutes +" 分钟:" + millis +" 毫秒:" + nanos +" 纳秒】");
SpringBoot中应用LocalDateTime
1.将LocalDateTime字段以时间戳的方式返回给前端
// 添加日期转化类
public class LocalDateTimeConverter extends JsonSerializer<LocalDateTime> {
@Override
public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeNumber(value.toInstant(ZoneOffset.of("+8")).toEpochMilli());
}
}
并在LocalDateTime字段上添加@JsonSerialize(using = LocalDateTimeConverter.class)注解,如下:
@JsonSerialize(using = LocalDateTimeConverter.class)
protected LocalDateTime gmtModified;
2.将LocalDateTime字段以指定格式化日期的方式返回给前端
在LocalDateTime字段上添加@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")注解即可,如下:
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
protected LocalDateTime gmtModified;
3.对前端传入的日期进行格式化
在LocalDateTime字段上添加@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")注解即可,如下:
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
protected LocalDateTime gmtModified;