1
我期待DateTimeFormatter返回null或什麼,如果它的方法「公共字符串打印(ReadableInstant即時)」傳遞一個零日期時間(這也是一個ReadableInstant。)喬達時間的DateTimeFormatter意外的行爲?
令人驚訝的是返回當前時間,而不是拋出的一個錯誤。這可能是一個錯誤?
下面是相關的代碼 -
import org.joda.time.*;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class JodaTest {
public static void main(String[]args){
DateTime dt = null;
String ss = getTimeInAFormat(dt);
System.out.println(ss);
}
public static String getTimeInAFormat(DateTime time) {
DateTimeFormatter dtf = DateTimeFormat.forPattern("MM-dd-YYYY hh:mm");
String dateTime = dtf.print(time); // Should throw an error or return,
return dateTime; // a String = "NO DATE SET"
}
}
確定。我知道了。但我很驚訝地看到爲什麼開發人員想要提供非空輸出。也許只是爲了節省創建DateTime所需的時間,然後將其傳遞給打印方法。 – 2013-03-24 22:48:44