• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Wrong month

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote the following code for an exercise and ran it 3/31 and it is returning a month of Febuary, so I printed the numeric month and get 2. My date is right on my PC. I am on a network so that could be it but I was wondering if anyone knew of some quirk. Another person ran it on her PC and returned the same results. (She is also on the network.)

import java.util.*;

class ClockTalk {
public static void main(String[] arguments) {
//get current time and date
Calendar now = Calendar.getInstance();
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int month = now.get(Calendar.MONTH);
int day = now.get(Calendar.DAY_OF_MONTH);
int year = now.get(Calendar.YEAR);

// dispay greeting
if (hour < 12)
System.out.print("Good morning.\n");
else if (hour < 17)
System.out.print("Good afternoon.\n");
else
System.out.print("Good evening.\n");

// begin time message by showing the minutes
System.out.print("It's");
if (minute != 0) {
System.out.print(" " + minute + " ");
System.out.print( (minute != 1) ? "minutes" :
"minute");
System.out.print(" past");
}

// display hour
System.out.print(" ");
System.out.println( (hour > 12) ? (hour - 12) : hour );
//display month
switch (month) {
case (1):
System.out.print("January");
break;
case (2):
System.out.print("February");
break;
case (3):
System.out.print("March");
break;
case (4):
System.out.print("April");
break;
case (5):
System.out.print("May");
break;
case (6):
System.out.print("June");
break;
case (7):
System.out.print("July");
break;
case (8):
System.out.print("August");
break;
case (9):
System.out.print("September");
break;
case (10):
System.out.print("October");
break;
case (11):
System.out.print("November");
break;
case (12):
System.out.print("December");
}

// display the date and year
System.out.print(" " + ", " + year + ".");
}
}



I am a mainframer trying to expand my horizons so this bugs me.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey u know what...
if i am not wrong..check the way the months are stored..i mean do they strat from 0 or 1....
since u are getting 2 for march....it means jan is represented by 0....check it out...

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I poked through the JavaDocs on java.util.Calendar, looking for something that would indicate that the month number starts with 0. Nothing. However, I did find this bit:

Calendar defines the range of values returned by certain fields, as well as their meaning. For example, the first month of the year has value MONTH == JANUARY for all calendars.



From that I infer that you cannot truly say, with certainty, that Calendar.MONTH in March will be 2; you can only be sure that Calendar.MONTH will be equal to Calendar.MARCH.

Try adding these right after your existing Syste.out.print statement and you 'll see what values they have -- 0, 1, and 2.



So, for your code to technically be correct, you should change you switch statement to this:



Of course, if you're not interested in being that much of a stickler for the rules, you could just change "switch(month)" to "switch(month+1)" and figure that'll probably work for any environment your program is likely to run on.
 
What's brown and sticky? ... a stick. Or a tiny ad.
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic