1

so I wanted to implement a method that gives me the dates of the workdays, and if it's Saturday or Sunday those of the next week. To achieve this I used Calendar and the setFirstDayOfWeek method. This is how my code looks:

    Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin"));
    calendar.setFirstDayOfWeek(Calendar.SATURDAY);
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); //write to array 
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);//write
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY); // and so on...

If I compile this in IntelliJ on Sat. Dec 6, I get the following dates:

Mon Dec 08 23:44:32 CET 2014
Tue Dec 09 23:44:32 CET 2014
...
Sat Dec 06 23:44:32 CET 2014
Sun Dec 07 23:44:32 CET 2014

..just as it was intended. w/o setting the first day of week to Saturday the Monday date would be Dec 01.

Now I use the exact same code under Android Studio. The dates look like they do in IntelliJ if I don't set the first day of week to Saturday. Can anybody tell me why the same code provides different results?

lexysoda
  • 11
  • 2
  • Is this question about the difference in how the code behaves on the device when run, or just about the IDE's representation of the date in the debugger? – GreyBeardedGeek Dec 06 '14 at 23:07
  • It's about how the code behaves on the different platforms PC and Android, although it is documented exactly the same. – lexysoda Dec 07 '14 at 14:08

2 Answers2

0

It's probably a difference in the default first day of the week. Try doing calendar.getFirstDayOfWeek() on both platforms right after getInstance(), and see what you get.

If you get different values, that's probably the issue, which in turn is probably due to different default Locale. Try constructing the Calendar with the same Locale on both platforms.

Then, also remember that while you write Android code in Java, at runtime, it's not executed on a JVM, it uses the Dalvik runtime. There are no guarantees that the two are 100% compatible, especially for something like a default Locale.

For more information, see How to specify firstDayOfWeek for java.util.Calendar using a JVM argument

Community
  • 1
  • 1
GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
  • Thanks for your response. On both platforms I get the same result for getFirstDayOfWeek(), so I guess the locale isn't the problem. Also I am aware of possible differences between the platforms , but since the Java Doc and the Android Doc for Calendar are exactly the same, I don't know where to find a solution to why the code behaves differently. – lexysoda Dec 09 '14 at 14:09
0

java.time

While I cannot explain the behavior difference, I can suggest dumping these troublesome old date-time classes for modern classes that work in a sane manner: java.time classes.

ZoneId z = ZoneId.of( "Europe/Berlin" ) ;
LocalDate today = LocalDate.now( z ) ;  // Current moment.

The DayOfWeek enum defines seven objects, one for each day of the week. Define "weekend" as a set of those objects.

Set< DayOfWeek > weekend = EnumSet.of( DayOfWeek.SATURDAY , DayOfWeek.SUNDAY ) ;

Compare today's day-of-week to see if it is contained in the set.

LocalDate monday = null ;
if( weekend.contains( todayDow ) {
    monday = today.with( TemporalAdjusters.next( DayOfWeek.MONDAY ) ) ;
} else {  // Else today is a week-day, not found in the weekend set.
    monday = today.with( TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY ) ) ;
}

Collect the rest of the days of the week.

List< LocalDate > daysOfTheWeek = new ArrayList<>( 5 ) ;
daysOfTheWeek.add( monday ) ;
for( int i = 1 ; i <= 4 ; i ++ ) {
    daysOfTheWeek.add( monday.plusDays( i ) ) ; // Tuesday, Wednesday, Thursday, Friday.
}

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?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154