2015-08-15 161 views
0

我是初學者,我想將毫秒添加到java時間戳(以毫秒爲單位),並以毫秒爲單位獲得結果時間以供進一步操作。我試圖通過每毫秒增加時間戳並檢查它是否着陸到假期或週末,然後跳過那一天來做到這一點。這樣做不是很明智。永遠。有人已經發明瞭輪子嗎?Java將毫秒添加到毫秒時間戳省略週末和節假日

這裏是我可憐的,非常糟糕的代碼(不要使用此,需要年齡

public static class businessdays { 

     public static long addWorkingTime(long timestamp, long milliseconds){ 

      for (int i=0;i<milliseconds;i ++){ 
       long test_timestamp = timestamp + 1; 
       while (isHoliday(test_timestamp)){ 
        System.out.println("isHoliady"); 
        timestamp += 24 * 60 * 60 * 1000; //jump weekend or holiday 
        test_timestamp += (12 * 60 * 60 * 1000); 
       }    
       timestamp += i; 
      } 

      return timestamp;   
     } 

     private static boolean isHoliday(long timestamp){ 
      List<String> holidays = new ArrayList<String>(Arrays.asList("01/01,05/01,06/01,10/20,12/12,12/25,12/26".split(","))); 
      SimpleDateFormat dw = new SimpleDateFormat("u"); //date of the week - 1 - 7 
      SimpleDateFormat dd = new SimpleDateFormat("dd"); //1 - 30/31 
      SimpleDateFormat dm = new SimpleDateFormat("MM"); //1 - 12 
      Date test_timeDate = new Date(timestamp); 
      String date = dm.format(test_timeDate)+"/"+dd.format(test_timeDate); //MM/dd 
      String doweek = dw.format(test_timeDate); 
      if (holidays.contains(date) || doweek.contains("6") || doweek.contains("7")){ 
       return true; 
      } 
      return false; 
     } 
    } 
+3

你想達到什麼目的。增加毫秒是實現某些功能的一種方式。不是最終目標。你最終的目標是什麼? –

+0

像JB Nizet說的 - 你想達到什麼目的?時間戳精確到毫秒(它永遠不會)是否重要? 你不能檢查每秒說,然後決定使用或不使用時間戳嗎?例如,如果你在Windows上(這不是實時操作系統),應用程序的毫秒可能與「真實生活」毫秒差別很大,即不要期望從毫秒到絕對精度 - 如果你這樣做,你會失望! – Frank

+0

我的最終目標是獲得一個函數,當給定當前時間戳並添加28小時或更長時間時,我應該按照工作日計算截止日期 – Martin

回答

1

Calendar類將讓你在天,這可能是最好操作。毫秒往往是日期處理的天真選擇,因爲不是所有的日子都有相同的毫秒數。每隔一段時間插入一次閏秒,並且夏令時的每一個結束日的時間比通常的24小時或更多。

final static int DAY_MILLIS = 24 * 60 * 60 * 1000; 
static String hols[] = new String[] { "01/01", "05/01", "06/01", "10/20", "12/12", "12/25", "12/26" }; 

public static long addWorkingTime(long timestamp, long milliseconds) 
{ 
    // Get a calendar from a timestamp. 
    Calendar cal = new GregorianCalendar(); 
    cal.setTimeInMillis(timestamp); 
    while (isHoliday(cal)) cal.add(Calendar.DATE, 1); 

    // Count off the days in milliseconds. 
    int days = (int)(milliseconds/DAY_MILLIS); 
    for (int i = 0; i < days; i++) { 
     cal.add(Calendar.DATE, 1); 
     while (isHoliday(cal)) cal.add(Calendar.DATE, 1); 
    } 

    // Apply the leftover from milliseconds if there is any. 
    milliseconds = milliseconds - days * DAY_MILLIS; 
    cal.add(Calendar.MILLISECOND, milliseconds); 
    while (isHoliday(cal)) cal.add(Calendar.DATE, 1); 

    // Return a timestamp from a calendar. 
    return cal.getTimeInMillis(); 
} 

static boolean isHoliday(Calendar cal) 
{ 
    int dow = cal.get(Calendar.DAY_OF_WEEK); 
    if (dow == Calendar.SATURDAY || dow == Calendar.SUNDAY) return true; 
    String mmdd = new SimpleDateFormat("MM/dd").format(cal.getTime()); 
    return Arrays.binarySearch(hols, mmdd) >= 0; 
} 
+0

您是John Bickers的救星 – Martin