How to get Locale from its String representation in Java?

前端 未结 12 1048
慢半拍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:16

    Option 1 :

    org.apache.commons.lang3.LocaleUtils.toLocale("en_US")
    

    Option 2 :

    Locale.forLanguageTag("en-US")
    

    Please note Option 1 is "underscore" between language and country , and Option 2 is "dash".

提交回复
热议问题