0

I have fromDate and toDate two milliseconds and I want to get all epoch milliseconds between these two date in weekly intervals.

`

val date1 = new Date(1668940033492L)
val calendar = Calendar.getInstance()
calendar.setTime(date1)
calendar.add(Calendar.WEEK_OF_YEAR, 1)

`

From above code I can add week to fromDate but how will I get it till to Date and all the intervals in between.

  • 2
    `Date` comes from `java.sql` I believe? Do not use it. Use `java.time` types, e.g. `Instant`. It will make it easy to compute a diff, change of time zones etc. – Mateusz Kubuszok Dec 20 '22 at 13:44
  • 1
    date is presuambly `java.util.Date`, @MateuszKuboszok - but other than that, yeah. Your advice is correct. – rzwitserloot Dec 20 '22 at 13:45
  • I have used Date from import java.util._ Also I have asked a different question – Alim Uddin Dec 20 '22 at 13:46
  • 1
    Do you want to know how many weeks btw 2 milliseconds? If yes, https://stackoverflow.com/questions/9963147/get-the-number-of-weeks-between-two-dates perfectly cover your question – Rustam Dec 20 '22 at 14:09
  • 1
    Welcome to Stack Overflow. It’s not clear, sorry. You need to explain for people who know nothing about your situation and context. Are you wanting to know all the times when a new week starts between your two points in time? And I know it’s not what you asked, but I still think the most helpful recommendation we can give you is: do not use `Calendar` and `Date`. They are poorly designed, cumbersome to work with, nothing you want, and fortunately also long outdated. Use [java.time, the modern java date and time API](https://docs.oracle.com/javase/tutorial/datetime/index.html). – Ole V.V. Dec 21 '22 at 14:44
  • Also when does your week start? It depends on both locale and time zone. it may start on Sunday or Monday or some other day of the week according to your locale. And the start of the day ("midnight") falls at different times, different milliseconds, depending on time zone. – Ole V.V. Dec 21 '22 at 15:12
  • I hope your question was answered through the linked original questions or through the comments. If not,please ask a new question, and please do your best to make it clearer than this one. Give precise and specific expected outcome from your example. I am voting to delete this question because as unclear as it is, I cannot believe that it can be useful to future readers. – Ole V.V. Dec 23 '22 at 08:09

1 Answers1

0

Not sure what is the problem. You seem to have all you need. Use calendar.getTime to get intermediate dates. Use date.before(date2) to check if it is inside the interval.

Or do you just not know how to write a "loop" in scala? That I would say is a rather open-ended question that should probably not be answered here as is, but since I started typing, here is one possibility:

val weekly: Iterator[Date] = Iterator.iterate(date1) { d => 
  Calendar.getInstance.setTime(_).add(Calendar.WEEK_OF_YEAR, 1).getTime)
}.takeWhile(_.before(date2)
Dima
  • 39,570
  • 6
  • 44
  • 70