Today is nth day of year [duplicate]
Solution 1:
Calendar calendar = Calendar.getInstance();
int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
Or using Joda-API
DateTime dt = new DateTime();
int dayOfYear = dt.getDayOfYear();
If you need 'th'
part, use switch statement
switch (dayOfYear > 20 ? (dayOfYear % 10) : dayOfYear) {
case 1: return dayOfYear + "st";
break;
case 2: return dayOfYear + "nd";
break;
case 3: return dayOfYear + "rd";
break;
default: return dayOfYear + "th";
break;
}
Solution 2:
LocalDate
Use the LocalDate
class in java.time package built into Java 8 and later.
Get the day-of-year:
int dayOfYear = LocalDate.now().getDayOfYear();
…and set the day-of-year:
LocalDate localDate = LocalDate.now().withDayOfYear( 195 );