2016-04-07 4 views
5

यहाँ मेरी स्केला मामले वर्ग DATERANGE है:ठहराव समय को छोड़कर तिथि सीमाओं जाओ

case class DateRange(startDT: DateTime, endDT: DateTime) 

नमूना इनपुट डेटा (Joda):

val dt1 = DateTime.parse("2016-01-04T03:00:00.000Z") // dt1 will always the range start date time 
val dt2 = DateTime.parse("2016-01-05T04:00:00.000Z") // dt2 will always the range end date time 

val dr = DateRange(dt1, dt2) // container to hold the date ranges 

val st = LocalTime.parse("13:00:00") // st will always the stoppage start time 
val et = LocalTime.parse("02:00:00") // et will always the stoppage end time 

मैं List[DateRange] के रूप में परिणाम प्राप्त करने की कोशिश कर रहा हूँ स्टॉपपेज समय अंतराल को छोड़कर। तिथि सीमा और समय शायद कुछ भी है।

ऊपर इनपुट डेटा के लिए

वांछित उत्पादन:

List(DateRange(2016-01-04T03:00:00.000Z,2016-01-04T13:00:00.000Z),DateRange(2016-01-05T02:00:00.000Z,2016-01-05T04:00:00.000Z)) 

मैं इस तरह की कोशिश की:

val result = if (st.isBefore(et)) { 
    if (dr.startDT.isBefore(dr.endDT) && st.isAfter(dr.startDT.toLocalTime)) { 
     DateRange(dr.startDT.withTime(st), dr.startDT.withTime(et)) 
    } else if (dr.startDT.isBefore(dr.endDT) && st.isBefore(dr.startDT.toLocalTime)) { 
     DateRange(dr.endDT.withTime(st), dr.endDT.withTime(et)) 
    } else { 
     DateRange(dr.startDT.withTime(st), dr.startDT.withTime(et)) 
    } 
    } 
    else { 
    if (dr.startDT.isBefore(dr.endDT) && et.isBefore(dr.endDT.toLocalTime) && st.isBefore(dr.endDT.toLocalTime)) { 
     DateRange(dr.startDT.withTime(st), dr.endDT.withTime(23, 59, 59, 999)) 
    } else if (dr.startDT.isBefore(dr.endDT) && et.isBefore(dr.endDT.toLocalTime) && st.isAfter(dr.endDT.toLocalTime)) { 
     DateRange(dr.startDT, dr.endDT.withTime(et)) 
    } else if (dr.startDT.isBefore(dr.endDT) && et.isBefore(dr.endDT.toLocalTime) && st.isAfter(dr.endDT.toLocalTime)) { 
     DateRange(dr.startDT, dr.endDT.withTime(et)) 
    } else { 
     DateRange(dr.startDT.withTime(st), dr.endDT.withTime(et)) 
    } 
+0

"सेंट हमेशा ठहराव समय शुरू कर देंगे" अगर और "एट हमेशा स्टॉपपेज एंड टाइम" होगा। st.isBefore (et) झूठी कैसे हो सकती है? –

+0

tldr इसे –

+0

@AlexeyRomanov को सॉर्ट करने का एक तरीका ढूंढता है ... रोकथाम शुरू करने का समय दिन के अंत तक शुरू हो सकता है और अगले दिन सुबह बंद होने का अंत हो सकता है। – Jet

उत्तर

3

यह प्रयास करें,

object Splitter extends App { 

    val shiftStartDate = DateTime.parse("2016-01-04T06:00:00.000") 
    val shiftEndDate = DateTime.parse("2016-01-04T16:00:00.000") 

    val st = LocalTime.parse("23:00:00") 
    val et = LocalTime.parse("08:00:00") 


    val stoppageStartDate = shiftStartDate.toLocalDate.toDateTime(st) 
    val StoppageEndDate = if(st.isBefore(et)){ 
    shiftStartDate.toLocalDate.toDateTime(et) 
    }else{ 
    shiftStartDate.toLocalDate.toDateTime(et).plusDays(1) 
    } 
    val result = if ((stoppageStartDate.isAfter(shiftStartDate) || stoppageStartDate.isEqual(shiftStartDate)) && 
    (StoppageEndDate.isBefore(shiftEndDate) || StoppageEndDate.isEqual(shiftEndDate))) { 
    List(DateRange(shiftStartDate, stoppageStartDate), DateRange(StoppageEndDate, shiftEndDate)) 
    } else if ((stoppageStartDate.isAfter(shiftStartDate) || stoppageStartDate.isEqual(shiftStartDate)) && 
    StoppageEndDate.isAfter(shiftEndDate) && stoppageStartDate.isBefore(shiftEndDate)) { 
    List(DateRange(shiftStartDate, stoppageStartDate)) 
    } 
    else if (stoppageStartDate.isBefore(shiftStartDate) && (StoppageEndDate.isBefore(shiftEndDate) || 
    StoppageEndDate.isEqual(shiftEndDate)) && StoppageEndDate.isAfter(shiftStartDate)) { 
    List(DateRange(StoppageEndDate, shiftEndDate)) 
    } else if (stoppageStartDate.isBefore(shiftStartDate) && StoppageEndDate.isAfter(shiftEndDate)) { 
    Nil 
    } else { 
    List(DateRange(shiftStartDate, shiftEndDate)) 
    } 
    println(">>>> " + result.result.filterNot(x=>x.startTS==x.endTS)) 
} 
संबंधित मुद्दे