This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Extending java.util.Calendar() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Extending java.util.Calendar()" Watch "Extending java.util.Calendar()" New topic
Author

Extending java.util.Calendar()

Ingudam Manoranjan
Ranch Hand

Joined: Jul 31, 2006
Posts: 48
Hi,
I am trying to extend the Calendar(). Following is the code.

import java.sql.Timestamp;

public class Calendar extends java.util.Calendar{
public Calendar() {
super();
System.out.println("Calendar after super(): " + super.getTime());

//if (AppMode.isTestMode() == true)
if (true == true)
{
//java.util.Calendar cal = java.util.Calendar.getInstance();
super.setTime(new Timestamp(System.currentTimeMillis()));

//Get the configured date from Application
java.util.Calendar configuredCal = MyDate.getConfiguredCal();

super.set( configuredCal.get(Calendar.YEAR),configuredCal.get(Calendar.MONTH), configuredCal.get(Calendar.DATE));

}
}
}

In my test class


Calendar cal = new Calendar(); //Uses my calendar
System.out.println(" Current Calendar --"+cal.getTime());

The output is coming as the current date even though:
Current Calendar --Tue Aug 22 17:53:19 IST 2006

Though the configured Date returned is
Config cal value is : Wed May 12 00:00:00 IST 2004

super.set( configuredCal.get(Calendar.YEAR),configuredCal.get(Calendar.MONTH), configuredCal.get(Calendar.DATE));

is settting the date properly.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Your code doesn't show to what date and time configuredCal is set to, so the code isn't sufficient to explain why you get that output.

Why are you extending class java.util.Calendar? That class isn't meant to be subclassed by client programs. What are you trying to achieve by subclassing class java.util.Calendar?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ingudam Manoranjan
Ranch Hand

Joined: Jul 31, 2006
Posts: 48
SOrry, I didn't explicitly mention in my post.

//Get the configured date from Application
java.util.Calendar configuredCal = MyDate.getConfiguredCal();

will return a calendar object with configuration as follows

Config cal value is : Wed May 12 00:00:00 IST 2004
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

So what are you confused about now? The line:

super.set( configuredCal.get(Calendar.YEAR),configuredCal.get(Calendar.MONTH), configuredCal.get(Calendar.DATE));

sets the Calendar to the date that's in configuredCal, so if configuredCal is set to May 12, 2004 then the calendar is ofcourse also set to that date.

Please explain clearly what the problem is. Explain exactly what you expect, what the actual output is and how it differs from what you expect, and show us a complete (compileable) short program that reproduces the problem.

What does the method MyDate.getConfiguredCal() look like?
[ August 22, 2006: Message edited by: Jesper Young ]
Ingudam Manoranjan
Ranch Hand

Joined: Jul 31, 2006
Posts: 48
Actual Result: Tue Aug 22 17:53:19 IST 2006
Expected Result: Wed May 12 17:53:19 IST 2004

public class MyDate
{

public static Calendar getConfiguredCal()
{
String strStaticDate = "2004-05-12";

SimpleDateFormat formatterDateTime = new SimpleDateFormat("yyyy-MM-dd");
Calendar configuredCal = Calendar.getInstance();
try{
configuredCal.setTime(formatterDateTime.parse(strStaticDate));
}
catch (java.text.ParseException e)
{
e.printStackTrace();
}
return configuredCal;
}
}

import java.sql.Timestamp;
public class Calendar extends java.util.Calendar{

public Calendar() {
super();
System.out.println("Calendar after super(): " + super.getTime());
//if (AppMode.isTestMode() == true)
if (true == true)
{
super.setTime(new Timestamp(System.currentTimeMillis()));
java.util.Calendar configuredCal = InfyDate.getConfiguredCal();
super.set( configuredCal.get(Calendar.YEAR),configuredCal.get(Calendar.MONTH), configuredCal.get(Calendar.DATE));
}

}
protected void computeTime() {}

protected void computeFields() { }

public void add(int arg0, int arg1) {}

public void roll(int arg0, boolean arg1) {}

public int getMinimum(int arg0) {
return 0;
}

public int getMaximum(int arg0) {
return 0;
}

public int getGreatestMinimum(int arg0) {
return 0;
}
public int getLeastMaximum(int arg0) {
return 0;
}

}

public class Tester
{

public static void main(String arg[])
{

Calendar cal = new Calendar();
System.out.println(" Current Calendar --"+cal.getTime());
}

}
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Ok, thank you...

The problem is that you have overridden the methods computeTime(), computeFields() etc. but that you do not call super.computeTime() and super.computeFields() in your own methods.

Class java.util.Calendar calls these methods to convert the separate fields (year, month, day) that you set by calling the set(...) method to the time value that is stored internally in the calendar.
Ingudam Manoranjan
Ranch Hand

Joined: Jul 31, 2006
Posts: 48
Thanks Jesper. I will check this up.
 
 
subject: Extending java.util.Calendar()
 
Similar Threads
Java util Date & Calendar - 1900 years
java calander class
java calander class
How to get end week date from given start date??
date question