String转LocalDateTime

    技术2022-07-11  130

    由于java8bug的缘故String转LocalDateTime报错,可以先转成localdate再转为LocalDateTime

    public class Test{ public static void main(String[] args) { /** * 这里根据不同的时间格式选择不同的国别 * 例如中国"01-一月-2020",ofPattern()参数用Locale.CHAINA */ String time = "01-一月-2020"; DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy",Locale.CHINA); LocalDate localDateTime = LocalDate.parse(time,dateTimeFormatter); Date date = Date.from(localDateTime.atStartOfDay(ZoneOffset.ofHours(8)).toInstant()); LocalDateTime localDate = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDateTime(); System.out.println(localDate); }

    输出如下:

    Processed: 0.011, SQL: 9