public static void main(String[] args) {
// input value
String input = "5-Feb-2000";
// the DateTimeFormatter to be used as a parser
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("d-MMM-uuuu", Locale.ENGLISH);
// use the parser to get a LocalDate from the input String
LocalDate localDate = LocalDate.parse(input, dtf);
// print its toString() (implicitly)
System.out.println(localDate);
// print the day of month
System.out.println("day of month: " + localDate.getDayOfMonth());
// print the month of year
System.out.println("month of year: " + localDate.getMonthValue());
// print the year
System.out.println("year: " + localDate.getYear());
}
输出:
2000-02-05
day of month: 5
month of year: 2
year: 2000
2条答案
按热度按时间iaqfqrcu1#
普通
java.time
...获取所需值的一种简单方法是,使用支持该格式的
DateTimeFormatter
解析String
,并直接访问结果LocalDate
中的值。输出:
如果你有一个 *birthday字段,它有3个不同的字段,分别是日期,月份,年份 *,你可以使用我的示例代码的最后三个print语句中调用的方法,取它们的值并插入到需要的地方...
q3qa4bjr2#
尝试将String值解析为Date对象,将Date对象解析为LocateDate。LocalDate对象包含日期、月份、年份字段...