0

I'm trying figurate a way to print all dates between for example

2018/20/01 15:20:31:001
2018/20/01 19:34:03:001

The output I want to see is:

2018/20/01 15:20:31:001
2018/20/01 15:20:31:002
2018/20/01 15:20:31:003
2018/20/01 15:20:31:004
2018/20/01 15:20:31:005
2018/20/01 15:20:31:006
...
2018/20/01 19:34:02:999
2018/20/01 19:34:03:001

Can anyone help me in that? Because all I could find was solutions without hours, minutes etc. and I really need that.

Mavematt
  • 51
  • 1
  • 4

2 Answers2

1

You need a DateTimeFormatter to format the date like this.

Then you need two LocalDateTimes: One for the start date, one for the end date.

Now you can create a loop that prints out the start date and then changes it (It's immutable, but you can recycle the variable.) via the date's method .plusSeconds() to get to the next second (Or do you indeed want millisecond resolution?), print out, rinse and repeat, until the two dates are equal.

Here's an example for formatting a date with milliseconds.

Dreamspace President
  • 1,060
  • 13
  • 33
0

Here is an example to do this with java.time:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/dd/MM HH:mm:ss:SSS");
LocalDateTime dt1 = LocalDateTime.parse("2018/20/01 15:20:31:001", formatter);
LocalDateTime dt2 = LocalDateTime.parse("2018/20/01 19:34:03:001", formatter);
System.out.println(Duration.between(dt1, dt2));

It prints:

PT4H13M32S

4 hours 13 minutes 32 seconds.

To get an output like the one you showed, you can do this:

long totalSeconds = Duration.between(dt1, dt2).getSeconds();
for (long i = 0 ; i < totalSeconds * 1000 ; i++) {
    System.out.println(dt1.plus(i, ChronoUnit.MILLIS).format(formatter));
}
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Thanks for your answer, can you explain how to use getSeconds in order to print out each time? – Mavematt May 06 '18 at 11:38
  • @Mavematt Edited. – Sweeper May 06 '18 at 11:50
  • Ohhh :(( But the problem is, I need output in that format 2018/20/01 15:20:31:001, 2018/20/01 15:20:31:002, 2018/20/01 15:20:31:003, 2018/20/01 15:20:31:004, 2018/20/01 15:20:31:005, 2018/20/01 15:20:31:006, ... 2018/20/01 19:34:02:999, 2018/20/01 19:34:03:001 – Mavematt May 06 '18 at 11:51
  • @Mavematt Edited again. – Sweeper May 06 '18 at 13:01