0

I have my small code and I want to get the bi-weekly but can't seem to get it right. How do I Display bi-Weekly in the Calender class. On my Bi Weekly I just put in random code.

   case "Bi-Weekly": {
                beginCalendar.add(Calendar.DAY_OF_MONTH, Calendar.WEDNESDAY);
                break;
            }
case "Month": {
                beginCalendar.add(Calendar.MONTH, 1);
                break;
            }


            case "Quarterly": {
                beginCalendar.add(Calendar.MONTH, 3);
                break;
            }
            case "Half Yearly": {
                beginCalendar.add(Calendar.MONTH, 6);
                break;
            }
            case "Year": {
                beginCalendar.add(Calendar.MONTH, 12);
                break;
Maria
  • 377
  • 2
  • 15
  • 1
    Consider not using the `Calendar` class. It is poorly designed and long outdated. Instead you may add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project so you can use [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Mar 28 '19 at 03:45

2 Answers2

0

I think

.add(Calendar.WEEK_OF_YEAR,2);

is the right one even though I would do some testing on how it behaves around the year-change

jonathan Heindl
  • 844
  • 7
  • 16
  • The answer is correct (and if I said not to use the `Calendar` class, I would just be repeating myself). – Ole V.V. Mar 28 '19 at 07:50
0

java.time

    LocalDate date = LocalDate.of(2019, Month.DECEMBER, 25);

    switch (recurrence) {
        case "Bi-Weekly": {
            date = date.plusWeeks(2);
            break;
        }
        case "Month": {
            date = date.plusMonths(1);
            break;
        }
        case "Quarterly": {
            date = date.plusMonths(3);
            break;
        }
        case "Half Yearly": {
            date = date.plusMonths(6);
            break;
        }
        case "Year": {
            date = date.plusYears(1);
            break;
        }

        default:
            System.err.println("Unrecognized recurrence: " + recurrence);
            break;
    }

    System.out.println("Added for " + recurrence + " gave: " + date);

Trying the code out with the difference strings (in a different order) gave:

Added for Bi-Weekly gave: 2020-01-08
Added for Year gave: 2020-12-25
Added for Quarterly gave: 2020-03-25
Added for Half Yearly gave: 2020-06-25
Added for Month gave: 2020-01-25

Have your calendar events got time of day too? No problem: the code works the same if using a ZonedDateTime or a LocalDateTime instead of a LocalDate.

The Calendar class that you used is poorly designed and long outdated. I find java.time, the modern Java date and time API, so much nicer to work with. Which is why I wanted to show you this option.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161