Question:
How do you calculate the day of the year?
Answer:
Getting the field DAY_OF_YEAR in the Calendar class.
Code:
import java.util.Calendar; import java.util.Date; public class DayOfYear { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); int day = calendar.get(Calendar.DAY_OF_YEAR); Date date = calendar.getTime(); System.out.println(date); System.out.println("Today is day " + day + " of the year"); } }
Output:
$ java DayOfYear Sun Feb 16 15:42:22 EST 2014 Today is day 47 of the year