- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
下面的代码在 Nougat 和 pre-Nougat 上给出了不同的结果。看一看,如果你愿意,可以自己尝试一下。如果有人能向我解释原因并提供解决方案,我将不胜感激。
我想要正确的 WEEK_OF_YEAR 值,取决于一周的第一天,适用于所有 Android 版本。我有一个时间表应用程序,我经常使用 gregorianCalendar,所以我不想切换到另一个类/库。
//default first day of the week is Monday for replication. I live in the Netherlands, it's weird.
Locale l = new Locale("nl", "NL");
GregorianCalendar test = new GregorianCalendar(l);
test.set(Calendar.YEAR, 2017);
test.set(Calendar.MONTH, 0);
test.set(Calendar.DAY_OF_MONTH, 29);//this is a Sunday
int week = test.get(Calendar.WEEK_OF_YEAR);//should be 4
test.setFirstDayOfWeek(1);//Set it to Sunday
int week2 = test.get(Calendar.WEEK_OF_YEAR);//should be 5 but is 4 below nougat???
最佳答案
如果您查看 release notes for Nougat您可以看到对 Locales 的支持得到了增强。
特别是,
Prior to Android 7.0, Android could not always successfully match app and system locales.
我也在自己的设备上注意到了这一点。我的手机设置为英语(澳大利亚)语言环境。在牛轧糖之前,
DateTimeFormat.forPattern("dd MMMM").print(new LocalDate(2017,1,29));
会打印 29 Jan
(没有句号/句点),但在 Nougat 之后它会打印 29 Jan.
(有句点)。
虽然我很难给出确切的细节,但您的情况似乎就是这样。后牛轧糖,手机能够更好地匹配应用程序和系统区域设置,包括您的区域设置的一周的第一天。在任何情况下,通过调试器逐步查找根本原因是行不通的,因为调用是在 libcore
内部处理的,而不是在源代码中公开的 Java 类。
如果 Android 设备错误地报告了一年的第一天/一年的第一周,除了解决它之外您几乎无能为力:
if (android.os.Build.VERSION.SDK_INT < 24) {
//pre-Nougat logic
}
else {
//Nougat/post-Nougat logic
}
您也可以尝试使用 enhanced replacement for GregorianCalendar (在 SDK 24 中添加)来解决您的问题。
如果你使用类似 Joda-time 的类或使用新的 JSR-310类(通过 ThreeTen 向后移植),您可能无需绕过 GregorianCalendar
即可获得所需内容。一般来说,这些类更容易使用并且更不容易出错。由于这样的问题,许多开发人员已经放弃了 java.util.Calendar
和 java.util.Date
。请看答案to this canonical question详情
如果您要使用 Joda,则可以使用 LocalDate.fromCalendarFields(test)
将您的 GregorianCalendar
对象转换为 LocalDate
。这些类(class)使用 ISO 标准,其中一周的第一天始终是星期一。然后,您将在它们之上编写您想要的逻辑。然后 GregorianCalendar
的问题将被“隔离”为一个简单的问题,即检索给定区域设置的一年中的第一周。如果您的应用包含对服务器的调用,您可以改为通过 API 调用为一周的第一天提供服务。
更新:
注意时区行为已在 Android O 中更新:
其他语言环境和国际化相关的更改如下:
Time zone name parsing has changed. Previously, Android devices used the system clock value sampled at boot time to cache the time zone names used for parsing date times. As a result, parsing could be negatively affected if the system clock was wrong at boot time or in other, rarer cases. Now, in common cases the parsing logic uses ICU and the current system clock value when parsing time zone names. This change provides more correct results, which may differ from earlier Android versions when your app uses classes like SimpleDateFormat. Android O updates the version of ICU to version 58.
关于android - GregorianCalendar setFirstDayOfWeek 不影响牛轧糖前的 WEEK_OF_YEAR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41883561/
更新: 好的,我似乎找到了一半的答案。如果我使用无参数 getInstance 创建我的日历,我得到 WEEK_OF_YEAR = 52。但是,如果我通过向 getInstance 提供 Local.
我在获取时遇到问题 WEEK_OF_YEAR从日历对象。 by passing date '31-Dec-2014': calendar1.get(Calendar.WEEK_OF_YEAR) - r
我尝试通过下面的代码获取一年中的第几周,但它总是返回“3”,我什至将时区设置为 GMT 但仍然返回相同的值(“3”)。请帮帮我这里 DateFormat formatter = new SimpleD
/* package whatever; // don't place package name! */ import java.text.ParseException; import java.te
这个问题已经有答案了: android how to identify the same days of a week (3 个回答) 已关闭 5 年前。 当我执行下面的代码时,我得到相同的输出。 p
Calendar.WEEK_OF_YEAR 无法正常工作。我的目标是每个星期一都是新的一周。我得到的结果在代码注释中。 我的代码是: Calendar cal = Calendar.getInstan
几天来我一直在努力让它发挥作用,但我已经筋疲力尽了。我想我只需要另一双眼睛来帮助我。 我将“条目”存储在数据库中,每个条目在输入时都有一个 Unix 时间戳。我有一个系统,用户可以在其中选择任何日期并
给定 week_of_year,我如何获取该周的开始和结束日期? 示例:假设日期是 2017 年 1 月 1 日。 Calendar calendar = Calendar.getInstance()
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
下面的代码在 Nougat 和 pre-Nougat 上给出了不同的结果。看一看,如果你愿意,可以自己尝试一下。如果有人能向我解释原因并提供解决方案,我将不胜感激。 我想要正确的 WEEK_OF_YE
例如:给定月份 = 2,并给定年份 = 2014,我使用以下代码获得了周列表。为什么我为月份申请 1 是默认月份仅以索引开始。 示例代码: Calendar calendar = Calendar.g
这个问题在这里已经有了答案: Why dec 31 2010 returns 1 as week of year? (6 个答案) Understanding java.util.Calendar
这个问题在这里已经有了答案: Why dec 31 2010 returns 1 as week of year? (6 个答案) 关闭 4 年前。 我试图了解 java.util.Calendar
要么我不理解方法 getActualMaximum(int) 或字段 WEEK_OF_YEAR,要么涉及 Sun 错误(或所有三个)...有人可以向我解释为什么(至少在德语语言环境中...) 以下代码
这个问题已经有答案了: 奥 git _a (1 个回答) 已关闭 6 年前。 我正在执行以下 Java 代码: Calendar cal = Calendar.getInstance(TimeZone
ISO-8601 标准指出 "The first week of a year is the week that contains the first Thursday of the year (an
Mongodb 的 $week operator州 Takes a date and returns the week of the year as a number between 0 and 53
知道年、年中的星期和星期中的日期,就可以获得年中的月份和月份中的日期。例如 // corresponding to September 15, 2012 if week starts on Mond
尝试查找给定日期的星期几。 使用下面的代码。 [Java 1.7] 问题: 2012 年 12 月 29 日 = 第 52 周 2012 年 12 月 30 日 = 第 1 周 2013 年 12 月
好吧,这个难倒我了。我有以下尝试创建此字符串的代码: OUTPUT: 52. 2012 (24 Dec to 30 Dec) 这是 2012 年第 52 周的开始和结束,星期一是一周的第一天。 pri
我是一名优秀的程序员,十分优秀!