有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java检查日期是否在范围内

我的Java FX应用程序可以处理工作时间。我在两个日期字段中有工作开始和结束时间。我成功地计算了两个日期之间的差异;但现在,我如何检查结果是否在夜间或白天范围内????一天从6点开始,到22点结束。例如,在凌晨3点到晚上11点之间工作的人。 下面是我如何计算总工作时间的

public void CalculNbreJourTravaille() {
    SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyy HH:mm");

    try {
        Date ddtt = format.parse(ddt.getText());
        Date dftt = format.parse(dft.getText());
        long diff = dftt.getTime() - ddtt.getTime();
        long diffhours = diff / (60*60*1000)%24;
        long diffdays = diff/(24*60*60*1000);
        long total = diffhours + (diffdays*24);
        result.setText(total + " Hours");   
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

我们有可以工作到晚上10点以上的工人,工资也不一样。如果他们在晚上10点以后工作,他们将获得特殊报酬。我们在工作结束时付款。他们只能工作10天或更长时间


共 (2) 个答案

  1. # 1 楼答案

    以下是我对你所有要求的尝试。在阅读之前,我编写了代码,您不需要考虑夏季时间(DST),因此我使用ZonedDateTime在夏季时间转换期间获得正确的小时数。出于同样的原因,我每天都需要重复。对于每个日期,我计算夜间工作时间和白天工作时间

    如果你想确保夏天的时间没有被考虑进去,用LocalDateTime代替ZonedDateTime。在这种情况下,一次计算整个工作日,而不是一次计算一天,也可能会有绩效提升

    以下代码使用2018年3月28日03:00和2018年3月29日23:30作为示例开始和结束时间。预计总工作时间为44.5小时,因为一天是24小时,从03:00到23:30有20.5小时。由于这两天每天有16个白天小时,因此预计的白天时间为32小时。剩下12.5小时作为夜间时间。事实上,代码会打印出来

    Day 32.0 hours; night 12.5 hours

    节目如下。请填写我在美国/蒙特利的正确时区

    static ZoneId zone = ZoneId.of("America/Monterrey");
    static LocalTime dayStart = LocalTime.of(6, 0);
    static LocalTime dayEnd = LocalTime.of(22, 0);
    static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/M/uuuu H:mm");
    
    public static void main(String[] args) {
    
        String workStartString = "28/03/2018 03:00";
        String workEndString = "29/03/2018 23:30";
        calculateWorkingHours(workStartString, workEndString);
    }
    
    public static void calculateWorkingHours(String workStartString, String workEndString) {
        ZonedDateTime workStart
                = LocalDateTime.parse(workStartString, formatter).atZone(zone);
        ZonedDateTime workEnd
                = LocalDateTime.parse(workEndString, formatter).atZone(zone);
    
        if (workEnd.isBefore(workStart)) {
            throw new IllegalArgumentException("Work end must not be before work start");
        }
    
        LocalDate workStartDate = workStart.toLocalDate();
        LocalDate workEndDate = workEnd.toLocalDate();
    
        Duration workedDaytime = Duration.ZERO;
        // first calculate work at nighttime before the start date, that is, work before 06:00
        Duration workedNighttime 
                = calculateNightTime(workStartDate.minusDays(1), workStart, workEnd);
    
        for (LocalDate d = workStartDate; ! d.isAfter(workEndDate); d = d.plusDays(1)) {
            workedDaytime = workedDaytime.plus(calculateDayTime(d, workStart, workEnd));
            workedNighttime = workedNighttime.plus(calculateNightTime(d, workStart, workEnd));
        }
    
        double dayHours = workedDaytime.toMinutes() / (double) TimeUnit.HOURS.toMinutes(1);
        double nightHours = workedNighttime.toMinutes() / (double) TimeUnit.HOURS.toMinutes(1);
    
        System.out.println("Day " + dayHours + " hours; night " + nightHours + " hours");
    
    }
    
    /**
     * Calculates amount of work in daytime on d,
     * that is between 06:00 and 22:00 on d.
     * Only time that falls with in workStart to workAnd
     * and also falls within 06:00 to 22:00 on d is included.
     * 
     * @param d The date for which to calculate day work
     * @param workStart
     * @param workEnd
     * @return Amount of daytime work on the said day
     */
    private static Duration calculateDayTime(LocalDate d, ZonedDateTime workStart, ZonedDateTime workEnd) {
        ZonedDateTime dayStartToday = d.atTime(dayStart).atZone(zone);
        ZonedDateTime dayEndToday = d.atTime(dayEnd).atZone(zone);
        if (workStart.isAfter(dayEndToday) || workEnd.isBefore(dayStartToday)) {
            return Duration.ZERO;
        }
    
        // restrict calculation to daytime on d
        if (workStart.isBefore(dayStartToday)) {
            workStart = dayStartToday;
        }
        if (workEnd.isAfter(dayEndToday)) {
            workEnd = dayEndToday;
        }
    
        return Duration.between(workStart, workEnd);
    }
    
    /**
     * Calculates amount of night work in the night after d,
     * that is from 22:00 on d until 06:00 the next morning.
     * 
     * @param d The date for which to calculate night work
     * @param workStart
     * @param workEnd
     * @return Amount of nighttime work in said night
     */
    private static Duration calculateNightTime(LocalDate d, ZonedDateTime workStart, ZonedDateTime workEnd) {
        assert ! workEnd.isBefore(workStart);
    
        ZonedDateTime nightStart = d.atTime(dayEnd).atZone(zone);
        ZonedDateTime nightEnd = d.plusDays(1).atTime(dayStart).atZone(zone);
    
        if (workEnd.isBefore(nightStart) || workStart.isAfter(nightEnd)) {
            return Duration.ZERO;
        }
    
        // restrict calculation to the night after d
        if (workStart.isBefore(nightStart)) {
            workStart = nightStart;
        }
        if (workEnd.isAfter(nightEnd)) {
            workEnd = nightEnd;
        }
    
        return Duration.between(workStart, workEnd);
    }
    
  2. # 2 楼答案

    定义两个格式化程序。一个来自atter,从edittext获取日期和时间。而其他人则在当天上午12点起床。现在我们需要对应于同一天早上6点和晚上11点的日期对象。我们可以通过在12AM对象上增加那么多毫秒来获得这些。这些添加的日期可用于比较

    SimpleDateFormat df_zero_hours = new SimpleDateFormat("dd/MM/yyy");
    SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm");
    
    Date ddtt = format.parse(ddt.getText()); //Work Start Time
    Date dftt = format.parse(dft.getText()); //Work End Time
    
    
    Date dateStart = df_zero_hours.parse(ddt.getText()); //12AM of the day job started
    Date dayStart = new Date();
        dayStart.setTime(dateStart.getTime()+6*60*60*1000); // Get 6AM of that day
    Date dayEnd = new Date();
        dayEnd.setTime(dateStart.getTime()+22*60*60*1000); //Get 10PM of that day
    
    //        Now check the worked hours. in Whatever way you need
    boolean isBefore6AM = (dayStart.getTime()-ddtt.getTime())>0;
    boolean isAfter10PM = (dftt.getTime()-dayEnd.getTime())>0;