Samsung J7 returning first day of week as 2

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=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]

It says firstDayOfWeek = 2 , for rest of the devices it shows as 1. So any ideas for the solution? Thanks.


Ok, I got the solution. Instead of using getFirstDayOfWeek() I passed 1 as parameter. So its working properly now.

 _calendar.set(year, (month - 1), 1);

in place of

_calendar.set(year, (month - 1), _calendar.getFirstDayOfWeek());

tl;dr

  • 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.
  • For first day of the week:

    LocalDate.now( ZoneId.of( "America/Montreal" ) )
             .with( TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY ) ) 
    

    For first day of the month:

    LocalDate.now( ZoneId.of( "Pacific/Auckland" ) )
             .with( ChronoField.DAY_OF_MONTH , 1L )
    

    Details

    getFirstDayOfWeek() to retrieve first day of every month

    You seem to be confusing first of the week with first of the month.

    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.

    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 .

    Avoid legacy date-time classes

    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.

    Using java.time

    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.

    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 );
    

    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 ) ) ;
    

    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 ) ;
    

    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 .

    The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

    To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

    Where to obtain the java.time classes?

  • 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 SE 6 and Java SE 7
  • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport .
  • Android
  • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
  • See How to use ThreeTenABP….
  • 链接地址: http://www.djcxy.com/p/18706.html

    上一篇: 触发器中的MySQL错误“NEW”中的未知列“”

    下一篇: 三星J7返回第一天为2