【发布时间】:2019-02-25 13:46:36
【问题描述】:
我有一个限制月份和年份的字符串,例如:2019-02,我需要转换两个 LocalDateTime。我需要 02 月的第一天和最后一天。
【问题讨论】:
标签: java string converters localdate
我有一个限制月份和年份的字符串,例如:2019-02,我需要转换两个 LocalDateTime。我需要 02 月的第一天和最后一天。
【问题讨论】:
标签: java string converters localdate
使用YearMonth类:
YearMonth yearMonth = YearMonth.parse("2019-02");
LocalDate startDate = yearMonth.atDay(1); // 2019-02-01
LocalDate endDate = yearMonth.atEndOfMonth(); // 2019-02-28
然后您可以转换为LocalDateTime,例如如果您需要一天的开始时间:
LocalDateTime startDayTime = startDate.atStartOfDay(); // 2019-02-01T00:00
【讨论】: