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;
}
}
你想达到什么目的。增加毫秒是实现某些功能的一种方式。不是最终目标。你最终的目标是什么? –
像JB Nizet说的 - 你想达到什么目的?时间戳精确到毫秒(它永远不会)是否重要? 你不能检查每秒说,然后决定使用或不使用时间戳吗?例如,如果你在Windows上(这不是实时操作系统),应用程序的毫秒可能与“真实生活”毫秒差别很大,即不要期望从毫秒到绝对精度 - 如果你这样做,你会失望! – Frank
我的最终目标是获得一个函数,当给定当前时间戳并添加28小时或更长时间时,我应该按照工作日计算截止日期 – Martin