The Calendar Class...

... is driving me insane.

Page 1 of 1

14 Replies - 2999 Views - Last Post: 04 June 2006 - 01:34 AM Rate Topic: -----

#1 moerl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 02-June 06

The Calendar Class...

Post icon  Posted 02 June 2006 - 10:34 PM

I'm working on an assignment and am hopelessly stuck. I've tried plenty of things but just can't wrap my head around how to do this. I'm new here as you can tell from my post-count. I'm also new to Java and am only weeks into my intro comp sci class with Java.

I'm stuck on what the assignment page calls the second "iteration" of this program. The description here is as follows:

Quote

CalendarCalculator02.java: Start with all of your code from CalendarCalculator01. You will next obtain 2 Calendar objects (for the 2 days) using that class's static factory method, and set the month, day, and year of each of the two days into it's respective Calendar object. Next, you will create at least 2 SimpleDateFormat objects, to help create the correct style of formatted output (e.g. "January 29, 1998" or "Thursday"). Finally, you will create the print statements to output each of the 2 dates. These print statements will be created using the format method of SimpleDateFormat to dictate how to display each Calendar object. Finally, you will use an accessor method on Calendar to obtain the week of the year.


The italic part is what I'm stuck on. I've read the Calendar Class API about a thousand times now and it's just not making any sense to my noobish Java mind. I use Eclipse for coding (and want to take a look at IntelliJ's IDEA just out of curiosity..), but using Eclipse's debug perspective I discovered that all those field values defined at the top of the Calendar API page are defined the moment I create an object of type Calendar. It's all based on the "now", the current time. I am to use an accessor method of Calendar to get the "week of year" as an integer, I suppose, and I just don't see how I can do that.

I've done some googling on the Calendar class and it seems programmers all around hate the way Java handles dates and times using the classes Calendar, SimpleDateFormat, Date, etc. I couldn't agree more, even if I don't know a fraction of what all those other users complaining about it must know..

I'm not quite sure how to get the "days and years" for both calendar objects either.. (that's in the next iteration of the program, so the next paragraph after the one I've quoted above).

Any help would be greatly appreciated. Of course I don't expect anybody to feed me the answer on a silver platter. ANY HELP will be appreciated. Tips, links, advice of any kind.. thanks in advance.

PS
Excuse the length of this post.. but I figure if I'm precise and thorough in my initial post, it will help you help me better than if I kept the description very short, forcing you to ask further questions about details on the problem.

This post has been edited by moerl: 02 June 2006 - 10:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: The Calendar Class...

#2 Videege  Icon User is offline

  • rÍvant.toujours
  • member icon

Reputation: 6
  • View blog
  • Posts: 1,413
  • Joined: 25-March 03

Re: The Calendar Class...

Posted 02 June 2006 - 10:52 PM

Well, after a quick run through of the API documentation on the calendar class (I must say it does look rather un-fun to use), I came across this constant:

Week of Year

It looks like you can use the Calendar class' get method to get the current week of the year.

 /*i.e*/ CalendarObject.get(Calendar.WEEK_OF_YEAR); //will return the current week of the year 


If I'm not mistaken that should take care of the week of the year thing.

Read up on the SimpleDateFormat here.

It appears to be able to format different string tokens into a more normal English type date readout.

Thus, if you created a string (getting the date and year numbers using the Calendar class' get(CONSTANTVALUE) method) and then used this class you could output the date as specified by the requirements.

Hope this is helpful, and as always, give it a shot, and if you should need assistance, we're never more than a few clicks away! Welcome to dream.in.code!
Was This Post Helpful? 0
  • +
  • -

#3 moerl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 02-June 06

Re: The Calendar Class...

Posted 02 June 2006 - 11:20 PM

Thanks a bunch, Videege!
I probably should have posted some sample code of what I've done already. I'm comfortable with SimpleDateFormat, even if it's a pain to use. It's API page is a lot more accessible and just makes sense, whereas the Calendar API page made me want to jump out the window.

I also should have mentioned that I saw the WEEK_OF_YEAR constant countless times, and focused on it as well since it sounds precisely like what I'm looking for. My problem is that I don't need the current week of year.. I need the week of the year that is input by the user for the two date formats. The first format is MM/DD/YYYY and the second one is YYYY-MM-DD. After the user inputs, I have this data stored as strings. Then I used the String class' substring method to grab the number parts of the read-in strings and then I used the Integer class' parseInt method to make of the numbers of data type String numbers of data type int. From that point on, I can work with the numbers.

I then created to Calendar objects and also two SimpleDateFormat objects for formatting. My formatting is already done and looks just like the sample input on the assignment page. What I'm missing is the third program iteration and the very last part of the second one, the getting of the week of year using an accessor method of class Calendar.

Now you know what my problem is.. I need to get the week of year for whatever two dates are stored in the calendar objects based on user input for the two date formats, not simply today's date. Also, see how the Calendar API page says this: "The first week of the year, as defined by getFirstDayOfWeek() and getMinimalDaysInFirstWeek(), has value 1. Subclasses define the value of WEEK_OF_YEAR for days before the first week of the year." Reading this, I have no clue where these two methods come in and just what they have to do with how I'm supposed to grab the WEEK_OF_YEAR integer.
Was This Post Helpful? 0
  • +
  • -

#4 Videege  Icon User is offline

  • rÍvant.toujours
  • member icon

Reputation: 6
  • View blog
  • Posts: 1,413
  • Joined: 25-March 03

Re: The Calendar Class...

Posted 03 June 2006 - 12:07 AM

Ah, so the Calendar objects default to the current date, eh? Must've missed that :D.

In that case, you can recalibrate the calendar objects to your user's inputted dates by using the Calendar class' setTime method. It takes a Date object as a parameter - the class seems pretty easy to use if you're already getting int values in your input process somewhere along the line.

After you set your calendar objects to the input dates, get(Calendar.WEEK_OF_YEAR) should return the correct week of the year according to your user's input.


On the off-chance that I'm totally misinterpreting your post again, please feel free to yell at me. :D
Was This Post Helpful? 0
  • +
  • -

#5 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: The Calendar Class...

Posted 03 June 2006 - 12:21 AM

sounds right Videege :) I had the pleasure (not sure that's the word i'm looking for) of using the calendar class in my first yr univ course.
Was This Post Helpful? 0
  • +
  • -

#6 moerl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 02-June 06

Re: The Calendar Class...

Posted 03 June 2006 - 07:05 AM

View PostVideege, on 3 Jun, 2006 - 12:59 AM, said:

Ah, so the Calendar objects default to the current date, eh? Must've missed that :D.

In that case, you can recalibrate the calendar objects to your user's inputted dates by using the Calendar class' setTime method. It takes a Date object as a parameter - the class seems pretty easy to use if you're already getting int values in your input process somewhere along the line.

After you set your calendar objects to the input dates, get(WEEK_OF_YEAR) should return the correct week of the year according to your user's input.


On the off-chance that I'm totally misinterpreting your post again, please feel free to yell at me. :D

Yeah, everything is based on the current time and that's simple enough, but as soon as you want to change the values of a Calendar object, it turns into a mess.

I realized that what you described in the second paragraph is what I want to do. I didn't think about the "setTime" method specifically so I went and checked it out. As a result, I had to look at the API page of "Date", and boy was that a surprise. Take a look, and tell me what you see! Just about everything in that class is deprecated and replaced by Calendar methods, which of course does not help my confusion with the Calendar class at all ;)

Also, I noticed that the assignment website does not mention the Date class anywhere. It states the following:
"In this assignment we will exercise our understanding of using classes from the Java API. Specifically, you will write 2 programs (EmailAddressBuilder and XYZ), which will work with objects of the String, Integer, Calendar, SimpleDateFormat, and DecimalFormat classes. In addition, you will continue to use the Scanner class to process input (text) from the keyboard."

I'm guessing then that what I'm trying to do has to be possible using just the classes mentioned above.

You're right in saying that it would be pretty easy to "recalibrate" a calendar object with chosen values if I'm already using ints of the values I want to change earlier in the code. That's absolutely true.. except, well, see the above :)

You interpreted the post just fine :)
Was This Post Helpful? 0
  • +
  • -

#7 Videege  Icon User is offline

  • rÍvant.toujours
  • member icon

Reputation: 6
  • View blog
  • Posts: 1,413
  • Joined: 25-March 03

Re: The Calendar Class...

Posted 03 June 2006 - 10:01 AM

Ah, well, if you just want to use the Calendar class, using the set(int field, int value) method or one of its other overloaded versions seems to be the way to go. The field parameter should refer to one of those many static final ints that the calendar class defines. Par example:

myCalendar.set(Calendar.MONTH, Calendar.JULY); //sets calendar to July.
myCalendar.set(Calendar.DAY, 23); //sets Calendar now to the 23rd day



However, be careful when setting these fields; make sure to check the documentation. In the above example, the enumerations (i.e Calendar.JULY, Calendar.JANUARY) for the Calendar.MONTH field begin with Calendar.JANUARY = 0. So if you took user input as the date being, for example, 03/24/2004, you would need to set the Calendar as following:

int month, day, year; //let's say you've already gotten the above values for these
myCalendar.set(Calendar.MONTH, month-1);//because January is 0, not 1
myCalendar.set(Calendar.DAY, day);
myCalendar.set(Calendar.YEAR, year);
/* note that alternatively you could use one of set(...)'s overloaded 
versions, like set(int year, int month, int date), which sets the 
year, month, and day all at once, like so: */
myCalendar.set(year, month-1, day);



As you can see, you can easily set the Calendar class' current time to whatever your user input is; just remember to be careful and check the documentation. :D

Hope I've finally given you some real help, hehe XD.
Was This Post Helpful? 0
  • +
  • -

#8 moerl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 02-June 06

Re: The Calendar Class...

Posted 03 June 2006 - 11:28 AM

That did help in that it helped me understand that I cannot just SET the value for what I'm looking for. I need to grab the "week of year" value for whatever date the user happens to input somehow. I don't have a value for week of year anywhere in the program thus far because I let SimpleDateFormat handle the output for the "week of year", which means I still don't have a clue how to do this.

I appreciate your attempts to help and I think we're getting somewhere here. This time I'll include my full code so you can see what I'm talking about here. I commented in some important information as well. Here we go:

package edu.bu.cs111.students.ismar.hw04;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;

public class CalendarCalculator03
{
	private static int WEEK_OF_YEAR;

	public static void main(String[] args)
	{
		// TASK 1 (CalendarCalculator01.java)

		// Collects user input and stores it in two strings, dateFormat1 and
		// dateFormat2
		Scanner kb = new Scanner(System.in);
		System.out.println("Enter a date in the form [MM/DD/YYYY]: ");
		String dateFormat1 = kb.next();

		System.out
				.println("Enter a different date in the format [YYYY-MM-DD]: ");
		String dateFormat2 = kb.next();

		// extracts substrings for month, day and year from dateFormat1 --
		// [MM/DD/YYYY]
		String month1 = dateFormat1.substring(0, 2);
		String day1 = dateFormat1.substring(3, 5);
		String year1 = dateFormat1.substring(6, 10);

		// parses substrings of month, day and year to obtain integer values for
		// dateFormat 1 -- [YYYY-MM-DD]
		int monthNumber1 = Integer.parseInt(month1);
		int dayNumber1 = Integer.parseInt(day1);
		int yearNumber1 = Integer.parseInt(year1);

		// creates substrings for month, day and year for dateFormat2 --
		// [YYYY-MM-DD]
		String year2 = dateFormat2.substring(0, 4);
		String month2 = dateFormat2.substring(5, 7);
		String day2 = dateFormat2.substring(8, 10);

		// parses substrings of month, day and year to obtain integer values for
		// dateFormat 2 -- [YYYY-MM-DD]
		int yearNumber2 = Integer.parseInt(year2);
		int monthNumber2 = Integer.parseInt(month2);
		int dayNumber2 = Integer.parseInt(day2);

		// value test
		// System.out.println(monthNumber1);
		// System.out.println(dayNumber1);
		// System.out.println(yearNumber1);

		// Required output format for SimpleDateFormat (example):
		// "Day one is June 9, 1977, a Thursday in week 24 of the year 1977."

		// Test user input values:
		// Date Format 1: 07/05/1984
		// Date Format 2: 1988-07-23

		// TASK 2 (CalendarCalculator01.java)

		// Creates objects of type Calendar
		Calendar dateFormatCal1 = Calendar.getInstance();
		Calendar dateFormatCal2 = Calendar.getInstance();

		// Sets user input values for year, month and day into calendar objects
		// as respective values
		dateFormatCal1.set(yearNumber1, monthNumber1 - 1, dayNumber1);
		dateFormatCal2.set(yearNumber2, monthNumber2 - 1, dayNumber2);

		// Formats correct output according to example above
		SimpleDateFormat d1 = new SimpleDateFormat(
				"'Day one is 'MMMM d', 'yyyy', a 'EEEE' in week 'w' of the year 'yyyy'.'");
		SimpleDateFormat d2 = new SimpleDateFormat(
				"'Day one is 'MMMM d', 'yyyy', a 'EEEE' in week 'w' of the year 'yyyy'.'");

		// Prints correctly formatted data to screen
		System.out.println(d1.format(dateFormatCal1.getTime()));
		System.out.println(d2.format(dateFormatCal2.getTime()));

		// TODO: Get "week of year" using a Calendar accessor method

		// dateFormatCal1.get(Calendar.WEEK_OF_YEAR, I have nothing to put
		// here!);

		/**
		 * Problem: WEEK_OF_YEAR must be defined for whatever date the user
		 * inputs. How to do that? I don't see how I can SET the WEEK_OF_YEAR
		 * values using a Calendar mutator method because, well, I don't have a
		 * "week of year" value anywhere in the program thus far. I should just
		 * be able to grab the correct "week of year" value based on the date/s
		 * the user inputs. But how?
		 */

		// Test correctness of "week of year" value
		/*System.out.println();
		System.out.println();
		System.out.println("weekOfYear = " + WEEK_OF_YEAR);*/

	}
}



I hope that makes sense. I just spent extra time commenting every function of the program just so I could post it here and make it transparent.
Was This Post Helpful? 0
  • +
  • -

#9 moerl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 02-June 06

Re: The Calendar Class...

Posted 03 June 2006 - 11:37 AM

I can't believe I have not tried this yet. Simple:

CalendarObject.get(Calendar.WEEK_OF_YEAR);



I have no idea whether it will work but that seems the way to do it. Here's what I'll do now:


CalendarObject.get(Calendar.WEEK_OF_YEAR);
System.out.println("Week of Year (accessor Calendar) = " + WEEK_OF_YEAR);



Let's see if that yields the correct value..
Was This Post Helpful? 0
  • +
  • -

#10 Videege  Icon User is offline

  • rÍvant.toujours
  • member icon

Reputation: 6
  • View blog
  • Posts: 1,413
  • Joined: 25-March 03

Re: The Calendar Class...

Posted 03 June 2006 - 11:51 AM

View Postmoerl, on 3 Jun, 2006 - 11:29 AM, said:

I can't believe I have not tried this yet. Simple:


From my second post?

Quote

After you set your calendar objects to the input dates, get(Calendar.WEEK_OF_YEAR) should return the correct week of the year according to your user's input.


:D

Note that your code will not work as intended, because WEEK_OF_YEAR is part of the Calendar class. It should be as follows:
System.out.println("Week of Year (accessor Calendar) = " + CalendarObject.get(Calendar.WEEK_OF_YEAR));

Was This Post Helpful? 0
  • +
  • -

#11 moerl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 02-June 06

Re: The Calendar Class...

Posted 03 June 2006 - 11:57 AM

Holy crap.. I did it! Both of the following yield an integer value of "27" for July 5th 1984, (my birthday):

int weekOfYear = dateFormatCal1.get(Calendar.WEEK_OF_YEAR);
System.out.println("Week of Year (accessor Calendar) = " + weekOfYear);

// and

System.out.println("Week of Year (accessor Calendar) = " + dateFormatCal1.get(Calendar.WEEK_OF_YEAR));



Damn that was simple. Where was my head all this time? Let's hope this is the answer I was looking for.. otherwise I may have to call a shrink.
Was This Post Helpful? 0
  • +
  • -

#12 Videege  Icon User is offline

  • rÍvant.toujours
  • member icon

Reputation: 6
  • View blog
  • Posts: 1,413
  • Joined: 25-March 03

Re: The Calendar Class...

Posted 03 June 2006 - 12:00 PM

Cool stuff. If you ever need more help with other stuff, feel free to ask. Now go hang out in the lounge and toast your victory.
Was This Post Helpful? 0
  • +
  • -

#13 moerl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 02-June 06

Re: The Calendar Class...

Posted 03 June 2006 - 12:23 PM

Ooooh I'm not quite done yet. One last task remains: to find the time (in days) between the two days and print that out in a sentence. I'm not sure how I'll do that just yet but I'll sit down and try to figure it out. I may be back sooner than you like :)

Thanks a ton for your help. Without your ideas, I would probably still be wondering what to do here!

By the way.. is there an easy way to click yourself to the SUBSCRIBED THREADS page here? How do you keep up with subscribed threads?
Was This Post Helpful? 0
  • +
  • -

#14 Videege  Icon User is offline

  • rÍvant.toujours
  • member icon

Reputation: 6
  • View blog
  • Posts: 1,413
  • Joined: 25-March 03

Re: The Calendar Class...

Posted 03 June 2006 - 12:35 PM

I don't think there is a direct link to it, no.

(By default) Your control panel is on the left of the page, click on something (like your messages) to get to all the controls. You should see a column for "Subscriptions", in which you can view all of your subscribed threads.
Was This Post Helpful? 0
  • +
  • -

#15 moerl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 02-June 06

Re: The Calendar Class...

Posted 04 June 2006 - 01:34 AM

The only way I see is to click on EDIT YOUR PROFILE on the left, then on VIEW TOPICS under "Subscriptions". Not exactly what I'd call efficient or handy.. but it works! Thanks again :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1