-1

in controller

@RequestMapping(value = "/testCalendar")
public String testCalendar(Calendar time){

    System.out.println(time == null);

    return "request ok" ;
}

and open in browser this url :127.0.0.1:8090/test/testCalendar

it got follwer error msg

springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.Calendar]: Is it an abstract class?; nested exception is java.lang.InstantiationException] with root cause java.lang.InstantiationException: null atsun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48) at java.lang.reflect.Constructor.newInstance(Constructor.java:423)

if i want got calendar with null,how can i do?

i try this

    @InitBinder
    public void initBinder(WebDataBinder binder) {
     binder.registerCustomEditor(Calendar.class, new CalendarEditor());
     binder.registerCustomEditor(Date.class, new DateEditor());
    }

but not still not working,please help me.

Pan Xinyi
  • 1
  • 1
  • 1
  • how do you send your object from your JSP ? – Mehdi Dec 07 '18 at 01:46
  • provide your CalendarEditor class also – Mehdi Dec 07 '18 at 01:47
  • i use browser request,not form commit.. – Pan Xinyi Dec 07 '18 at 01:55
  • CalendarEditor can work in springMVC, but in spring-boot can not work. – Pan Xinyi Dec 07 '18 at 01:57
  • 3
    I suggest you dont want to use a `Calendar`. That class has design problems and is long outdated (and yes, it’s abstract, the message is correct, so you cannot instantiate it). Instead use `ZonedDateTime` or another appropriate class from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Dec 07 '18 at 06:20
  • it also got exception `No primary or default constructor found for class java.time.LocalDateTime] ` – Pan Xinyi Dec 08 '18 at 01:32

1 Answers1

0

protected

I suspect the issue is that Calendar is a protected class. So you cannot instantiate from an unrelated class. See this other Answer.

One alternate route is to use the usual backing-class, GregorianCalendar.

java.time

The better solution is to avoid Calendar and GregorianCalendar altogether. These are terribly designed classes with many flaws. They were supplanted years ago with the adoption of JSR 310, implemented by the java.time classes.

ZonedDateTime

The replacement for GregorianCalendar is ZonedDateTime.

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;  // Capture the current moment as seen in the wall-clock time used by the people of a particular region (a time zone).

Lack of no-arg constructor

Note that the java.time classes purposely lack public constructors. So that also means a lack of no-arg constructors. Instead they use factory methods such as ZonedDateTime.now seen above. As a guide, study the java.time naming conventions.

If you have a framework needing a no-arg constructor, you will need to find a workaround or accommodation.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • thanks `ZonedDateTime` is lack of no-arg constructor. and framework can't instantiation it. i want find a way to custom instance `Calendar` method in spring boot. – Pan Xinyi Dec 11 '18 at 01:29