三星J7返回每周的第一天为2 [英] Samsung J7 returning first day of week as 2
问题描述
我编写了用于自定义日历的代码,其中使用了日历对象。我使用了getFirstDayOfWeek()方法来检索每月的第一天。每月加载时,方法 _calendar.getTime()返回第一个日期。在每个设备上都可以正确返回。但是在三星J7上,它返回的起始日期为2。这是我的三星J7调试器日志
I have written a code for custom calendar where I am using calendar object.I have used the method getFirstDayOfWeek() to retrieve first day of every month.On loading every month the method "_calendar.getTime()" returns the first date. On every device it is returning correctly.But on samsung J7 it returns starting date of week as 2 . Here is my debugger log for samsung J7
java.util.GregorianCalendar [time = ?, areFieldsSet = false,lenient = true,zone =亚洲/ Calcutta,firstDayOfWeek = 2,minimumDaysInFirstWeek = 4,ERA = 1,YEAR = 2016,MONTH = 10,WEEK_OF_YEAR = 41,WEEK_OF_MONTH = 3,DAY_OF_MONTH = 14,DAY_OF_YEAR = 288,DAY_OF_WEEK = 6,DAY_OF_WEEK_IN_MONTH 1,HOUR = 3,HOUR_OF_DAY = 15,MINUTE = 46,SECOND = 58,MILLISECOND = 199,ZONE_OFFSET = 19800000,DST_OFFSET = 0]
java.util.GregorianCalendar[time=?,areFieldsSet=false,lenient=true,zone=Asia/Calcutta,firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2016,MONTH=10,WEEK_OF_YEAR=41,WEEK_OF_MONTH=3,DAY_OF_MONTH=14,DAY_OF_YEAR=288,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=3,HOUR_OF_DAY=15,MINUTE=46,SECOND=58,MILLISECOND=199,ZONE_OFFSET=19800000,DST_OFFSET=0]
对于其他显示为1的设备,它说firstDayOfWeek = 2,那么该解决方案有什么想法?
谢谢。
It says firstDayOfWeek = 2 , for rest of the devices it shows as 1. So any ideas for the solution? Thanks.
推荐答案
tl; dr
- 您似乎将月初与一周的第一天混为一谈。
-
2
是表示星期一的硬编码常量。 - 显然,您当前的默认语言环境将星期一视为一周的第一天。
- 改为使用java.time。
- You seem to conflate first-of-month with first-day-of-week.
- The
2
is a hard-coded constant representing Monday. - Apparently your current default locale considers Monday to be the first day of the week.
- Use java.time instead.
tl;dr
一周的第一天:
LocalDate.now( ZoneId.of( "America/Montreal" ) )
.with( TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY ) )
每月第一天:
LocalDate.now( ZoneId.of( "Pacific/Auckland" ) )
.with( ChronoField.DAY_OF_MONTH , 1L )
< h1>详细信息
Details
getFirstDayOfWeek()检索每月的第一天
getFirstDayOfWeek() to retrieve first day of every month
您似乎将一周的第一天和一月的第一天混淆了。
You seem to be confusing first of the week with first of the month.
当然,本月的第一天总是 1
。
The first of the month is always 1
of course.
一周的第一天是星期几,例如星期日或星期一等。 日历
类有所不同,具体取决于语言环境
。例如,在北美大部分地区,第一天通常是星期日。在欧洲大部分地区,一周的第一天是星期一。
The first of the week is a day-of-week such as Sunday or Monday etc. The definition of the first day of the week in the Calendar
class varies, depending on the Locale
. For example, in much of North America, the first day is Sunday commonly. In much of Europe, the first day of the week is Monday.
如果您未指定 Locale
,则 Calendar
类隐式应用JVM的当前默认语言环境。显然,在默认语言环境中,一周的第一天是星期一。我推断是因为您报告了数字 2
。如果您浏览 int
常量 Calendar.MONDAY
,您发现这确实是值<$ c的原始 int
$ c> 2 。
If you fail to specify a Locale
, the Calendar
class implicitly applies the JVM’s current default locale. Apparently in your default locale the first day of the week is Monday. I deduce that because you report the number 2
. If you explore the int
constant Calendar.MONDAY
, you find it is indeed a primitive int
of value 2
.
Calendar
类的设计决策很多。我认为对星期几的这种不同定义就是其中之一。避免这些麻烦的旧日期时间类的众多原因之一,例如 Calendar
和 Date
。这些类现在已被遗留,由java.time类取代。
The Calendar
class has many poor design decisions. I consider this varying definition of day-of-week to be one of them. One of many reasons to avoid these troublesome old date-time classes such as Calendar
and Date
. These classes are now legacy, supplanted by the java.time classes.
a href = https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html rel = nofollow noreferrer> LocalDate
类表示没有日期和时区的仅日期值。
The LocalDate
class represents a date-only value without time-of-day and without time zone.
时区对于确定日期至关重要。在任何给定时刻,日期都会在全球范围内变化。例如,在法国巴黎午夜过后的几分钟,虽然仍然是昨天中魁北克省。
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still "yesterday" in Montréal Québec.
以 continent格式指定适当的时区名称 / region
,例如 America / Montreal
, 非洲/ Casablanca
或 Pacific / Auckland
。请勿使用3-4个字母的缩写,例如 EST
或 IST
,因为它们不是 真实时区,不是标准化的,甚至不是唯一的(!)。
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
要获取一周的第一天,(a)确定一周的第一天是什么为您(b)使用 <$在 TemporalAdjusters
以获取特定 DayOfWeek
枚举对象。
To get the first day of the week, (a) decide what is the first day of the week for you, (b) use a TemporalAdjuster
implementation defined in TemporalAdjusters
to get the date for a specific DayOfWeek
enum object.
DayOfWeek firstDow = DayOfWeek.MONDAY ;
LocalDate ld = today.with( TemporalAdjusters.previousOrSame( firstDow ) ) ;
要在某天获得 LocalDate
每月,调用 with
方法并从 ChronoField.DAY_OF_MONTH
。
To get a LocalDate
for a certain day of month, call the with
method and pass an enum object from ChronoField.DAY_OF_MONTH
.
LocalDate firstOfMonth = today.with( ChronoField.DAY_OF_MONTH , 1L ) ;
关于java.time
java.time 框架内置于Java 8及更高版本中。这些类取代了麻烦的旧旧版日期时间类,例如 java.util.Date
, Calendar
,& SimpleDateFormat
。
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
现在处于维护模式的Joda-Time 项目建议迁移到 java.time 类。
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
要了解更多信息,请参见 Oracle教程。并在Stack Overflow中搜索许多示例和说明。规范为 JSR 310 。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
从哪里获取java.time类?
Where to obtain the java.time classes?
- Java SE 8 , Java SE 9 ,然后是
- 内置。
- 具有捆绑实现的标准Java API的一部分。
- Java 9添加了一些次要功能和修复。
- Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- 许多java.time功能都反向移植到Java 6& 7在> ThreeTen-Backport 中。
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- ThreeTenABP 项目专为Android改编了 ThreeTen-Backport (如上所述)。
- 请参阅 如何使用ThreeTenABP… 。
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
- See How to use ThreeTenABP….
这篇关于三星J7返回每周的第一天为2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!