File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes public abstract class Calendar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "public abstract class Calendar" Watch "public abstract class Calendar" New topic
Author

public abstract class Calendar

Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77
Hi

This is my code:

import java.sql.Date;
import java.util.Calendar;

class A {


public static void main (String [] args) {

Calendar x = new Calendar ();

}
}

One can not compile it: java.util.Calendar is abstract; cannot be instantiated

I read in the API. There are two constructors in class Calendar.

How can I use them? At the moment they seem to be useless for me, an abstract class can never be instantiated, class Celandar is abstract.

How can I create an object of the type Calendar?

Regards
Urs


SCJP 1.4
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14146
    
  18

Do this:

Calendar cal = Calendar.getInstance();


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

Joined: Mar 13, 2007
Posts: 77
Hi

Could you explain me:
Calendar cal = Calendar.getInstance();

My initial question was: How can I use the constructor Calendar ()?

Could anyone give an example with the constructor Calendar ()?

Regards
Urs
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38788
    
  23
Look at the API for the getInstance() methods. Get a printout, which looks something like this, and note the seplling error which caused the compiler error:-
CalendarTest.java:8: package system does not exist
system.out.println(myCalendar);
^
1 error
[Campbell@dhcppc0 ~]$ javac CalendarTest.java
[Campbell@dhcppc0 ~]$ java CalendarTest
java.util.GregorianCalendar[time=1173862262095,
areFieldsSet=true,areAllFieldsSet=true,lenient=true,
zone=sun.util.calendar.ZoneInfo[id="Europe/London",
offset=0,dstSavings=3600000,useDaylight=true,transitions=242,
lastRule=java.util.SimpleTimeZone[id=Europe/London,offset=0,
dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,
startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,
startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,
endTime=3600000,endTimeMode=2]],firstDayOfWeek= 1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2007,MONTH=2,WEEK_OF_YEAR=11,
WEEK_OF_MONTH=3,DAY_OF_MONTH=14,DAY_OF_YEAR=73,DAY_OF_WEEK=4,
DAY_OF_WEEK_IN_MONTH=2,AM_PM=0,HOUR=8,HOUR_OF_DAY=8,MINUTE=51,
SECOND=2,MILLISECOND=95,ZONE_OFFSET=0,DST_OFFSET=0]
You can see it defaults to Gregorian Calendar, and you ought to be able to get 8.51am, 14th March 2007 out of the printout somewhere.
The code I am using looks like this:-

You can't use the Calendar() constructor at all. Close examination of the constructors (in the above mentioned API page) shows that they have protected access; you cannot get access to them from outside the java.util package. The reason the constructors exist is to be called from subclasses.

[edit]Inserted line breaks, corrected spelling errors, and added the code[/edit]
[ March 14, 2007: Message edited by: Campbell Ritchie ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: public abstract class Calendar