How do I get localized date pattern string?

前端 未结 10 2276
死守一世寂寞
死守一世寂寞 2020-11-28 05:31

It is quite easy to format and parse Java Date (or Calendar) classes using instance of DateFormat, i.e. I could format current date into short localize date like this:

10条回答
  •  难免孤独
    2020-11-28 06:16

    For those still using Java 7 and older:

    You can use something like this:

    DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
    String pattern       = ((SimpleDateFormat)formatter).toPattern();
    String localPattern  = ((SimpleDateFormat)formatter).toLocalizedPattern();
    

    Since the DateFormat returned From getDateInstance() is instance of SimpleDateFormat. Those two methods should really be in the DateFormat too for this to be less hacky, but they currently are not.

提交回复
热议问题