亚马逊电商网站,网站建设属什么费用,网站域没到期不能续费吗,广东建设网证件查询文章目录 1. LocalDate2. LocalTime3. LocalDateTime3.1创建 LocalDateTime3.2 LocalDateTime获取方法 4. LocalDateTime转换方法4.1 LocalDateTime增加或者减少时间的方法4.2 LocalDateTime修改方法 5. Period6. Duration7. 格式转换7.1 时间日期转换为字符串7.2 字符串转换为… 文章目录 1. LocalDate2. LocalTime3. LocalDateTime3.1创建 LocalDateTime3.2 LocalDateTime获取方法 4. LocalDateTime转换方法4.1 LocalDateTime增加或者减少时间的方法4.2 LocalDateTime修改方法 5. Period6. Duration7. 格式转换7.1 时间日期转换为字符串7.2 字符串转换为时间日期 8. LocalDateTime在SpringBoot中的应用8.1 将LocalDateTime字段以时间戳的方式返回给前端 添加日期转化类8.2 将LocalDateTime字段以指定格式化日期的方式返回给前端8.3 对前端传入的日期进行格式化8.4 前后端日期时间转化问题 其他 1. LocalDate
LocalDate now LocalDate.now();
System.out.println(now);//2023-04-07
System.out.println(now.getYear());//2023
System.out.println(now.getMonthValue());//4
System.out.println(now.getDayOfMonth());//7
System.out.println(now.getDayOfWeek());//FRIDAY
System.out.println(now.getDayOfWeek().getValue());//52. LocalTime
LocalTime now LocalTime.now();
System.out.println(now);//08:43:06.518
System.out.println(now.getHour());//8
System.out.println(now.getMinute());//43
System.out.println(now.getSecond());//373. LocalDateTime 3.1创建 LocalDateTime
// 获取当前系统时间(2022-11-30T16:26:13.158)
LocalDateTime now LocalDateTime.now();// 指定年月日和时分秒初始化(2022-11-30T10:00:15)
LocalDateTime localDateTime LocalDateTime.of(2022, 11, 30, 10, 00, 15);3.2 LocalDateTime获取方法
获取年
System.out.println(localDateTime.getYear()); // 2022获取月
System.out.println(localDateTime.getMonthValue()); // 11获取日
System.out.println(localDateTime.getDayOfYear()); // 334
System.out.println(localDateTime.getDayOfMonth()); //30
System.out.println(localDateTime.getDayOfWeek()); // WEDNESDAY获取小时和分钟
System.out.println(now.getHour()); //16
System.out.println(now.getMinute()); //344. LocalDateTime转换方法
转换成为一个LocalDate对象
LocalDate localDate now.toLocalDate(); // 2022-11-30转换成为一个LocalTime对象
LocalTime localTime now.toLocalTime(); // 16:36:15.903LocalDateTime now LocalDateTime.now();
System.out.println(now);//2023-04-07T08:44:59.799System.out.println(now.toLocalDate());2023-04-07
System.out.println(now.toLocalTime());//08:44:59.7994.1 LocalDateTime增加或者减少时间的方法
LocalDate**、LocalTime、LocalDateTime、**Instant 为不可变对象修改这些对象对象会返回一个副本
LocalDateTime localDateTime LocalDateTime.of(2022, 11, 30, 13, 14, 52);
//2022-11-30T13:14:52
System.out.println(localDateTime);
//2023-11-30T13:14:52
System.out.println(localDateTime.plusYears(1));
//2022-11-30T13:15:52
System.out.println(localDateTime.plusMinutes(1));
//2022-12-01T13:14:52
System.out.println(localDateTime.plusDays(1));
//2022-11-30T14:14:52
System.out.println(localDateTime.plusHours(1));
//2022-11-30T13:15:52
System.out.println(localDateTime.plusMinutes(1));
//2022-11-30T13:14:53
System.out.println(localDateTime.plusSeconds(1));
//2022-12-07T13:14:52
System.out.println(localDateTime.plusWeeks(1));4.2 LocalDateTime修改方法
方法名说明public LocalDateTime withYear(int year)直接修改年public LocalDateTime withMonth(int month)直接修改月public LocalDateTime withDayOfMonth(int dayofmonth)直接修改日期(一个月中的第几天)public LocalDateTime withDayOfYear(int dayOfYear)直接修改日期(一年中的第几天)public LocalDateTime withHour(int hour)直接修改小时public LocalDateTime withMinute(int minute)直接修改分钟public LocalDateTime withSecond(int second)直接修改秒 5. Period
Period:用于计算两个“日期”间隔 。 Period 定义了日期间隔通过 Period.between 得到了两个 LocalDate 的差返回的是两个日期差几年零几月零几天。如果希望得知两个日期之间差几天直接调用 Period 的 getDays() 方法得到的只是最后的“零几天”而不是算总的间隔天。
LocalDate now LocalDate.now();
System.out.println(now);//2023-04-07
System.out.println(Period.between(now, LocalDate.of(2021, 12, 24)));//P-1Y-3M-14D
System.out.println(Period.between(now, LocalDate.of(2021, 12, 24)).getDays());//-14System.out.println(Period.between(now, LocalDate.of(2024, 12, 24)));//P1Y8M17D
System.out.println(Period.between(now, LocalDate.of(2025, 12, 24)).getDays());//17API
方法名说明public static Period between(开始时间,结束时间)计算两个“时间的间隔public int getYears()获得这段时间的年数public int getMonths()获得此期间的总月数public int getDays()获得此期间的天数public long toTotalMonths()获取此期间的总月数 6. Duration
Duration:用于计算两个“时间”间隔。
方法名说明public static Durationbetween(开始时间,结束时间)计算两个“时间的间隔public long toSeconds()获得此时间间隔的秒public int toMillis()获得此时间间隔的毫秒public int toNanos()获得此时间间隔的纳秒 7. 格式转换 7.1 时间日期转换为字符串
LocalDate localDate LocalDate.of(2020, 10, 10);
String s1 localDate.format(DateTimeFormatter.BASIC_ISO_DATE); //20201010
String s2 localDate.format(DateTimeFormatter.ISO_LOCAL_DATE); //2020-10-10DateTimeFormatter默认提供了多种格式化方式如果默认提供的不能满足要求可以通过DateTimeFormatter的ofPattern方法创建自定义格式化方式
LocalDateTime now LocalDateTime.now();
System.out.println(now);//2023-04-07T09:23:27.606
System.out.println(now.format(DateTimeFormatter.BASIC_ISO_DATE));//20230407
System.out.println(now.format(DateTimeFormatter.ISO_LOCAL_DATE));//2023-04-07
System.out.println(now.format(DateTimeFormatter.ISO_DATE_TIME));//2023-04-07T09:23:27.606DateTimeFormatter dateTimeFormatter DateTimeFormatter.ofPattern(yyyy-MM-dd HH:mm:ss);
System.out.println(now.format(dateTimeFormatter));//2023-04-07 09:24:307.2 字符串转换为时间日期
DateTimeFormatter formatter DateTimeFormatter.ofPattern(yyyy-MM-dd HH:mm:ss);
LocalDateTime localDateTime LocalDateTime.parse(2023-04-07 10:10:10, formatter);
System.out.println(localDateTime);//2023-04-07T10:10:10
DateTimeFormatter formatter2 DateTimeFormatter.ofPattern(yyyy-MM-dd);
LocalDate localDate LocalDate.parse(2023-04-09, formatter2);
System.out.println(localDate);//2023-04-098. LocalDateTime在SpringBoot中的应用 8.1 将LocalDateTime字段以时间戳的方式返回给前端 添加日期转化类
public class LocalDateTimeConverter extends JsonSerializerLocalDateTime { 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; 8.2 将LocalDateTime字段以指定格式化日期的方式返回给前端
在LocalDateTime 字段上添加JsonFormat(shapeJsonFormat.Shape.STRING, pattern“yyyy-MM-dd HH:mm:ss”) 注解即可如下
JsonFormat(shapeJsonFormat.Shape.STRING, patternyyyy-MM-dd HH:mm:ss)
protected LocalDateTime gmtModified; 8.3 对前端传入的日期进行格式化
在LocalDateTime字段上添加DateTimeFormat(pattern “yyyy-MM-dd HH:mm:ss”) 注解即可如下
DateTimeFormat(pattern yyyy-MM-dd HH:mm:ss)
protected LocalDateTime gmtModified;8.4 前后端日期时间转化问题
在实体类上加DatetimeFormat与JsonFormat注解
DatetimeFormat 将前台日期字符串转换成Date格式 DateTimeFormat(patternyyyy-MM-dd)
JsonFormat 将服务器端Date日期转换成指定字符串格式 JsonFormat(patternyyyy-MM-dd,timezoneGMT8)
两个需要同时加否则会有时区的问题 其他
获取两个时间之间的间隔天-时-分
/*** 创建时间: 2023/4/23* 函数名称:* 函数功能: 获取两个时间之间的 范围* 函数参数:* * param begin: 开始* * param end: 结束* return: String* author: Snow******************************************************** 修改记录(时间--修改人--修改说明):*/
public static String getTwoDateTime(LocalDateTime begin, LocalDateTime end){end end null ? LocalDateTime.now() : end;String durationTime;Duration duration Duration.between(begin, end);// 秒long seconds duration.getSeconds();// 天int days (int)(seconds / DAY_SECONDS);// 总秒数 - 天数后剩余的秒数int remainderSeconds (int)(seconds % DAY_SECONDS);// 小时0int hours remainderSeconds / 3600;// 剩余秒数remainderSeconds remainderSeconds % 3600;int minutes remainderSeconds / 60;durationTime (days 0 ? days 天 : ) (hours 0 ? hours 时 : ) (minutes 0 ? minutes 分钟 : 1分钟);return durationTime;
}