2

How can I create Java calendar which will point on Tuesday, Wednesday at 5:30 pm, every two weeks.

Calendar cal = Calendar.getInstance();

Should it be two separate calendars? One for Tuesday and second for Wednesday? how can get every two weeks?

I googled it, but has not found any example.

Can you please hep?

fge
  • 119,121
  • 33
  • 254
  • 329
danny.lesnik
  • 18,479
  • 29
  • 135
  • 200
  • 5
    Out of curiosity: have you considered using Joda Time? It has facilites for such constructs. – fge Dec 25 '12 at 16:16
  • @fge, I need it as calendar for further usage in quartz. – danny.lesnik Dec 25 '12 at 16:17
  • 1
    I have added `quartz-scheduler` as a tag: chances are it has this sort of mechanisms in place already – fge Dec 25 '12 at 16:21
  • See [CronTrigger](http://quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger) – mishadoff Dec 25 '12 at 16:22
  • @mishadoff, it doesn't help, I verified it already. – danny.lesnik Dec 25 '12 at 16:22
  • On the other hand, if your job is purely Java based, you can always check the result of `cal.getWeekYear() % 2`... – fge Dec 25 '12 at 16:29
  • FYI, the [Joda-Time](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), with the team advising migration to the [java.time](http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. – Basil Bourque Feb 21 '17 at 03:24

3 Answers3

3
  Calendar startWed = Calendar.getInstance();
  startWed.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
  startWed.set(Calendar.HOUR, 17);
  startWed.set(Calendar.MINUTE, 30);
  Calendar startThu = Calendar.getInstance();
  startThu.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
  startThu.set(Calendar.HOUR, 17);
  startThu.set(Calendar.MINUTE, 30);

  for (int i = 0; i++ < 100;)
  {
     startWed.add(Calendar.DAY_OF_YEAR, 7 * 2); // each 2 weeks
     startThu.add(Calendar.DAY_OF_YEAR, 7 * 2); // each 2 weeks
  }
Ilya
  • 29,135
  • 19
  • 110
  • 158
1

You have just to use the number of the

WEEK_OF_YEAR % 2 [== or !=] 0

cl-r
  • 1,264
  • 1
  • 12
  • 26
0

Using java.time

The modern way is with the java.time classes rather than the troublesome old legacy classes such as Calendar.

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );

TemporalAdjuster

The TemporalAdjuster interface provides for classes that can make adjustments to date-time values. The TemporalAdjusters class provides several handy implementations. One gets the next desired day-of-week you specify with the DayOfWeek enum.

LocalDate nextOrSameTuesday = today.with( TemporalAdjusters.nextOrSame( DayOfWeek.TUESDAY );
LocalDate nextOrSameWednesday = today.with( TemporalAdjusters.nextOrSame( DayOfWeek.WEDNESDAY );

Get succeeding weeks by adding a number of weeks.

LocalDate followingTuesday = nextOrSameTuesday.plusWeeks( 1 );

To get a moment, an actual point on the timeline, apply your desired time-of-day along with a time zone to get a ZonedDateTime object.

LocalTime localTime = LocalTime.of( 17 , 30 );
ZonedDateTime zdt = ZonedDateTime.of( nextOrSameTuesday , localTime, z );

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154