2017-08-17 12 views
0

この問題はしばらく私を悩ましています。 数ヶ月前、私は1つのプロジェクトを開始しましたが、この愚かな問題のため1つしか行けません。1つの期間から2つの期間タイプを計算しますか?

enter image description here

あなたは6と21の間にタイムラインが表示される場合は昼間があります。私の問題は、2つの日付または時間の間の時間を別々に計算する必要があることです。例えば。開始時間は午後5時で、終了時間は午後22時であるとします。これらの2回の間に日中に何時間、何時に別々に何時間を計算することができますか?

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

答えて

1

を見てみましょう。このソリューションではなくジョダ時間のjava.time APIを使用していますが、あなたはそれが必要だと思うならば、それを自分で書き換えする方法を見つけ出すことができるはずです。ナノ秒の精度で

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

それは少し複雑になる:

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); 
+1

これは私が探していたものです。 TNX a lootoot !!!ロジックはとてもシンプルです!!! TNXもう一度たくさん!!!!! – simenesky

+0

OPは時間精度のみで満足されるようです。そうであれば、あなたの答えは十分です。 –

-1

あなたはこの試みることがあります。あなたは単位はあなたに合うものを依存Chronounitsをいじっているよう

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 

を。

thisthis

+0

昼間と夜間の時間はどのように区別されますか? –

+0

それはそうではありません、私はQを取得していないようです。 – vanjiii

0

は、これは私がこのプロジェクトのために何を望むかの最も簡単なコードです。

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); 
関連する問題