473,322 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Calculating week from GregorianCalendar.

I understand that the GregorianCalendar can be used to calculate the week of
year for a date, and after instantiating a GregorianCalendar object with
the no arg constructor, I can infact retrieve the current week (since it
initializes itself to the current date). However, when I try to create a
GregorianCalendar object by specifying a date in the constructor, it
returns an incorrect value for the WEEK_OF_YEAR field? This is demonstrated
by the code below.

Is there something in need to do to 'initialize' this field in the calendar
object? Am I not understanding the use of this object or is there something
I am missing here?

Any help or advice would be greatly appreciated.
Carl.

--- code ---
import java.util.*;

public class WeekTest {

private GregorianCalendar cal;
public WeekTest(){
// this works...
this.cal = new GregorianCalendar();
setupWeek();
}
public WeekTest(int year, int month, int day){
// this does not work?
this.cal = new GregorianCalendar(
year, month, day, 01, 01, 01);
setupWeek();
}
private void setupWeek() {
this.cal.setFirstDayOfWeek(Calendar.SUNDAY);
this.cal.setMinimalDaysInFirstWeek(4);
}

public void show(){
System.out.println(this.cal.toString());
System.out.println("Dare: " + this.cal.get(Calendar.MONTH) +
"-" + this.cal.get(Calendar.DATE) +
"-" + this.cal.get(Calendar.YEAR));
System.out.println("Week: " +
this.cal.get(Calendar.WEEK_OF_YEAR) +
" Year: " + this.cal.get(Calendar.YEAR));
}
public static void main(String[] args){
//this seems to work fine...
WeekTest wc = new WeekTest();
wc.show();

System.out.println();

//this does not !!!
WeekTest wc2 = new WeekTest(2004, 01, 01);
wc2.show();
}
}
--- end-code ---
Jul 17 '05 #1
1 8188
cg_news wrote:
I understand that the GregorianCalendar can be used to calculate the week
of year for a date, and after instantiating a GregorianCalendar object
with the no arg constructor, I can infact retrieve the current week (since
it initializes itself to the current date). However, when I try to create
a GregorianCalendar object by specifying a date in the constructor, it
returns an incorrect value for the WEEK_OF_YEAR field? This is
demonstrated by the code below.

Is there something in need to do to 'initialize' this field in the
calendar object? Am I not understanding the use of this object or is there
something I am missing here?

Any help or advice would be greatly appreciated.
Carl.

--- code ---
import java.util.*;

public class WeekTest {

private GregorianCalendar cal;
public WeekTest(){
// this works...
this.cal = new GregorianCalendar();
setupWeek();
}
public WeekTest(int year, int month, int day){
// this does not work?
this.cal = new GregorianCalendar(
year, month, day, 01, 01, 01);
setupWeek();
}
private void setupWeek() {
this.cal.setFirstDayOfWeek(Calendar.SUNDAY);
this.cal.setMinimalDaysInFirstWeek(4);
}

public void show(){
System.out.println(this.cal.toString());
System.out.println("Dare: " + this.cal.get(Calendar.MONTH)
+
"-" + this.cal.get(Calendar.DATE) +
"-" + this.cal.get(Calendar.YEAR));
System.out.println("Week: " +
this.cal.get(Calendar.WEEK_OF_YEAR) +
" Year: " +
this.cal.get(Calendar.YEAR));
}
public static void main(String[] args){
//this seems to work fine...
WeekTest wc = new WeekTest();
wc.show();

System.out.println();

//this does not !!!
WeekTest wc2 = new WeekTest(2004, 01, 01);
wc2.show();
}
}
--- end-code ---


Nevermind,

Someone already informed me of my error. I was missing the bit about
calendar months being zero based (i.e. 2004, 01, 01 is infact Feb 1, 2004).

Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: chennakeshava_ramesh | last post by:
hi, I have a problem, I am not able to find out which day of the week it is using the calendar class. I am using set() function to set the date and want to find out which day i.e mon,tue etc of...
2
by: Rustan | last post by:
Hi Im using GregorianCalendar to find out the current years week numbers. When the user chooses a week number in a dropdown i want to show that week in a table with the corresponding dates. For...
1
by: David | last post by:
Hi, I have a problem with returning a value from an external function..... My asp page is basically a list taken from a database. A date record is written from the DB, then all the...
3
by: Wired Hosting News | last post by:
Lets say I have 10 products in 10 different stores and every week I get a report from each store telling me how many items they have left for each of the 10 products. So each week I enter in 100...
5
by: Wired Hosting News | last post by:
I tried to be breif and give a scenario so as not to be overlooked because it was soooo long. Let me give you real world. I am a manufacturer of goods and produce 11 items that are distributed...
1
by: AliceG | last post by:
Hi all, By using JOptionPane, I'm trying to do this little program with Notepad for generating a password from the user's first two letters of the first name, the age calculated as a difference of...
1
by: cnixuser | last post by:
I am currently working on an "experimental" simple java program to allow me to learn how to work with arrays of GregorianCalendars. Is there a reason why these two lines of code temp2.set(1902, 11,...
1
by: alicat6 | last post by:
Hello, I need some help writing code that will calculate late fees. The code needs to return 0.0 if the item is not overdue and caluclate a late fee of .75 for every day overdue. Below is what I...
5
by: CKKwan | last post by:
Dear All, Anybody know how can I get the Work Week of a given date using CSharp? Example, in SQL Server we can get using DATEPART(ww, date); I need something equavalent. Thanks In Advance
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.