0

This problem is bothering me for a while. A couple months ago I started one project and just can't go one because of this stupid problem.

enter image description here

If you see a time line between 6 and 21 there is a daytime. My problem is that i need to calculate time between 2 dates or hours separately. For example. Let say that start time is at 5:00pm and end time is at 22pm. how can i calculate how many hours was at day and nigh separately between those 2 times ?

String enter = "2017-08-16 15:00:00";
String leave = "2017-08-17 12:00:00";
org.joda.time.format.DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime start = formatter.parseDateTime(enter);
DateTime end = formatter.parseDateTime(leave);
Period period = new Period(start,end);
int hours = period.toStandardHours().getHours();
System.out.println(hours);
Robin Topper
  • 2,295
  • 1
  • 17
  • 25
simenesky
  • 3
  • 1

3 Answers3

1

This solution uses the java.time API instead of Joda time, but you should be able to figure out how to rewrite it yourself if you think that's necessary.

String enter = "2017-08-14 15:00:00";
String leave = "2017-08-17 12:00:00";

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime start = LocalDateTime.parse(enter, formatter);
LocalDateTime end = LocalDateTime.parse(leave, formatter);
Duration duration = Duration.between(start, end);

int duringDay = 0;
int duringNight = 0;

// take care of all full days
long days;
if ((days = duration.toHours() / 24L) > 0) {
    duringDay += 15 * days;
    duringNight += 9 * days;
}

// take care of the remainder
for (int i = 1; i <= duration.toHours() % 24; i++) {
    LocalDateTime ldt = start.plusHours(i);
    if (ldt.getHour() <= 6 || ldt.getHour() > 21) {
        duringNight++;
    } else {
        duringDay++;
    }
}

System.out.println("Hours during day: " + duringDay);
System.out.println("Hours during night: " + duringNight);

With nanosecond precision it becomes a bit more complicated:

String enter = "2017-08-13 15:30:30";
String leave = "2017-08-17 22:00:00";

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime start = LocalDateTime.parse(enter, formatter);
LocalDateTime end = LocalDateTime.parse(leave, formatter);

Duration duration = Duration.between(LocalDateTime.of(start.toLocalDate().plusDays(1), LocalTime.of(0, 0)), LocalDateTime.of(end.toLocalDate(), LocalTime.of(0, 0)));

Duration timeDuringDay = Duration.ofDays(0);
Duration timeDuringNight = Duration.ofDays(0);

// take care of all full days
long days;
if ((days = duration.toHours() / 24L) > 0) {
    timeDuringDay = timeDuringDay.plusHours(15 * days);
    timeDuringNight = timeDuringNight.plusHours(9 * days);
}

// take care of the first day
if (start.isBefore(LocalDateTime.of(start.toLocalDate(), LocalTime.of(6, 0)))) {
    timeDuringNight = timeDuringNight.plus(Duration.between(start, LocalDateTime.of(start.toLocalDate(), LocalTime.of(6, 0))));
    timeDuringNight = timeDuringNight.plus(Duration.ofHours(3));
    timeDuringDay = timeDuringDay.plusHours(15);
} else if (start.isAfter(LocalDateTime.of(start.toLocalDate(), LocalTime.of(21, 0)))) {
    timeDuringNight = timeDuringNight.plus(Duration.between(start, LocalDateTime.of(start.toLocalDate().plusDays(1), LocalTime.of(0, 0))));
} else {
    timeDuringDay = timeDuringDay.plus(Duration.between(start, LocalDateTime.of(start.toLocalDate(), LocalTime.of(21, 0))));
    timeDuringNight = timeDuringNight.plusHours(3);
}   

// take care of the last day
if (end.isBefore(LocalDateTime.of(end.toLocalDate(), LocalTime.of(6, 0)))) {
    timeDuringNight = timeDuringNight.plus(Duration.between(LocalDateTime.of(end.toLocalDate(), LocalTime.of(0, 0)), end));
} else if (end.isAfter(LocalDateTime.of(end.toLocalDate(), LocalTime.of(21, 0)))) {
    timeDuringNight = timeDuringNight.plusHours(6);
    timeDuringNight = timeDuringNight.plus(Duration.between(LocalDateTime.of(end.toLocalDate(), LocalTime.of(21, 0)), end));
    timeDuringDay = timeDuringDay.plusHours(15);
} else {
    timeDuringNight = timeDuringNight.plusHours(6);
    timeDuringDay = timeDuringDay.plus(Duration.between(LocalDateTime.of(end.toLocalDate(), LocalTime.of(6, 0)), end));
}

System.out.println("Time during day: " + timeDuringDay);
System.out.println("Time during night: " + timeDuringNight);
Robin Topper
  • 2,295
  • 1
  • 17
  • 25
0

This is the simplest code what I wanted for this project.

 String enter = "2017-08-16 05:00:00";
    String out = "2017-08-17 15:00:00";
    DateTimeFormatter time_format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); //need to format date fore recognition
    LocalDateTime start = LocalDateTime.parse(enter,time_format);
    LocalDateTime finish = LocalDateTime.parse(out,time_format);
    Duration duration = Duration.between(start,finish); // duration between 2 dates

    int duringDay = 0; // storing hours during day
    int duringNight = 0; // storing hours during night

    for (int i=1;i<=duration.toHours();i++){ // from 1 to number of hours between 2 dates

    LocalDateTime ldt = start.plusHours(i); // for every hour in a range give +1 form started time 5am in this example

    if(ldt.getHour()<=6 || ldt.getHour()>21){ // while it's adding check if hour is between 6am and 21

        duringNight++; // if it's not add to duringNight integer

    } else {duringDay++;} // else give it to duringDay integer

    }

    System.out.println("Day:" + duringDay + "\n" + "Night:" + duringNight);
simenesky
  • 3
  • 1
-1

You may try this:

LocalDateTime enterDate = LocalDateTime.of(2017, Month.AUGUST, 16, 15, 0); //2017-08-16 15:00:00
LocalDateTime leaveDate = LocalDateTime.of(2017, Month.AUGUST, 17, 12, 0); //2017-08-17 12:00:00

long differnce = enterDate.until(leaveDate, ChronoUnit.HOURS); // 21

As you may play around with Chronounits depending what units suit you.

Also take a look at this and this

vnj
  • 74
  • 3