2015-10-16 120 views
0

我有給定日期,格式爲dd。 MMMM yyyy HH:mm'Uhr'檢查給定的日期時間是否在當前日期時間的範圍內(+/- 1小時)

現在我想用當前日期檢查這個日期,檢查它是否在當前日期時間的+1小時和-1小時範圍內,當它在此範圍內時,如果條件應該是合格的。

我將不勝感激,當有人可以幫助我!

順便說一句,我沒有機會使用JODA。

+0

[?如何比較在Java中的日期( http://stackoverflow.com/questions/2592501/how-to-compare-dates-in-java) – sam

+0

http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html –

+0

什麼是Uhr? –

回答

1

Eric張貼在另一個答案:Here's the link for credit.一個相當簡單的方法得到的除了使用Calendar類的時間。如果有什麼,你可以選擇它來了解兩次之間的差異。

public static int hoursAgo(String datetime) { 

    Calendar date = Calendar.getInstance(); 
    date.setTime(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.ENGLISH).parse(datetime)); // Parse into Date object 
    Calendar now = Calendar.getInstance(); // Get time now 
    long differenceInMillis = now.getTimeInMillis() - date.getTimeInMillis(); 
    long differenceInHours = (differenceInMillis)/1000L/60L/60L; // Divide by millis/sec, secs/min, mins/hr 
    return (int)differenceInHours; 

} 
+0

這正是我需要的,謝謝! :) – Matej

+0

完全沒問題,這是爲了感謝Eric,我不需要自己重寫代碼來證明我的觀點! –

+0

此代碼忽略了時區的問題。我不確定,因爲我避免了這些舊的日期時間問題,但我期望JVM當前的默認時區將應用於解析。這可能不是由此輸入字符串表示的時區。 –

0

可能是以下方法可以幫到你。

public static String getDiffBtwnDates(Date date1, Date date2){ 
     long date1InMillis = date1.getTime(); 
     long date2InMillis; 
     if (date2==null){ 
      date2InMillis = System.currentTimeMillis(); 
     }else{ 
      date2InMillis = date2.getTime(); 
     } 
     long dateDiffInMillis = date2InMillis-date1InMillis; 


     StringBuffer sTimeSince = new StringBuffer(""); 
     if(dateDiffInMillis > YEAR){ 
      if(dateDiffInMillis/YEAR>1){ 
       sTimeSince.append(dateDiffInMillis/YEAR).append(" Years "); 
       dateDiffInMillis %= YEAR; 
      }else { 
       sTimeSince.append(dateDiffInMillis/YEAR).append(" Year "); 
       dateDiffInMillis %= YEAR; 
      } 
     } 
     if (dateDiffInMillis > DAY) { 
      if(dateDiffInMillis/DAY > 1){ 
       sTimeSince.append(dateDiffInMillis/DAY).append(" Days "); 
       dateDiffInMillis %= DAY; 
      }else { 
       sTimeSince.append(dateDiffInMillis/DAY).append(" Day "); 
       dateDiffInMillis %= DAY; 
      } 
     } 
     if (dateDiffInMillis > HOUR) { 
      if(dateDiffInMillis/HOUR>1){ 
       sTimeSince.append(dateDiffInMillis/HOUR).append(" Hrs "); 
       dateDiffInMillis %= HOUR; 
      }else{ 
       sTimeSince.append(dateDiffInMillis/HOUR).append(" Hr "); 
       dateDiffInMillis %= HOUR; 
      } 
     } 
     if (dateDiffInMillis > MINUTE) { 
      if (dateDiffInMillis/MINUTE > 1) { 
       sTimeSince.append(dateDiffInMillis/MINUTE).append(" Mins "); 
       dateDiffInMillis %= MINUTE; 
      } else { 
       sTimeSince.append(dateDiffInMillis/MINUTE).append(" Min "); 
       dateDiffInMillis %= MINUTE; 
      } 
     } 
     if (dateDiffInMillis > SECOND) { 
      sTimeSince.append(dateDiffInMillis/SECOND).append(" Sec "); 
      dateDiffInMillis %= SECOND; 
     } 
     sTimeSince.append(dateDiffInMillis + " ms"); 
     sTimeSince.toString(); 
     return sTimeSince.toString(); 
    } 
+0

謝謝,但這有點複雜,我不需要它如此詳細.. :) – Matej

0

這是一個非常簡單的方法,做你所需要的精確比較,我做了一些測試,它似乎工作:

package test; 

import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.Locale; 

public class TestDateDiff { 

    public static void main(String[] args) throws Exception { 
     if (isWhitinOneHOur("16. October 2015 16:30")) { 
      System.out.println("Date OK"); 
     } else { 
      System.out.println("Date KO"); 
     } 

    } 

    public static boolean isWhitinOneHOur(String dateAsStr) throws Exception { 
     SimpleDateFormat sdf= new SimpleDateFormat("dd. MMMM yyyy HH:mm", Locale.US); 
     Date date=sdf.parse(dateAsStr); 
     Date now=new Date(); 
     long oneHour=1000*60*60; 
     if ((now.getTime()+oneHour)>=date.getTime() && (now.getTime()-oneHour)<=date.getTime()) { 
      return true; 
     } else { 
      return false; 
     }   
    } 

} 
1
DateFormat format = new SimpleDateFormat("dd. MM yyyy HH:mm"); 
String dateString= "16. 10 2015 11:05"; 

Date date = format.parse(dateString); 
    private static boolean DateInScope(Date date) { 
     Date currentTime = new Date(); 
     long hours = TimeUnit.MILLISECONDS.toHours(currentTime.getTime()); 
     long hours2 = TimeUnit.MILLISECONDS.toHours(date.getTime()); 
     return hours - 1 == hours2 || hours + 1 == hours2; 
} 
相關問題