本文介紹了如何比較Java中的兩個字符串日期?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我有兩個字符串格式的日期,如下所示 -
I have two dates in String format like below -
String startDate = "2014/09/12 00:00";
String endDate = "2014/09/13 00:00";
我想確保 startDate 應該小于 endDate.startDate 不應大于 endDate.
I want to make sure startDate should be less than endDate. startDate should not be greater than endDate.
如何比較這兩個日期并相應地返回布爾值?
How can I compare these two dates and return boolean accordingly?
推薦答案
將它們轉換為實際的 Date
對象,然后調用 before
.
Convert them to an actual Date
object, then call before
.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd h:m");
System.out.println(sdf.parse(startDate).before(sdf.parse(endDate)));
回想一下,parse
將拋出 ParseException
,因此您應該在此代碼塊中捕獲它,或者將其聲明為作為方法簽名的一部分拋出.
Recall that parse
will throw a ParseException
, so you should either catch it in this code block, or declare it to be thrown as part of your method signature.
這篇關于如何比較Java中的兩個字符串日期?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!