java – 如何知道Locale是使用12或24小时格式?
发布时间:2020-12-15 03:06:17 所属栏目:Java 来源:网络整理
导读:参见英文答案 How do I find out whether a locale uses 12 or 24 hour time in Java?3个 我想知道,从Locale,我是否必须使用24或12格式. 我找到了这个: if (android.text.format.DateFormat.is24HourFormat(getApplicationContext())) 但是这会根据系统返回
参见英文答案 >
How do I find out whether a locale uses 12 or 24 hour time in Java?3个
我想知道,从Locale,我是否必须使用24或12格式. 我找到了这个: if (android.text.format.DateFormat.is24HourFormat(getApplicationContext())) 但是这会根据系统返回一个布尔值,而不是Locale.这意味着如果用户使用英语为应用程序但使用24小时格式的手机,它将返回true … 我怎样才能得到一个布尔值告诉我使用的格式是否是24小时?我不是在使用Joda.提前致谢. 编辑似乎有些人认为这是重复的,我已经阅读了你提供的帖子作为重复标志的来源.它只能通过近似的变通方法或提供日期的方法来解决,大部分时间都是当前的,即使它是相应的格式. 你可以猜到,如果我想要一种方法来知道我是否应该根据Locale使用12小时格式,这意味着我不需要日期或任何东西,我已经拥有,但是确认我必须使用的格式. 解决方法
看一下实际的android.text.format.DateFormat.is24HourFormat方法的
source code,您将看到它们是如何做到的:
Locale locale = context.getResources().getConfiguration().locale; java.text.DateFormat natural = java.text.DateFormat.getTimeInstance( java.text.DateFormat.LONG,locale); if (natural instanceof SimpleDateFormat) { SimpleDateFormat sdf = (SimpleDateFormat) natural; String pattern = sdf.toPattern(); if (pattern.indexOf('H') >= 0) { value = "24"; } else { value = "12"; } } else { value = "12"; } 因此,您可以根据您获取区域设置的方式进行调整. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |