Reputation: 18639
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?
Upvotes: 2
Views: 1945
Reputation: 338775
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 );
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.
Upvotes: 0
Reputation: 29693
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
}
Upvotes: 3