0

Need help understanding below test code. Specifically I do not understand what the "11" and "12" represent in the calendar.set method? example "openCal.set(11, openHrs).

public static void main(String[] args) {

     {

         int openHrs = 07;
         int openMins = 30;
         int closedHrs = 23;
         int closedMins = 00;
         Calendar cal = Calendar.getInstance();
         Calendar openCal = Calendar.getInstance();
         openCal.set(11, openHrs);
         openCal.set(12, openMins);
         Calendar closeCal = Calendar.getInstance();
         closeCal.set(11, closedHrs);
         closeCal.set(12, closedMins);
         if(openCal.before(cal) && closeCal.after(cal))
         {
           System.out.println("The Business is OPEN");

         } else
         {
            System.out.println("The Business is CLOSED");
         }
     }

}

5 Answers5

4

This is perfect example of why we should avoid magic numbers.

Instead of set(11 code should look like set(Calendar.HOUR_OF_DAY.
Instead of set(12 code should look like set(Calendar.MINUTE.

If you take a look at documentation of Calendar class you will find few examples of how to use set methods like

  • set(Calendar.MONTH, Calendar.SEPTEMBER)
  • set(Calendar.DAY_OF_MONTH, 30)

By looking at source code of Calendar class you will find many constants and their values. They can also be found at

http://docs.oracle.com/javase/8/docs/api/constant-values.html

so you see that

Community
  • 1
  • 1
Pshemo
  • 122,468
  • 25
  • 185
  • 269
1

Just digging into Calendar source code those magic numbers are tied to the following fields

private static final String[] FIELD_NAME = {
    "ERA", "YEAR", "MONTH", "WEEK_OF_YEAR", "WEEK_OF_MONTH", "DAY_OF_MONTH",
    "DAY_OF_YEAR", "DAY_OF_WEEK", "DAY_OF_WEEK_IN_MONTH", "AM_PM", "HOUR",
    "HOUR_OF_DAY", "MINUTE", "SECOND", "MILLISECOND", "ZONE_OFFSET",
    "DST_OFFSET"
};

So in that case you can see that 11 is HOUR_OF_DAY and 12 is MINUTE

gtgaxiola
  • 9,241
  • 5
  • 42
  • 64
  • You can also find the values in the [Constant Field Values](http://docs.oracle.com/javase/7/docs/api/constant-values.html#java.util.Calendar.ALL_STYLES) section of the API documentation. – Jesper Jun 27 '14 at 13:15
  • Thanks to all. Appreciate the quick, clear answers. – user2351988 Jun 27 '14 at 13:21
1

Where are you looking at this tutorial? In the set function for Calendar that has two parameters the first parameter is an index for where the data is and the second is the value to set. So from the code that would suggest that 11 is for Hours and 12 is for minutes. The documentation is at http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html . The numbers should be replaced with constants from the Calendar class to make this code more readable and self answer your question.

wckd
  • 410
  • 2
  • 9
1

Calendars get and set methods use integers as the first parameters, which indicate the field that should be retrieved respectively changed. This might seem strange, but Calendar is older than Enums in Java (and considering all the other stupidities in Java's date related classes, this one is a minor one).

As others have pointed out, the only acceptable practice is to use the constants defined in Calendar (HOUR_OF_DAY etc.), but syntactically, a [expletive removed] programmer can use numerical literals too (or even expressions that result in an int value).

Erich Kitzmueller
  • 36,381
  • 5
  • 80
  • 102
  • 1
    so I could do calendar.set("HOUR_OF_DAY".length(), 9); and oddly enough still change the Calendar.HOUR_OF_DAY field..... interesting – gtgaxiola Jun 27 '14 at 13:22
  • No only that, you could even expect to see your code posted on www.thedailywtf.com as soon as some other programmer finds it. – Erich Kitzmueller Jun 27 '14 at 13:26
0

tl;dr

LocalTime now = LocalTime.now( ZoneId.of( "America/Montreal" ) );
Boolean isOpenNow = 
    ( ! now.isBefore( LocalTime.of( 7 , 30 ) ) ) 
    && 
    now.isBefore( LocalTime.of( 23 , 00 ) ) 
    ;

Details

The Answer by Pshemo is correct and should be accepted.

Avoid legacy date-time classes

The Question and answers have outmoded code using troublesome old date-time classes now supplanted by the java.time classes.

LocalTime

The LocalTime classe represents a time-of-day without a date and without a time zone.

LocalTime opening = LocalTime.of( 7 , 30 );
LocalTime closing = LocalTime.of( 23 , 00 );

Time zone

Determining the current time of day requires a time zone. For any given moment the time-of-day (and the date) vary around the globe by zone.

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

Comparing

We can compare the LocalTime objects with compareTo, equals, isAfter, and isBefore.

In this example code we use the Half-Open approach to defining a span of time, where the beginning is inclusive while the ending is exclusive. This approach is sensible and commonly used. Using this approach throughout all your code makes your logic easier to comprehend and makes errors less likely. So we ask “is the current moment the same or later than the start but also earlier than the stop?”. A more compact way to say the same thing is:

Is now not earlier than start AND now is earlier than stop?

Boolean isOpenNow = ( ! now.isBefore( opening ) ) && now.isBefore( closing ) ;
Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154