0

I am trying to do a simple exercise where I take a date, add 90 days to it, and format it to something like this:

Monday, April 20, 1998.

I am to do this using GregorianCalendar and DateFormat. So far, I have this compiling code, but I get a runtime error where I cannot format the given Object as Date:

import java.util.*;
import java.text.*;

class Assignment21 {
    public static void main(String args[]) {
        GregorianCalendar ddate = new GregorianCalendar(1994, 10, 20);
        ddate.add(Calendar.DAY_OF_MONTH, 90);
        SimpleDateFormat sdf = new SimpleDateFormat("EEE, MM dd, yyyy");
        String date = sdf.format(ddate);
    }
}

How can I correctly output the predefined GregorianCalendar date using DateFormat?

JMV12
  • 965
  • 1
  • 20
  • 52
  • 2
    `SimpleDateFormat` doesn't have a `format` method taking a `Calendar` parameter . – Arnaud Sep 25 '17 at 15:19
  • Will I have to use DateFormat? – JMV12 Sep 25 '17 at 15:28
  • Just 3 little details: 1- in `Calendar` API, months are zero-indexed (January is `0`), so month 10 is actually November (not sure if you're aware, but anyway it's something to take care). 2- To get exactly the format `Monday, April 20, 1998`, you should use `new SimpleDateFormat("EEEE, MMMM dd, yyyy", Locale.US)` - the `java.util.Locale` specifies the language (if you don't set it, it'll use the JVM default, and it's not guaranteed to always be English). 3- Prefer to use - if possible - the new java.time API, explained in [@Basil Bourque's answer](https://stackoverflow.com/a/46408956/7605325). –  Sep 25 '17 at 15:58
  • 1
    FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Apr 10 '18 at 04:48

2 Answers2

2

You have to correct your code:

instead of

String date = sdf.format(ddate);

try:

String date = sdf.format(ddate.getTime());
assembler
  • 3,098
  • 12
  • 43
  • 84
2

tl;dr

LocalDate.of( 1994 , Month.OCTOBER , 20 )                  // Generate a date-only value, a `LocalDate` object, without time-of-day and without time zone.
.plusDays( 90 )                                            // Add a span of time. Using immutable objects, a new `LocalDate` object is instantiated, without altering the first.
.format(                                                   
    DateTimeFormatter.ofLocalizedDate( FormatStyle.FULL )
                     .withLocale( Locale.US )
)

Wednesday, January 18, 1995

java.time

You are using troublesome old date-time classes that are now legacy, supplanted by the java.time classes.

LocalDate ld = LocalDate.of( 1994 , 10 , 20 ) ; // Sane numbering for year and month, unlike legacy classes. '1994' = 1994, and 10 = October.
LocalDate ldLater = ld.plusDays( 90 ) ;

Or use Month enum.

LocalDate ld = LocalDate.of( 1994 , Month.OCTOBER , 20 ) ; 
LocalDate ldLater = ld.plusDays( 90 ) ;

Let java.time automatically localize for you.

DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.FULL ).withLocale( Locale.US ) ;
String output = ldLater.format( f ) ;

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