簡體   English   中英

在Java中使用DateFormat格式化Date對象

[英]Format a Date object with DateFormat in Java

我正在嘗試了解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對象,但是正在接收String,並且我不確定為什么,因為我正在提供Date對象。

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

誰能幫我弄清楚為什么當我將提供的變量(dueDate)編碼為Date對象時,它告訴我的是String嗎?

format(Date date)Date格式化為日期/時間String

假的是對的

shortDate.format(dueDate);

返回一個字符串,您可以輕松地解決此問題,更改返回類型

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

Shamse的答案是正確的。

對於此問題,這里是相同類型的代碼,但:

  • 使用第三方庫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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM