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?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
The code goes something like this:
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();
}
回答2:
Simple steps to follow and achieve what you want:
for example date is:
String date = "871223";
Create SimpleDateFormat with source pattern
SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
get the date object by parsing the date
Date d1 = sdf.parse(date);
Change the pattern to the target one
sdf.applyPattern("yyyy-MM-dd");
Format as per the target pattern
System.out.println(sdf.format(d1));
Hope this helps.