Android Calendar Day of the Week Problem

I am trying to list all the days of the week in the current week from Monday to Sunday. For example, today (dispatch day) is September 4, 2011 and Sunday.

I start the calendar and set the first day of the week on Monday:

Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);

When I check the day of the month, I get the correct result:

int check = cal.get(Calendar.DAY_OF_MONTH);
// check is equal to 4

But when I set the weekday to Monday, it jumps to the next week, and does not return on Monday of this week:

cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
int mon = cal.get(Calendar.DAY_OF_MONTH);
// mon is equal to 5, when expected to be 29 (last Monday of August)

Even setting the weekday to Sunday returns the next Sunday and not today.

Can someone explain why this works this way and how best to solve this problem?

+5
source share
3 answers

, , , , , ​​:

4-29:

Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(4);
cal.set(2011, 8, 4);
int test = cal.get(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
int mon = cal.get(Calendar.DAY_OF_MONTH);
bTest.setText("" + test + "-" + mon);

5-5:

Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(4);
cal.set(2011, 8, 5);
int test = cal.get(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
int mon = cal.get(Calendar.DAY_OF_MONTH);
bTest.setText("" + test + "-" + mon);

14-12:

Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(4);
cal.set(2011, 8, 14);
int test = cal.get(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
int mon = cal.get(Calendar.DAY_OF_MONTH);
bTest.setText("" + test + "-" + mon);

, :

Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(4);
//cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
int test = cal.get(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
int mon = cal.get(Calendar.DAY_OF_MONTH);
bTest.setText("" + test + "-" + mon); // Display 4-5

:

Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(4);

// Workaround
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));

int test = cal.get(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
int mon = cal.get(Calendar.DAY_OF_MONTH);
bTest.setText("" + test + "-" + mon); // Display 4-29

:

Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(4);

// Workaround
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
cal.get(Calendar.DAY_OF_MONTH);

cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
int mon = cal.get(Calendar.DAY_OF_MONTH);
bTest.setText("" + mon); // Display 29

:

Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(4);

// Workaround
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
int mon = cal.get(Calendar.DAY_OF_MONTH);
bTest.setText("" + mon); // Display 5
+6

-, , , . , , 0 , 1900 , . , Date Sunday 0, Sunday Sunday 1.

//THIS WORKS CORRECTLY
Date my = new Date(1986 - 1900, 04 - 1, 26);
System.out.println(my);
System.out.println(my.getDay());

Calendar cal = Calendar.getInstance(TimeZone.getDefault());
cal.set(1986, 04 - 1, 26);
System.out.println(cal.getTime());
System.out.println(cal.get(Calendar.DAY_OF_WEEK)-Calendar.SUNDAY);

,

//ONE AND ONLY, HUMAN FRIENDLY WAY TO ENTER DATE INTO JAVA
String date = "1986-04-26:01:23:47";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd:HH:mm:SS");
Date convertedDate = (Date) formatter.parse(date);
System.out.println(convertedDate);

, ?

0

TL;DR

LocalDate, Calendar.

LocalDate.now( ZoneId.of( "Africa/Tunis" ) )
    .with( TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY ) )
    .plusDays( i ) )

java.time

Calendar, , java.time( ZonedDateTime).

LocalDate, + , Calendar ZonedDateTime. LocalDate .

. . , - , "" .

, JVM . , . / .

continent/region, America/Montreal, Africa/Casablanca Pacific/Auckland. 3-4 , EST IST, , (!).

ZoneId z = ZoneId.of( "America/Montreal" ) ;  
LocalDate today = LocalDate.now( z ) ;

JVMs, . , JVM . , JVM.

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

. , 1-12 -.

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.

, , Month , . . Month , , , type- .

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

DayOfWeek

DayOfWeek enum. , , -.

DayOfWeek dow = ld.getDayOfWeek() ;  // Get an enum representing the day-of-week of this date, such as `DayOfWeek.MONDAY`.

, . , TemporalAdjusters.previousOrSame TemporalAdjuster.

TemporalAdjuster ta = TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY ) ;
LocalDate previousOrSameMonday = ld.with( ta ) ;

, LocalDate.plusDays(). , java.time . , , () .

// Hard-coded `7` is the seven days in a week.
List< LocalDate > dates = new ArrayList<>( 7 ) ;
for( int i = 0 , i < 7 , i ++ ) {
    LocalDate localDate = previousOrSameMonday.plusDays( i ) ;
    dates.add( localDate ) ;
}

java.time

java.time Java 8 . legacy , java.util.Date, Calendar SimpleDateFormat.

Joda-Time, , java.time.

, . Oracle. Qaru . JSR 310.

java.time . JDBC, JDBC 4.2 , , java.sql.*.

java.time?

ThreeTen-Extra java.time . java.time. , Interval, YearWeek, YearQuarter .


UPDATE: Joda-Time , java.time. .

Joda

?

- java.util.Date .Calendar Joda-Time . Joda-Time Android.

Joda-Time LocalDate - .

Joda-Time 2.3.

LocalDate localDate = new LocalDate( 2011, DateTimeConstants.SEPTEMBER, 4 );
LocalDate firstDateOfWeek = localDate.withDayOfWeek( DateTimeConstants.MONDAY );
for ( int i = 0; i < 7; i++ ) {
    LocalDate someDateOfWeek = firstDateOfWeek.plusDays( i );
    System.out.println( "someDateOfWeek: " + someDateOfWeek + "  le jour de la semaine: " + someDateOfWeek.dayOfWeek().getAsText( Locale.CANADA_FRENCH ) );
}

...

someDateOfWeek: 2011-08-29  le jour de la semaine: lundi
someDateOfWeek: 2011-08-30  le jour de la semaine: mardi
someDateOfWeek: 2011-08-31  le jour de la semaine: mercredi
someDateOfWeek: 2011-09-01  le jour de la semaine: jeudi
someDateOfWeek: 2011-09-02  le jour de la semaine: vendredi
someDateOfWeek: 2011-09-03  le jour de la semaine: samedi
someDateOfWeek: 2011-09-04  le jour de la semaine: dimanche

: , ISO 8601, weekOfWeekYear. :

int weekNumber = firstDateOfWeek.getWeekOfWeekyear();

35.

0

All Articles