File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extending java.util.Calendar()

 
Ingudam Manoranjan
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ingudam Manoranjan
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesper. I will check this up.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic