检查日期是否在范围内

时间:2018-04-30 05:14:00

标签: java date javafx fxml

我的Java FX应用处理工作时间。我在2个日期字段中有工作开始和结束时间。我成功地计算了2个dateTime之间的差异;但是现在我怎么能检查结果是在夜晚还是在白天范围内?那天从6点开始到22点结束。例如,工作在3Am到11Pm之间的人。 以下是我如何处理总工作小时数。

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天或更长时间。

5 个答案:

答案 0 :(得分:4)

您应该使用新的if [[ "$line" =~ "$re_containing_word" ]]; then ...类为您提供一个DateTimeFormatter对象,您可以从中提取时间。

LocalDateTime

答案 1 :(得分:2)

您可以查看LocalTimeLocalDateTime部分,使用isAfterisBefore进行简单检查。

我会在这个例子中使用这些值。

LocalDateTime start = LocalDateTime.of(2018, Month.APRIL, 30, 23, 0);
LocalDateTime end = LocalDateTime.of(2018, Month.MAY, 1, 5, 0);

然后定义夜晚的限制。

LocalTime startNight = LocalTime.of(22, 0);
LocalTime endNight = LocalTime.of(6, 0);

只需使用两个日期的LocalTime并检查它们是否在范围内。您可以使用toLocalTime获取值。

if(start.toLocalTime().isAfter(startNight) &&
        end.toLocalTime().isBefore(endNight)){
    System.out.println("NIGHT TIME");
} else {
    System.out.println("DAY TIME");
}
  

夜晚

输出有效,因为我们从2​​3:00开始到05:00结束。

如果您需要为5:45定义类似LocalTime.of(5,45)的时间,则使用此方法可以提供更简单的解决方案

这是一个例子,如果允许它开始第22部分但在6之后继续工作,这可能需要一些调整。这只是一个如何使用这些方法的例子。

答案 2 :(得分:2)

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

如果您想确保考虑夏令时 ,请使用LocalDateTime代替ZonedDateTime。在这种情况下,在一次计算整个工作日而不是一次计算整个工作日时,可能也会有性能提升。

以下代码使用28/03/2018 03:00和29/03/2018 23:30作为示例开始和结束时间。预计总工作时数为44.5,因为一天是24小时,从03:00到23:30有20.5小时。由于两天中每天有16个白天,所以预期的白天时间为32小时。这留下了12.5小时的夜晚时间。确实代码打印

  

第32.0小时;晚上12.5小时

该计划如下。请填写我放置America / Monterey的正确时区。

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);
}

答案 3 :(得分:1)

如果您使用<?php header('Access-Control-Allow-Origin: *'); $target_path = "uploads/"; $target_path = $target_path . basename( $_FILES['file']['name']); if (move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) { echo "Upload and move success"; } else { echo $target_path; echo "There was an error uploading the file, please try again!"; } ?> API,这会更容易。您只需检查一下,如果日期不同或者开始时间不在java.time6:00的范围内:

22:00
private static final LocalTime START_TIME = LocalTime.of(6, 0); // 06:00
private static final LocalTime END_TIME = LocalTime.of(22, 0); // 22:00
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");

答案 4 :(得分:0)

定义两个格式化程序。一个Fromatter用于从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;