我有這些代碼以取得今天的日期在我的服務器減去日至昨日獲得最新
DefaultTableModel dnow = MyDB.DataTable("SELECT date_format(now(),'%m-%d-%Y')");
而且這些代碼的格式的日期。
SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
現在我怎麼能得到昨天的日期? 我應該減去一個嗎?
我有這些代碼以取得今天的日期在我的服務器減去日至昨日獲得最新
DefaultTableModel dnow = MyDB.DataTable("SELECT date_format(now(),'%m-%d-%Y')");
而且這些代碼的格式的日期。
SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
現在我怎麼能得到昨天的日期? 我應該減去一個嗎?
的另一種方法是做數學題的SQL。該聲明可能會因您使用的數據庫平臺而異。
DefaultTableModel dnow =
MyDB.DataTable("SELECT date_format(now() - INTERVAL 1 DAY,'%m-%d-%Y')");
非常感謝! – kelvz 2013-04-25 03:34:41
這應該工作適合你,
Calendar cal = Calendar.getInstance()
cal.add(Calendar.DATE, -1);
System.out.println("Yesterday's date = "+ cal.getTime());
這隻會減去1日,從當前日曆日期,爲您提供方昨日
不,建立一個Date
(或更好,但使用joda「 s DateTime
對象),然後做你的算術。我給你兩個解決方案,一個與喬達和其他沒有,從沒有:
Date d = format.parse(dnow.getDataVector().get(dnow.getTable().getSelectedRow()));
d.add(Calendar.DATE, -1);
現在,使用喬達:
DateTime d = new DateTime(format.parse(dnow.getDataVector().get(dnow.getTable().getSelectedRow()))).minusDays(1);
如果你在內部存儲您的時間戳爲POSIX時間(因爲MN 1970年1月1日毫秒),那麼你可以添加或一天輕鬆減去的任何時間戳記爲:
Date today = new Date(); // today
Date tomorrow = new Date(today.getTime() + (24 * 60 * 60 * 1000)); // tomorrow
巨大POSIX時間的附加值是始終使用UTC。 UTC是一個全球性的「固定」參考點。然後,如果您需要在任何時區,任何夏令時區域中爲用戶顯示此日期,那麼對於在Olson時區數據庫中記錄的任何地點,您只需創建日曆:
GregorianCalendar gc = new GregorianCalendar(TimeZone.getTimeZone("America/Chicago"));
SimpleDateFormat sdf = new SimpleDateFormat();
sdf.setCalendar(gc);
sdf.applyPattern("MM-dd-yyyy");
// Now your formatter is set with the pattern and a time zone-aware Calendar.
// The world is at your command
String myOutput = sdf.format(tomorrow);
我強烈建議以某種形式的UTC表示在數據模型內部處理時間戳。使用POSIX時間(或Julian天數或修改的Julian天數)進行日期算術很容易,Java日期/時間API有足夠的能力來處理大多數本地時間轉換,而無需大驚小怪。
[如何檢查日期對象是否等於昨天?](http://stackoverflow.com/q/3006150/642706)和[獲取昨天 - 類型爲Date的方法getDate()已被棄用] (http://stackoverflow.com/q/14938253/642706)。 – 2014-06-13 20:54:20