Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

public abstract class Calendar  RSS feed

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Java Cowboy
Sheriff
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do this:

Calendar cal = Calendar.getInstance();
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 59769
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!