How to get Locale from its String representation in Java?

前端 未结 12 1042
慢半拍i
慢半拍i 2020-12-08 00:06

Is there a neat way of getting a Locale instance from its \"programmatic name\" as returned by Locale\'s toString() method? An obvious and ugly solution would b

12条回答
  •  眼角桃花
    2020-12-08 00:38

    There doesn't seem to be a static valueOf method for this, which is a bit surprising.

    One rather ugly, but simple, way, would be to iterate over Locale.getAvailableLocales(), comparing their toString values with your value.

    Not very nice, but no string parsing required. You could pre-populate a Map of Strings to Locales, and look up your database string in that Map.

提交回复
热议问题