I have the following Java
POJO:
public class Meeting {
private String id;
private String startDateAndTime;
private String endDateAndTime;
private int recurrenceTimes;
private String reccurenceType;
//getters and setters etc
}
This is essentially a high level view of a Meeting. I.e:
Meeting Id
First startDateAndTime
First endDateAndTime
How often it reccurs: recurrenceTimes
Recurrence Type: I.e. daily weekly etc
I would like to break this object down into the series of individual meetings, i.e. if the recurrence times was 5 and the type was "daily" then the first object will contain the original Start and End dateAndTimes and the 4 subsequent times will be dynamically generated to be at the same times but their start and end dateAndTimes will be on the days after this.
What would be the best way to do so?
I started the logic by looping
on amount of recurrenceTimes but I am confused as to how to create the subsequent strucutures
int recurrenceTimes= meeting.getRecurrenceTimes;
//Create Single Meeting Objects
for(int i=0; i<=recurrenceTimes i++){
String firstStartDate = meeting.getStartDate();
String firstEndDate = meeting.getEndDate();
//logic to create the subsqeuent Meeting Objects
}
Use the java.time classes for such date-time work.
LocalDateTime start = LocalDateTime.of( 2017 , Month.March , 27 ) ;
Define an Enum
for your recurrence types. See the Oracle Tutorial if not familiar with Java’s surprisingly useful enum facility.
public enum Recurrence {
DAILY , WEEKLY , MONTHLY , QUARTERLY , YEARLY
}
Use those Enum objects in your code.
if( Recurrence.WEEKLY.equals( meeting.recur ) ) {
LocalDateTime ldt = start.plusWeeks( n ) ;
Note that you do not need an ending date-time. A start plus a number of recurrences implies the ending. So no need to store that. If you need that ending for other purposes in your app, then of course calculate and store it.
So your class would look something like this.
public class Meeting {
private UUID id ; // java.util.UUID.randomUUID()
private LocalDateTime start ;
private Recurrence recur ; // Stores an object from your own Enum defining DAILY, WEEKLY, etc.
private Integer limit ; // The number of times this event recurs.
}
Collect your results in a List
.
List<LocalDateTime> list = new ArrayList<>() ;
…and…
list.add( ldt ) ;
Those LocalDateTime
objects have no concept of time zone or offset-from-UTC. So they do not represent a specific moment on the timeline.
For that you must apply a time zone to give them meaning. If that time-of-day on that date in that zone is invalid, adjustments are made automatically.
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ldt.atZone( 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.