// input format: dd/MM/yy
SimpleDateFormat parser = new SimpleDateFormat("dd/MM/yy");
// output format: yyyy-MM-dd
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(formatter.format(parser.parse("12/1/20"))); // 0020-11-01
我正在使用上面的代码,但它给我的年份为“0020”而不是“2020”。
最佳答案
使用java.time
为此:
public static void main(String[] args) {
String dateString = "12/1/20";
LocalDate localDate = LocalDate.parse(dateString, DateTimeFormatter.ofPattern("dd/M/yy"));
System.out.println(localDate.format(DateTimeFormatter.ISO_LOCAL_DATE));
}
输出为
2020-01-12
关注金额M
在模式中,您无法解析 String
使用 double M
包含月份的一位数在这里。
关于java - 将年份字段的日期转换为两位数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60393945/