fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.time.LocalDateTime;
  7. import java.time.format.DateTimeFormatter;
  8. import java.time.temporal.WeekFields;
  9.  
  10. import static java.time.temporal.ChronoUnit.WEEKS;
  11.  
  12.  
  13. /* Name of the class has to be "Main" only if the class is public. */
  14. class Ideone
  15. {
  16. public static void main(String[] args) {
  17. DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss','SSS");
  18. LocalDateTime[] startDate = {
  19. LocalDateTime.parse("2018-08-24T12:18:06,166", format),
  20. LocalDateTime.parse("2018-08-24T12:18:06,166", format),
  21. LocalDateTime.parse("2018-08-24T12:18:06,166", format),
  22. };
  23. LocalDateTime[] endDate = {
  24. LocalDateTime.parse("2018-08-24T12:18:06,166", format),
  25. LocalDateTime.parse("2018-08-30T12:18:06,166", format),
  26. LocalDateTime.parse("2019-08-24T11:18:06,166", format),
  27. };
  28.  
  29. for (int i = 0; i < startDate.length; i++) {
  30. numberOfWeeks(startDate[i], endDate[i]);
  31. }
  32.  
  33. }
  34.  
  35. public static void numberOfWeeks(LocalDateTime startDate, LocalDateTime endDate) {
  36. int addWeek = 0;
  37. if (startDate.get(WeekFields.ISO.weekOfYear()) < endDate.get(WeekFields.ISO.weekOfYear())) {
  38. addWeek = 1;
  39. }
  40. long weeks = WEEKS.between(startDate, endDate) + addWeek;//Get the number of weeks in your case (52)
  41. List<String> numberWeeks = new ArrayList<>();
  42. if (weeks >= 0) {
  43. int week = 0;
  44. do {
  45. //Get the number of week
  46. LocalDateTime dt = startDate.plusWeeks(week);
  47. int weekNumber = dt.get(WeekFields.ISO.weekOfYear());
  48. numberWeeks.add(String.format("%d-W%d", dt.getYear(), weekNumber));
  49. week++;
  50. } while (week <= weeks);
  51. }
  52. System.out.println(numberWeeks);
  53. }
  54. }
Success #stdin #stdout 0.21s 34952KB
stdin
Standard input is empty
stdout
[2018-W34]
[2018-W34, 2018-W35]
[2018-W34, 2018-W35, 2018-W36, 2018-W37, 2018-W38, 2018-W39, 2018-W40, 2018-W41, 2018-W42, 2018-W43, 2018-W44, 2018-W45, 2018-W46, 2018-W47, 2018-W48, 2018-W49, 2018-W50, 2018-W51, 2018-W52, 2019-W1, 2019-W2, 2019-W3, 2019-W4, 2019-W5, 2019-W6, 2019-W7, 2019-W8, 2019-W9, 2019-W10, 2019-W11, 2019-W12, 2019-W13, 2019-W14, 2019-W15, 2019-W16, 2019-W17, 2019-W18, 2019-W19, 2019-W20, 2019-W21, 2019-W22, 2019-W23, 2019-W24, 2019-W25, 2019-W26, 2019-W27, 2019-W28, 2019-W29, 2019-W30, 2019-W31, 2019-W32, 2019-W33, 2019-W34]