Java – Period

Suppose you’re assign a job to run a program started from 1st of January 2019 until end of April 2019. You’re require to run through every week.

Java Period

The following job will run from first day of the defined date, and adding period of 1 week until the condition match and end the process.

public class Java8Period {

  public static void main(String[] args) {
    LocalDate start = LocalDate.of(2019, Month.JANUARY, 1);
    LocalDate end = LocalDate.of(2019, Month.MAY, 1);
    Period period = Period.ofWeeks(1);
    runSchedule(start, end, period);
  }
	
  public static void runSchedule(LocalDate start, LocalDate end, Period period) {
    while(start.isBefore(end)) {
      System.out.println("Do the job... " + start);
      start = start.plus(period);
    }
  }

Output

Do the job... 2019-01-01
Do the job... 2019-01-08
Do the job... 2019-01-15
Do the job... 2019-01-22
Do the job... 2019-01-29
Do the job... 2019-02-05
Do the job... 2019-02-12
Do the job... 2019-02-19
Do the job... 2019-02-26
Do the job... 2019-03-05
Do the job... 2019-03-12
Do the job... 2019-03-19
Do the job... 2019-03-26
Do the job... 2019-04-02
Do the job... 2019-04-09
Do the job... 2019-04-16
Do the job... 2019-04-23
Do the job... 2019-04-30
Java – Period

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.