简体   繁体   中英

Incorrect DAY_OF_WEEK when set calendar

When I get the current date, it returns me 4 as the day of the current week. But when I set the date with calendar.set(mYear, mMonth, mDay); , I get 7 as the current day of the week. I checked that the dates are the same, and everything seems to be the same, but I get a different day.

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
Calendar calendario = Calendar.getInstance();    
Log.w("TAG",""+"********:"+calendario.get(Calendar.DAY_OF_WEEK));
Log.w(4);


mYear = "2012";
mMonth = "10";
mDay = "10";

Calendar calendario = Calendar.getInstance();
calendario.set(mYear, mMonth, mDay);
Log.w("TAG","---------: "+calendario.get(Calendar.DAY_OF_WEEK));
    Log.w(7);

Does anyone have idea of that may be happening?

Thanks!

Your likely problem is that Calendar uses a zero-based index for months . So if you're trying to set your calendar to October , you have to use 9, not 10.

Is this stupid and inconsistent of Calendar? Yes! Use the Joda-Time API for working with dates and times instead. As far as I'm aware, it's currently the de-facto standard until JSR 310 comes around.

int month=9; int year=2012 ; int day=10; calendario.set(2012, 9, 10);

Use 9 for october as months JAN-DEC are triggered as 0-11.

tl;dr

LocalDate.of( 2012 , Month.OCTOBER , 10 ) // 2012-10-10
         .getDayOfWeek()                  // DayOfWeek.WEDNESDAY
         .getValue()                      // 3

java.time

The java.time classes use sane numbering schemes, unlike the troublesome old date-time classes they supplanted.

  • 2018 is year 2018.
  • 1-12 for January-December.
  • 1-7 for Monday-Sunday.

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 .

If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment, so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.

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

If you want to use the JVM's current default time zone, ask for it and pass as an argument. If omitted, the JVM's current default is applied implicitly. Better to be explicit, as the default may be changed at any moment during runtime by any code in any thread of any app within the JVM.

ZoneId z = ZoneId.systemDefault() ;  // Get JVM’s current default time zone.

Or specify a date. You may set the month by a number, with sane numbering 1-12 for January-December.

LocalDate ld = LocalDate.of( 1986 , 2 , 23 ) ;  // Years use sane direct numbering (1986 means year 1986). Months use sane numbering, 1-12 for January-December.

Or, better, use the Month enum objects pre-defined, one for each month of the year. Tip: Use these Month objects throughout your codebase rather than a mere integer number to make your code more self-documenting, ensure valid values, and provide type-safety .

LocalDate ld = LocalDate.of( 1986 , Month.FEBRUARY , 23 ) ;

Get day-of-week via a smart DayOfWeek object.

DayOfWeek dow = ld.getDayOfWeek() ;

I suggest using such objects instead of mere integer numbers. But if you insist:

int dowNumber = ld.getDayOfWeek().getValue() ;

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?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM