aspose file tools
The moose likes Java in General and the fly likes Tough Calendar class questions Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Tough Calendar class questions" Watch "Tough Calendar class questions" New topic
Author

Tough Calendar class questions

Peter Wipperfurth
Greenhorn

Joined: Jul 05, 2006
Posts: 4
Hello,

I have two really tough Calendar class questions that
resemble word problems (two variables known, third unknown).

first
You have month and year (integer format).
How many days were in that month?
Example: 10/2005,4/2004,6/2006

second
You have week and year (integer format)
What month does it refer to?
Example: week5,week10,week32,week40 and 2006,2005,2002

I'm not familiar with Calendar classes but it seems you would create a calendar object, set the month and year, generate milliseconds...then I'm stuck.

Any ideas?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341
Peter, I would recommend looking at the API docs for the Calendar class.
Peter Wipperfurth
Greenhorn

Joined: Jul 05, 2006
Posts: 4
Originally posted by Keith Lynn:
I would recommend looking at the API docs for the Calendar class.


Thanks Keith, found a solution on the web for the second problem:

import java.util.Calendar;

class weektst
{
public static Calendar c=Calendar.getInstance();

public static Calendar setwk(Calendar c,int woy,int year)
{
System.out.println("for week: " + woy + " and year:" + year);
c.set(Calendar.YEAR,year);
c.set(Calendar.WEEK_OF_YEAR,woy);
System.out.println("date: " + c.getTime());
System.out.println("week: " + c.get(Calendar.WEEK_OF_YEAR));
System.out.println();
return c;
}

public static void main(String[] args)
{
c=setwk(c,5,2004);
c=setwk(c,10,2004);
c=setwk(c,32,2005);
c=setwk(c,40,2006);
}
}

From there you simply parse out the month...

Peter
[ July 05, 2006: Message edited by: Peter Wipperfurth ]
Peter Wipperfurth
Greenhorn

Joined: Jul 05, 2006
Posts: 4
Originally posted by Peter W.:
You have month and year (integer format).
How many days were in that month?
Example: 10/2005,4/2004,6/2006


There are apparently nuances to using the Calendar class with exact precision. Following is a solution that is approximate:

import java.util.Calendar;
import java.util.GregorianCalendar;

class daysin_month
{
public static Calendar c;
public static Calendar setmy(int m,int y)
{
Calendar tmpc=new GregorianCalendar(y,m,1);

System.out.println("max days in month: " + m + " in year: " + y);
System.out.println("month: " + m);
System.out.println("year: " + y);
System.out.println("max days: " + tmpc.getActualMaximum(Calendar.DAY_OF_MONTH));
return tmpc;
}

public static void main(String[] args)
{
c=setmy(10,2005);
c=setmy(4,2004);
c=setmy(6,2006);
}
}

However, the third result (June 2006) has too many days:

max days in month: 6 in year: 2006
month: 6
year: 2006
max days: 31

What modifications are needed to return correctly?

Peter
[ July 05, 2006: Message edited by: Peter Wipperfurth ]
Andy T�pfer-Hinz
Greenhorn

Joined: Jul 05, 2006
Posts: 1
I think this is correct as the months are counted from 0 to 11,
so month 6 is july which has 31 days !
Peter Wipperfurth
Greenhorn

Joined: Jul 05, 2006
Posts: 4
so month 6 is july which has 31 days !


Thanks!
Wu Kong
Greenhorn

Joined: Jun 12, 2006
Posts: 5
For Gregorian calendar,
1) Find the max. # of days for a particular month - solution is avaliable


2)
Find the month, given the week # in a year - not easy. A week may span two months. However, it is possible to find out the 1st day of Week #xx is in which month, for a particular calendar year.

NB: Please check getFirstDayOfWeek() and getMinimalDaysInFirstWeek().
[ July 06, 2006: Message edited by: Wu Kong ]
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Tough Calendar class questions
 
Similar Threads
Formatting date having month and year
comparing date fields from a database
Time and Date class
IF statement problem
Static variable Question