如何在java中将日期格式从YYMMDD更改为YYYY-MM-DD?
问题描述:
我从机器可读代码中获取日期格式为YYMMDD,如何将其更改为YYYY-MM-DD?
例如我得到871223(YYMMDD)
我将魔杖变成1987-12-23(YYYY-MM-DD)
有可能吗?
有人可以帮我提供一些示例代码吗?
I am getting the date format from the machine readable code as YYMMDD, how can i change it into YYYY-MM-DD ? for example am getting 871223(YYMMDD) i wand to change it into 1987-12-23(YYYY-MM-DD) is it possible ? can anyone help me with some sample codes?
答
代码如下:
SimpleDateFormat fromUser = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
String reformattedStr = myFormat.format(fromUser.parse(inputString));
} catch (ParseException e) {
e.printStackTrace();
}