Bootstrap

java 中日期时间戳之间的转换与截取 LocalDateTime

代码

        String fileName="20200624_08.csv";
        long Time=DateUtil.parse(fileName.split("\\.")[0], "yyyyMMdd_HH").getTime();
        System.out.println(Time+"  ===");

        DateTime yyyyMMdd_hh = DateUtil.parse(fileName.split("\\.")[0], "yyyyMMdd_HH");
        System.out.println(yyyyMMdd_hh+" ---");

        String s = fileName.split("\\.")[0];
        System.out.println(s+" ***");

        String[] split = fileName.split("\\.");
        for (String ss:split) {
            System.out.println(ss+" &&&");
        }

运行结果

1592956800000  ===
2020-06-24 08:00:00 ---
20200624_08 ***
20200624_08 &&&
csv &&&

java获取当前年月日

根据日期(天数)获取 相差多少年 多少天 LocalDate类型

    @ApiModelProperty(value = "出生日期")
    private LocalDateTime birthDate;

	//LocalDateTime  转换成 LocalDate 
	pet.getBirthDate().toLocalDate())

		// 创建两个日期对象
//		LocalDate date1 = LocalDate.of(2022, 1, 1);
//		LocalDate date2 = LocalDate.of(2023, 3, 15);

		LocalDate date1 = LocalDate.of(2021, 2, 14);
		LocalDate date2 = LocalDate.of(2023, 3, 15);

		// 计算相差的年数和天数
		Period period = Period.between(date1, date2);
		int years = period.getYears();
		int days = period.getMonths() * 30 + period.getDays(); // 将月份转换为天数
//		int dayOfMonth = date1.getDayOfMonth();

		// 输出结果
		System.out.println("相差年数: " + years);
		System.out.println("相差天数: " + days);
//		System.out.println("相差天数: " + dayOfMonth);
//		System.out.println("相差天数: " +  period.getMonths());
//		System.out.println("相差天数: " +  period.getDays());

		//计算两个时间相差多少天
		LocalDateTime dateTime = LocalDateTime.now();
		LocalDateTime of = LocalDateTime.of(2021, 11, 20, 15, 34, 34);

		long l = Duration.between(of, dateTime).toDays();
		System.out.println(l+ "天");

时间转换

	 @ApiModelProperty(value = "使用有效期")
	  private LocalDateTime availableDate;

	//返回格式 ××××年××月××日
	DateUtils.format(card.getAvailableDate(), PatternConstant.DAY_PATTERN)

	public static final String DAY_PATTERN = "yyyy年MM月dd日";

    public static String format(LocalDateTime localDateTime,String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return formatter.format(localDateTime);
    }



这辈子坚持与不坚持都不可怕,怕的是独自走在坚持的道路上!!!

;