2014-07-24 43 views
4

我有一個時間字符串像「15:30」我想比較該字符串與當前時間。 請提出一些簡單的建議。 以及如何以小時分鐘格式(「HH:mm」)獲取當前時間如何在java中比較時間字符串與當前時間?

+0

參考SimpleDateFormat的http://stackoverflow.com/questions/3056703/simpledateformat – CycDemo

+0

你想「現在」的範圍?例如在「12:34.999」,12:34是比賽,但是1毫秒後不是 - 是你想要的嗎? – Bohemian

+0

我只想比較當前時間是否大於時間字符串。 –

回答

8

TL;博士

LocalTime.now() 
     .isAfter(LocalTime.parse("15:30")) 

詳細

你應該考慮周圍的其他方法:如何得到這個字符串變成了一個時間值。您不會通過將您的數字轉換爲字符串來嘗試數學。日期時間值也是如此。

避免舊的捆綁類,java.util.Date和.Calendar,因爲它們非常麻煩,在設計和實現上都存在缺陷。它們被Java 8中的新java.time package所取代。 java.time的靈感來源於Joda-Time

java.timeJoda-Time都提供了一個時間段來捕獲一個時間段,沒有任何日期到時區:LocalTime

java.time

使用內置到Java的java.time類,特別是LocalTime。獲取當地時區的當前時間。根據您的輸入字符串構建時間。與isBeforeisAfterisEqual方法進行比較。

LocalTime now = LocalTime.now(); 
LocalTime limit = LocalTime.parse("15:30"); 
Boolean isLate = now.isAfter(limit); 

更好地指定您想要的/預期的時區,而不是隱式地依賴於JVM當前的默認時區。

ZoneId z = ZoneId.of("Pacific/Auckland") ; 
LocalTime now = LocalTime.now(z); // Explicitly specify the desired/expected time zone. 
LocalTime limit = LocalTime.parse("15:30"); 
Boolean isLate = now.isAfter(limit); 

喬達時間

在這種情況下,使用喬達時庫代碼正好是幾乎與上面相同見過java.time的代碼。

請注意,Joda-Time項目現在在maintenance mode,團隊建議遷移到java.time類。


關於java。時間

java.time框架內置於Java 8及更高版本。這些類取代了日期時間類legacy,如java.util.Date,Calendar,& SimpleDateFormat

Joda-Time項目現在位於maintenance mode,建議遷移到java.time類。請參閱Oracle Tutorial。並搜索堆棧溢出了很多例子和解釋。規格是JSR 310

從何處獲取java.time類?

ThreeTen-Extra項目與其他類擴展java.time。這個項目是未來可能增加java.time的一個試驗場。您可以在這裏找到一些有用的類,如Interval,YearWeek,YearQuartermore

+0

感謝您的回覆,我使用的是Java 6,你能否提供相同的解決方案java 6 –

+1

@mayanksharma [Joda-Time 2.3](http://www.joda.org/ joda-time /)在Java 5,6,7和8中運行。只需將[jar文件](http://sourceforge.net/projects/joda-time/files/joda-time/)添加到您的項目中即可。 –

+0

@mayanksharma更新:Joda-Time項目現在處於維護模式。團隊建議遷移到java.time類。對於Java 6和7,您會發現許多在ThreeTen-Backport項目中移植的java.time功能。對於Android,ThreeTenABP。 –

3

以下將獲得時間。你可以不是用它來比較

String currentTime=new SimpleDateFormat("HH:mm").format(new Date()); 
5

只是字符串比較正常,像這樣:

String currentTime = new SimpleDateFormat("HH:mm").format(new Date()); 
String timeToCompare = "15:30"; 
boolean x = currentTime.equals(timeToCompare); 

如果時間相同x是true,如果他們不x是false

0

試試這個

public static String compareTime(String d) { 
     if (null == d) 
      return ""; 

     try { 

      SimpleDateFormat sdf= new SimpleDateFormat("HH:mm"); 
      d = sdf.format(new Date()); 
     } catch (Exception e) { 
      e.printStackTrace(); 

     } 

     return d; 

    } 

那麼你就可以用比較您的字符串

0

我有類似的情況,我需要比較像「15:30」時間字符串與當前時間。 我正在使用Java 7所以我不能使用Basil Bourque的解決方案。 所以我已經實現了一個方法,它需要一個字符串,如「15:30」和 檢查當前時間,如果當前時間在輸入時間之後,則返回

public static boolean checkSlaMissedOrNot(String sla) throws ParseException { 
     boolean slaMissedOrnot = false; 
     Calendar cal = Calendar.getInstance(); 
     cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(sla.substring(0, 2))); 
     cal.set(Calendar.MINUTE, Integer.parseInt(sla.substring(3))); 
     cal.set(Calendar.SECOND, 0); 
     cal.set(Calendar.MILLISECOND, 0); 
     if (Calendar.getInstance().after(cal)) { 
      System.out.println("it's SLA missed"); 
      slaMissedOrnot = true; 
     } else { 
      System.out.println("it's fine & not SLA missed"); 
     } 
     return slaMissedOrnot; 
    } 

日曆具有其他採用全方法,如after(Object when)

返回此日曆是代表在指定Object表示的時間之後的時間。 您也可以使用此方法將時間字符串與當前時間進行比較。

0

這是我的例子之前和當前時間之後進行比較:

MainActivity.java

Button search = (Button) findViewById(R.id.searchBus); 
     SimpleDateFormat sdfDate = new SimpleDateFormat("HH:mm"); 

     String limit = "23:00"; 
     Date limitBus=null; 
     try { 
      limitBus = sdfDate.parse(limit); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 

     String start = "07:15"; 
     Date startBus=null; 
     try { 
      startBus = sdfDate.parse(start); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 

     String user_time = getCurrentTimeStamp(); 
     Date now = null; 
     try { 
      now = sdfDate.parse(user_time); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 

     if (now.after(limitBus)||now.before(startBus)){ 
      Toast.makeText(this, "Bus Operation Hours : 7:15AM - 11.00PM", Toast.LENGTH_LONG).show(); 
      search.setEnabled(false); 
     } 

     else 
     { 
      search.setEnabled(true); 
     } 



//get current time method 
public static String getCurrentTimeStamp() { 
     SimpleDateFormat sdfDate = new SimpleDateFormat("HH:mm");//dd/MM/yyyy 
     Date now = new Date(); 
     String strDate = sdfDate.format(now); 
     return strDate; 
    } 
相關問題