Just have small program to practice with Calendar. However I come accross something unusual. Once you set date for 1st April 2007 or 1st June 2008 for example you get wrong number of the week.

example bellow show code set to date 1st June 2008

import java.util.*;
import java.text.DateFormat;

public class Cas
{
	public static void main(String[] agrs)
	{
		Calendar calendar = null;
		calendar = new GregorianCalendar();
		
		//Set date to 1st June 2008
		calendar.setTime(new Date(2008-1900, 5, 1));
		
		System.out.println("Integer of day in the week " + calendar.get(Calendar.DAY_OF_WEEK) );
		System.out.println("Month integer " + calendar.get(Calendar.MONTH));
		System.out.println("Last day in the month " + calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		System.out.println("Week of year " + calendar.get(Calendar.WEEK_OF_YEAR) );
	}
}

output is

Integer of day in the week 1
Month integer 5
Last day in the month 30
Week of year [B]22[/B]

Week number should be 23

Any idea how to get around this problem?

Recommended Answers

All 3 Replies

It is the correct number. Remember most things in Java are zero based :)

sorry jwenting, you wrong in this point. Try to run that program and you will see. 1st January 2007 week number is 1, 31st May 2008(Sunday) is week number 22 and 1st June 2008(Monday) is week number 22

import java.util.*;
import java.text.DateFormat;

public class Cas
{
	public static void main(String[] agrs)
	{
		Calendar calendar = null;
		calendar = new GregorianCalendar();
		
		//Set date to 1st June 2008
		calendar.setTime(new Date(2008-1900, 5, 1));
		
		System.out.println("Integer of day in the week " + calendar.get(GregorianCalendar.DAY_OF_WEEK) );
		System.out.println("Month integer " + calendar.get(GregorianCalendar.MONTH));
		System.out.println("Last day in the month " + calendar.getActualMaximum(GregorianCalendar.DAY_OF_MONTH));
		System.out.println("Week of year " + calendar.get(GregorianCalendar.WEEK_OF_YEAR) );
	}
}

GregorianCalendar is answer to my troubles :cheesy:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.