java.time
Use the modern java.time classes that supplant the troublesome old date-time classes.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" );
LocalDate start = LocalDate.parse( "01/07/2015" , f );
LocalDate stop = LocalDate.parse( "31/07/2015" , f );
List<LocalDate> dates = new ArrayList<>( ChronoUnit.DAYS.between( start , stop ) + 1 );
LocalDate ld = start ;
while ( ! ld.isAfter( stop ) ) {
dates.add( ld );
// Set up next loop.
ld = ld.plusDays( 1 );
}
To get Strings of the name of the day of week, use the DayOfWeek
enum and its ability to localize the name of the day of the week.
…
String output = ld.getDayOfWeek().getDisplayName( TextStyle.FULL_STANDALONE , Locale.US ); // Or Locale.CANADA_FRENCH etc.
…
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).
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.