Search code examples
scalaloopscalendarseq

scala loop to add date string into a seq


I am trying to add dates in string from an array into a seq while determining whether it is a weekend day.

import java.text.SimpleDateFormat
import java.util.{Calendar, Date, GregorianCalendar}
import org.apache.spark.sql.SparkSession

val arrDateEsti=dtfBaseNonLong.select("AAA").distinct().collect.map(_(0).toString);
    
var dtfDateCate = Seq(
      ("0000", "0")
    );
    
for (a<-0 to arrDateEsti.length-1){
      val dayDate:Date = dateFormat.parse(arrDateEsti(a));
      val cal=new GregorianCalendar
      cal.setTime(dayDate);
    
  if (cal.get(Calendar.DAY_OF_WEEK)==1 || cal.get(Calendar.DAY_OF_WEEK)==7){
    dtfDateCate:+(arrDateEsti(a),"1")
  }else{
    dtfDateCate:+(arrDateEsti(a),"0")
  }
};

scala> dtfDateCate
res20: Seq[(String, String)] = List((0000,0))

It returns the same initial sequence. But if I run one single element it works. What went wrong?

scala>   val dayDate:Date = dateFormat.parse(arrDateEsti(0));
dayDate: java.util.Date = Thu Oct 15 00:00:00 CST 2020

scala>   cal.setTime(dayDate);

scala>   if (cal.get(Calendar.DAY_OF_WEEK)==1 || cal.get(Calendar.DAY_OF_WEEK)==7){
     |     dtfDateCate:+(arrDateEsti(0),"1")
     |   }else{
     |     dtfDateCate:+(arrDateEsti(0),"0")
     |   };
res14: Seq[(String, String)] = List((0000,0), (20201015,0))

Solution

  • I think this gets at what you're trying to do.

    import java.time.LocalDate
    import java.time.DayOfWeek.{SATURDAY, SUNDAY}
    import java.time.format.DateTimeFormatter
    
                      //replace with dtfBaseNonLong.select(... code
    val arrDateEsti = Seq("20201015", "20201017")  //place holder
    
    val dtFormat = DateTimeFormatter.ofPattern("yyyyMMdd")
    val dtfDateCate = ("0000", "0") +:
                      arrDateEsti.map { dt =>
                        val day = LocalDate.parse(dt,dtFormat).getDayOfWeek()
                        if (day == SATURDAY || day == SUNDAY) (dt, "1")
                        else                                  (dt, "0")
                      }
    //dtfDateCate = Seq((0000,0), (20201015,0), (20201017,1))