• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

public abstract class Calendar

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do this:

Calendar cal = Calendar.getInstance();
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76829
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
pie. tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic