-4

I am working on a Java application and I have some problem trying to create a Date object:

So I have done:

Calendar dataRendimentoLordoCertificatoCalendar = Calendar.getInstance();
dataRendimentoLordoCertificatoCalendar.set(annoCorrente - 1, 10, 01);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dataRendimentoLordoCertifiacato = new Date(sdf.format(dataRendimentoLordoCertificatoCalendar.getTime()));

Using the Eclipse debugger I can see that the value of sdf.format(dataRendimentoLordoCertificatoCalendar.getTime()) (passed as parameter to the Date constructor) is 2015-11-01 (and it is what I expect: the first of November of 2015).

The problem is that when this line is performed:

Date dataRendimentoLordoCertifiacato = new Date(sdf.format(dataRendimentoLordoCertificatoCalendar.getTime()));

I obtain this exception:

Exception in thread "main" java.lang.IllegalArgumentException
    at java.util.Date.parse(Date.java:598)
    at java.util.Date.<init>(Date.java:255)
    at com.mycompany.dbmanager.MyProject.getRendimentoLordoCertificato(PucManager.java:64)

How can I fix this issue?

halfer
  • 19,824
  • 17
  • 99
  • 186
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • 8
    What on earth are you trying to do? `dataRendimentoLordoCertificatoCalendar.getTime()` is **already** a `Date`. P.S. with the release of Java 8, I would strongly suggest you avoid `Date` and `Calendar` like the plague wherever possible. – Boris the Spider Aug 24 '16 at 13:36
  • 4
    *"I can see that the value of sdf.format(dataRendimentoLordoCertificatoCalendar.getTime()) (passed as parameter to the Date constructor) is 2015-11-01"* No, it's a **`Date` instance** containing that date. It makes no sense to turn that into a string and then pass that string raw into the `Date` constructor. Just use the `Date` you already have. – T.J. Crowder Aug 24 '16 at 13:38
  • 1
    And if not using Java 8, use Joda-Time. Avoid using `Calendar` and `Date`. – RealSkeptic Aug 24 '16 at 13:41

2 Answers2

1

I don't know why you want this solution, but if you want to use String (returned type of getTime), you have to use parse, like this:

public static void main(String[] args) throws ParseException {
  Calendar dataRendimentoLordoCertificatoCalendar = Calendar.getInstance();

  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  Date dataRendimentoLordoCertifiacato = sdf.parse(sdf.format(dataRendimentoLordoCertificatoCalendar.getTime()));

  System.out.println(dataRendimentoLordoCertifiacato);
}

But, I don't know what exactly you want. Because you have a DATE, then you are gettin String of this date and then you parse it back to date :-D

Hrabosch
  • 1,541
  • 8
  • 12
0

@Hrabosch is right.

However I am assuming you are doing this because you want to get rid of the time part and only want the date i.e. date at midnight. You can use the java.time classes built into Java 8 and later.

LocalDate date = LocalDate.of(2015, Month.JANUARY, 1);

If not, there is Joda-Time library which has DateMidnight, so you can do:

DateMidnight dateMidnight = new DateMidnight(2015, 1, 1);
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Kiran K
  • 703
  • 4
  • 11
  • 2
    All of the “midnight” classes and methods in Joda-Time are deprecated. The team recommends replacing their usage with a call to [`withTimeAtStartOfDay`](http://www.joda.org/joda-time/apidocs/org/joda/time/DateTime.html#withTimeAtStartOfDay--). Also, Joda-Time has a [`LocalDate`](http://www.joda.org/joda-time/apidocs/org/joda/time/LocalDate.html) class. – Basil Bourque Aug 24 '16 at 14:23
  • Much of the java.time functionality is back-ported to Java 6 & 7 in [ThreeTen-Backport](http://www.threeten.org/threetenbp/) and further adapted to [Android](https://en.wikipedia.org/wiki/Android_(operating_system)) in [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) (see [*How to use…*](http://stackoverflow.com/q/38922754/642706)). – Basil Bourque Aug 24 '16 at 23:18