我應該如何設置日期? (How should I set the date?)


問題描述

我應該如何設置日期? (How should I set the date?)

I want to modify the date in Java. That's no problem, but I'm a bit confused about how I should do this.

1.

Calendar c = Calendar.getInstance(); 
c.set(2010,9,10,6,0);

2.

Calendar c = Calendar.getInstance();
c.set(c.YEAR, 2010);
c.set(c.MONTH, 9);
c.set(c.DATE, 10);
c.set(c.HOUR, 6);
c.set(c.MINUTE, 0);

Which one is the better way? If I use the second way I have to type a bit more and I learned that the code should be as short as possible. On the other side the second way is a bit more clear.


參考解法

方法 1:

Try this, but ctrl+space could help you next time.

Calendar cal = Calendar.getInstance();
    cal.set(year, month, date, hourOfDay, minute);

Thanks

方法 2:

The ONLY way to modify a date is in one atomic operation.  Doing it a field at a time will result in surprisingly incorrect results sometimes.  When you create a new Calendar it gets initialized with todays date.  Suppose today is May 31st and you set the month to June, intending to set the day next.  When you set the month, to June, 31 is no longer valid, so the date gets "normalized" to July 1st. You then set the day, but now the month is wrong.  

You can construct quite a few of these "interesting" cases.  Some will fail if you set the month first, and some will fail if you set the day first, so there's no order that is guaranteed to work for all cases.  Set the date as a single operation.

Here are some examples:

Today         Changes             Result
5/31/2013     MM‑>6, DD‑>12       7/12/2013 (not 6/12)
6/01/2013     DD‑>31, MM‑>07      7/01/2013 (not 7/31)

方法 3:

A short code is not all time the good way in Java (or other language). You have to be clear for you and for other developers who want to use your code. 

So, in my opinion, you can use the first method but with more java doc and comments than the second method which is the most explicit (attention : the second method is the most explicit but it need comments too...).

方法 4:

Well Stefan, I think you should go for the cleaner approach. The first method has only two lines of code to solve your problem . You should adopt it. From my development experience, I have learned that the LOC (Line Of Code) really matters in the maintenance of code and we should always think about solving a problem with minimum lines of code. So I think you should take advantage of multi‑parameters setter API. Thanks! 

(by StefanFelquirJim GarrisonLePeruvienSR‑Rehman)

參考文件

  1. How should I set the date? (CC BY‑SA 3.0/4.0)

#java #Date #calendar






相關問題

電子郵件地址中帶有 + 字符的 Java 郵件 (Java mail with + character in email address)

如何快速原型化 Java 代碼? (How to quickly prototype Java code?)

如何使用 Maven 在目標(SVN-)服務器上創建 Javadoc? (How to create Javadoc on the target (SVN-) server using Maven?)

為什麼檢查二叉樹有效性的解決方案不起作用? (Why the solution for checking the validity of binary tree is not working?)

Selenium webdriver通過第一個數字找到texy (Selenium webdriver find texy by first digits)

setOnClickListener 沒有在圖像視圖上被調用 (setOnClickListener is not getting called on image view)

繪製多邊形:找不到錯誤 (Drawing Polygon : unable to find error)

半透明 JButton:對像出現在背景中 (Semi-Transparent JButton: Objects appear in Background)

比較同一數組的元素 (Compare elements of the same array)

Java 屏幕截圖小程序 (Java screen capture applet)

Minecraft 1.8.9 Forge Modding 的Java 開發工具包,需要什麼JDK/JRE,代碼是否正確? (Java Development Kit with Minecraft 1.8.9 Forge Modding, What JDK/JRE Is Needed, Is Code Correct?)

java while (resultset.next()) 不返回同一列中的所有數據 (java while (resultset.next()) does not return all data in the same column)







留言討論