Joda DateTime Clarification Point

I would like to do something like

private DateTime[] importantDates = { new DateTime(2013, 6, 15, 0, 0), new DateTime(2013, 9, 15, 0, 0) }; 

Where the year is always in the current year. Does Joda allow something like this without calculations?

Example: right now we live in 2013. I would rather not hard code this value.

For that matter, what I really like

 private DateTime[] importantDates = { new DateTime(current_year, 6, 15), new DateTime(current_year, 9, 15), }; 

Can this be done?

+4
source share
2 answers

DateTime.now().getYear() will return the current year ( javadoc ). You might want to use one of the overloaded versions to set the time zone, for example.

+4
source

Well, the easiest approach:

 int year = new DateTime().getYear(); DateTime[] dates = new DateTime[] { new DateTime(year, 6, 15, 0, 0), new DateTime(year, 9, 15, 0, 0), }; 

This is not ideal for variable variables, as it introduces an additional instance variable ( year ) for no good reason, but you can easily include it in a helper method:

 private final DateTime[] importantDates = createImportantDatesThisYear(); private static DateTime[] createImportantDatesThisYear() { int year = new DateTime().getYear(); return new DateTime[] { new DateTime(year, 6, 15, 0, 0), new DateTime(year, 9, 15, 0, 0), }; } 

Please note that all this code assumes that you want

+5
source

Source: https://habr.com/ru/post/1488371/


All Articles