File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

public abstract class Calendar

Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77

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?


SCJP 1.4
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15082

Do this:

Calendar cal = Calendar.getInstance();

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77

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 ()?

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
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:- package system does not exist
1 error
[Campbell@dhcppc0 ~]$ javac
[Campbell@dhcppc0 ~]$ java CalendarTest
endTime=3600000,endTimeMode=2]],firstDayOfWeek= 1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2007,MONTH=2,WEEK_OF_YEAR=11,
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:
subject: public abstract class Calendar
It's not a secret anymore!