date日期转为localDateTime

//获取业务中的日期

  SimpleDateFormat _yearToMonth = new SimpleDateFormat("yyyyMM");
        Date parse = null;
        try {
            parse = _yearToMonth.parse("202103");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(parse);
            calendar.add(Calendar.MONTH, -1);
            parse = calendar.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        LocalDateTime localDateTime = parse.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
        LocalDateTime lastDay = localDateTime.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX);

        System.out.println("Date = " + parse);
        System.out.println("LocalDateTime = " + localDateTime);
        System.out.println("lastDay = " + lastDay);

首先将日期转为date类型:parse = _yearToMonth.parse("202103");

data类型再转为localDateTime类型:LocalDateTime localDateTime = parse.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDateTime();

获取当前月份最后的日期最后的时间点 localDateTime.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX);

localDateTime.with(TemporalAdjusters.lastDayOfMonth())表示当前月的最后一天.with(LocalTime.MAX);表示当前月的23:59:59

 


版权声明:本文为TianmingQi94原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。