将日期格式化为本地设置,但仅限于月和日

And*_*Dev 6 android date

我的日期是格式为“2013-12-31”的字符串。我想根据用户的设备设置将其转换为本地日期,但只显示月份和日期。因此,如果用户的设备设置为德语,则日期应转换为“31.12”。在德国,首先是日,然后是月。我不希望包括年份。

And*_*Dev 0

这有效:

  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)