0

I've been working on a program for my Computer Programming class and I'm having a little trouble. Truthfully, I'm on the verge of insanity...

Its a calendar program and the part I'm having trouble with is the Calendar object in java. Here is my code for one of methods:

public static void calendarGet(){
  String md = getInput("What date would you like to look at?(mm/dd)"); 
  int slash = md.indexOf('/');
  MDFromDate(md);

  cal.set(cal.MONTH, MONTH - 1); 
  cal.set(cal.DATE, DAY); 

  TARGET_DAY = DAY;
  MAX_DAY = cal.getActualMaximum(cal.DAY_OF_MONTH);
  WEEKS_IN_MONTH = MAX_DAY / 7;

  System.out.println(cal.MONTH + 1);
  System.out.println(cal.DAY_OF_MONTH);
  System.out.println(cal.getTime());

  drawMonth(cal.MONTH);

}

And the output for this is:

What date would you like to look at?(mm/dd)12/12
3
5
Wed Dec 12 22:47:32 PST 2018

As you can see, if I use getTime(); it returns the right month and day but cal.MONTH and cal.DAY_OF_MONTH do not.

Also, when I use the debugger and look in cal, none of the variables had changed.

I'm incredibly confused, I would appreciate some help! :D

EDIT:

public static String getInput(String prompt){
     System.out.print(prompt);
     Scanner inScan = new Scanner(System.in);
     return inScan.nextLine();
}

public static void MDFromDate(String md){
     Scanner getMD = new Scanner(md).useDelimiter("/");
     MONTH = getMD.nextInt();
     DAY = getMD.nextInt();

}
  • 1
    Any reason why you're still using `Calendar` and not `java.Time` APIs instead? – MadProgrammer Mar 21 '18 at 06:05
  • *"I'm incredibly confused"* - You're not the only one - how is `MONTH` and `DAY` obtained? – MadProgrammer Mar 21 '18 at 06:07
  • My teacher felt strongly about us using it; I was confused as well. – Cyrus Bozorgzad Mar 21 '18 at 06:07
  • 1
    *"My teacher felt strongly about us using it"* Your teachers ... out of touch :/ – MadProgrammer Mar 21 '18 at 06:09
  • Don't post code in the comments, update you question with a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) which demonstrates what you are doing and the issues you are having - yes, this might require you to start a new project – MadProgrammer Mar 21 '18 at 06:10
  • Sorry this was my first post on the site. So I might have to restart using java.Time? – Cyrus Bozorgzad Mar 21 '18 at 06:13
  • If your teacher wants you using `Calendar`, use `Calendar`, let's get the assignment done first, then we can talk about best practices – MadProgrammer Mar 21 '18 at 06:15
  • Sorry, I'm a little confused, what do you recommend me doing? Thank you for your time, by the way. – Cyrus Bozorgzad Mar 21 '18 at 06:31
  • I recommend you start by following what you teach has asked you to do - if they want you to use `Calendar`, then that's what you should use. But if you need us to help you, then you need to provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – MadProgrammer Mar 21 '18 at 07:20

2 Answers2

1

So...

System.out.println(cal.MONTH + 1);
System.out.println(cal.DAY_OF_MONTH);

is printing the constant values assigned to Calendar.MONTH and Calendar.DAY_OF_MONTH, this are not the values contained within the Calendar instance, they are simply values you can use to set/get values of the Calendar

So, if instead we use Calendar#get, for example...

Calendar cal = Calendar.getInstance();

int month = 12;
int day = 12;

cal.set(cal.MONTH, month - 1);
cal.set(cal.DATE, day);

System.out.println(cal.get(Calendar.MONTH));
System.out.println(cal.get(Calendar.DATE));

It will print...

11
12

But wait, why 11, because the months are zero indexed (ie January is 0)

Part of the confusion is down to the fact that you're not using the standard coding conventions for the Java Language, which states variable names which all all uppercase (MONTH/DAY) are constants, which they aren't in your code

And, yes, as I'm sure some is bound to point out, you shouldn't be using Calendar but instead the newer java.time API, but if your teacher wants you to use it, who are we to contradict them :/

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

In a very well-explained answer MadProgrammer asks:

And, yes, as I'm sure some is bound to point out, you shouldn't be using Calendar but instead the newer java.time API, but if your teacher wants you to use it, who are we to contradict them :/

I am taking the chance. If only for other readers that are not under the same restrictions. Teachers are different. When I was teaching, I certainly appreciated whenever students found a different solution from what I had imagined. You know your teacher better, so I hope you know what is right in your situation, I certainly don’t.

private static final DateTimeFormatter monthDayFormatter
        = DateTimeFormatter.ofPattern("M/d");

public static void calendarGet(){
      String md = getInput("What date would you like to look at?(mm/dd)");
      LocalDate date = MonthDay.parse(md, monthDayFormatter)
            .atYear(Year.now(ZoneId.of("America/Los_Angeles")).getValue());
      int targetDay = date.getDayOfMonth();
      int maxDay = date.lengthOfMonth();

      System.out.println(date.getMonth());
      System.out.println(date.getDayOfMonth());
}

When I entered 04/05, this printed

APRIL
5

IMHO the code using java.time is clearer to read and what should be taught in schools.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161