使用Java中的日期格式格式化Date对象

时间:2013-11-28 18:56:50

标签: java date date-format

我试图了解Date对象和DateFormat类,并且在我试图做的示例中我一直收到错误。目标是通过在假装发票日期中添加30天来获得到期日期,然后格式化该截止日期。我相信,dueDate方法是正确的,但我在格式化它时遇到了麻烦。

这是我的第一件事,它采用了发票日期并为其增加了30天。

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

接下来的部分是我遇到麻烦的地方,因为它一直告诉我它需要一个Date对象,但是收到一个字符串,我不知道为什么,因为我提供了一个日期对象。

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

任何人都可以帮我弄明白为什么当它被提供的变量(dueDate)是一个字符串时它被编码为Date对象?

3 个答案:

答案 0 :(得分:1)

format(Date date)Date格式化为日期/时间String

答案 1 :(得分:0)

Shamse是对的

shortDate.format(dueDate);

返回一个String,你可以轻松修复这个改变你的返回类型

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

答案 2 :(得分:0)

Shamse的answer是正确的。

对于它来说,这是相同类型的代码,但是:

  • 使用第三方库Joda-Time 2.3
  • 编写
  • 注意时区。根据默认时区是有风险的。
// © 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 );

在控制台上显示值:

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 );

跑步时......

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