Java tutorial
//package com.java2s; //License from project: Apache License import java.util.Calendar; import java.util.Date; public class Main { /** * Calculates the current day of the year and returns an int * @return Day - Day of the year. No time info. */ public static int getCurrentDay() { return Calendar.getInstance().get(Calendar.DAY_OF_YEAR); } /** * Same as the getCurrentDay() method but uses the passed in argument * as a long milliseconds timestamp to calculate vs a day object * @parma timestamp This is the time (in milliseconds from the epoch date) * used to calculate the current day of the year. (IE 365 = Dec 31st) * @return Day Day of the year based on timestamp. */ public static Integer getCurrentDay(long timestamp) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(timestamp); return cal.get(Calendar.DAY_OF_YEAR); } /** * Returns the day of the year (IE 365 = Dec 31st) * @param date This is the date to use to calculate the day of the year * @return Day - Day of the year based on date. */ public static Integer getCurrentDay(Date date) { Calendar cal = Calendar.getInstance(); if (date != null) cal.setTime(date); return cal.get(Calendar.DAY_OF_YEAR); } }