lfernandes
lfernandes

Reputation: 55

Format a Date object with DateFormat in Java

I'm trying to learn about Date objects and the DateFormat class and I keep getting an error in the examples I'm trying to do. The goal is to get a due date by adding 30 days to a pretend invoice date, and then to format that due date. The dueDate method, I believe, is correct, but I'm having trouble formatting it properly.

Here is the first thing I have that takes the invoice date and adds 30 days to it.

public Date getDueDate()
{
    Calendar cal = new GregorianCalendar();
    cal.setTime(getInvoiceDate());
    cal.add(Calendar.DATE, 30);
    Date dueDate = cal.getTime();
    return dueDate;
}

The next part is where I'm having the trouble, as it keeps telling me it expects a Date object but is receiving a String and I'm not sure why, as I'm supplying a Date object.

public Date getFormattedDueDate()
{
    Date dueDate = getDueDate();
    DateFormat shortDate = DateFormat.getDateInstance(DateFormat.SHORT);
    return shortDate.format(dueDate);        
}

Can anyone help me figure out why it's telling me that my supplied variable (dueDate) is a String when it's coded as a Date object?

Upvotes: 0

Views: 1078

Answers (3)

Basil Bourque
Basil Bourque

Reputation: 338624

The answer by Shamse is correct.

For the heck of it, here's the same kind of code but:

  • Written using the third-party library, Joda-Time 2.3
  • Care taken with time zones. Depending on default time zones is risky.
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.

java.util.Date date = new Date(); // = getInvoiceDate();
org.joda.time.DateTime invoiceStoredDateTime = new org.joda.time.DateTime( date );
// Set to desired time zone. Ideally that invoice date was stored in UTC.
// Time Zone list: http://joda-time.sourceforge.net/timezones.html
org.joda.time.DateTimeZone denverTimeZone = org.joda.time.DateTimeZone.forID( "America/Denver" );
org.joda.time.DateTime invoiceZonedDateTime = invoiceStoredDateTime.toDateTime( denverTimeZone );
// Call method .withTimeAtStartOfDay() to set the time component to first moment of the day.
org.joda.time.DateTime dueDateInThirtyDays = invoiceZonedDateTime.plusDays( 30 ).withTimeAtStartOfDay();
org.joda.time.DateTime dueDateInOneMonth = invoiceZonedDateTime.plusMonths( 1 ).withTimeAtStartOfDay(); // Smart month calculation, aiming at same day number of month.
// Style – Specify a character of 'S' for short style, 'M' for medium, 'L' for long, and 'F' for full. First for date, second for time.
// A date or time may be omitted by specifying a style character '-'.
String dueDateAsString = org.joda.time.format.DateTimeFormat.forStyle("S-").withLocale( Locale.US ).print( dueDateInThirtyDays );
org.joda.time.DateTime dueDateInUtcForStorage = dueDateInThirtyDays.toDateTime( org.joda.time.DateTimeZone.UTC );

Show values on the console:

System.out.println( "date: " + date );
System.out.println( "invoiceZonedDateTime: " + invoiceZonedDateTime );
System.out.println( "dueDateInThirtyDays: " + dueDateInThirtyDays );
System.out.println( "dueDateInOneMonth: " + dueDateInOneMonth );
System.out.println( "dueDateAsString: " + dueDateAsString );
System.out.println( "dueDateInUtcForStorage: " + dueDateInUtcForStorage );

When run…

date: Thu Nov 28 13:39:05 PST 2013
invoiceZonedDateTime: 2013-11-28T14:39:05.125-07:00
dueDateInThirtyDays: 2013-12-28T00:00:00.000-07:00
dueDateInOneMonth: 2013-12-28T00:00:00.000-07:00
dueDateAsString: 12/28/13
dueDateInUtcForStorage: 2013-12-28T07:00:00.000Z

Upvotes: 0

Baxter Lopez
Baxter Lopez

Reputation: 1129

Shamse is right

shortDate.format(dueDate);

returns a String, you can easly fix this changing your return type

public String getFormattedDueDate()
{
 Date dueDate = getDueDate();
 DateFormat shortDate = DateFormat.getDateInstance(DateFormat.SHORT);
 return shortDate.format(dueDate);        
} 

Upvotes: 0

Shamse Alam
Shamse Alam

Reputation: 1315

format(Date date) Formats a Date into a date/time String.

Upvotes: 1

Related Questions