我的日期是格式为“2013-12-31”的字符串。我想根据用户的设备设置将其转换为本地日期,但只显示月份和日期。因此,如果用户的设备设置为德语,则日期应转换为“31.12”。在德国,首先是日,然后是月。我不希望包括年份。
这有效:
String dtStart = "2010-12-31";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse(dtStart);
SimpleDateFormat df = (SimpleDateFormat)
DateFormat.getDateInstance(DateFormat.SHORT);
String pattern = df.toLocalizedPattern().replaceAll(".?[Yy].?", "");
System.out.println(pattern);
SimpleDateFormat mdf = new SimpleDateFormat(pattern);
String localDate = mdf.format(date);
Run Code Online (Sandbox Code Playgroud)