I think you tried a little bit too much:
LocalDate date = new LocalDate(); // note: current date in system time zone
date = date.withMonthOfYear(1); // set to January
while (date.getMonthOfYear() < 12) {
long monthStart = date.dayOfMonth().withMinimumValue().toDateTimeAtStartOfDay().getMillis();
long monthEnd = date.dayOfMonth().withMaximumValue().toDateTimeAtStartOfDay().getMillis();
// store your longs in db
date = date.plusMonths(1);
}
Personally I would like you to reconsider your decision to store the results as absolute long-millis. This approach makes it difficult to change the stored timestamps back to plain dates. Sometimes there will not be a reversible 1:1-mapping. ANSI-SQL has the column type definition of a DATE without time or timezone. In JDBC you can then use the class java.sql.Date
. Generally you should choose the data type which is most appropriate for your domain use case. A UNIX-like timestamp in millis should not be seen as all-purpose solution. So if you decide to choose a plain date instead then your code can be simplified further! Then no need for conversion to DateTime
at begin of day (even timezone-dependent).