从包含所有必要字段的另一个部分构造一个部分的最佳方法是什么(例如,来自 LocalDate
的 YearMonth
?我能看到的一种方法是转换为完整的瞬间和返回,即
YearMonth ld2ym(LocalDate ld) {
return new YearMonth(ld.toDateTime(DateTime.now()));
}
但似乎应该有一种更有效的方法。
最佳答案
在 YearMonth
类上,我们可以找到这个方法,它似乎给出了适当的解决方案:
/**
* Parses a {@code YearMonth} from the specified string using a formatter.
*
* @param str the string to parse, not null
* @param formatter the formatter to use, not null
* @since 2.0
*/
public static YearMonth parse(String str, DateTimeFormatter formatter) {
LocalDate date = formatter.parseLocalDate(str);
return new YearMonth(date.getYear(), date.getMonthOfYear());
}
关于java - 缩小 Joda-Time 部分的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9076481/