2015-08-15 88 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