SimpleDateFormat pattern based on locale, but forcing a 4-digit year

前端 未结 4 1609

I need to build a date format like dd/MM/yyyy. It\'s almost like DateFormat.SHORT, but contains 4 year digits.

I try to implement it with

4条回答
  •  感动是毒
    2021-02-15 17:53

    I have similar way to do this, but I need to get the locale pattern for the ui controller.

    So here's the code

                // date format, always using yyyy as year display
            DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, locale);
            SimpleDateFormat simple = (SimpleDateFormat) dateFormat;
            String pattern = simple.toPattern().replaceAll("\\byy\\b", "yyyy");
            System.out.println(pattern);
    

提交回复
热议问题