How do I convert day of the year to date?

Bookmark this example!  
Category: java.util, viewed: 1097 time(s).
 
package org.kodejava.example.util;
 
import java.util.Calendar;
 
public class DayOfYearToDate {
    public static void main(String[] args) {
	//
	// In the example we want to get the date value of the specified day of
	// the year. Using the calendar object we can define our calendar for
	// a specific day of the year.
	//
 
	int dayOfYear = 112;
	Calendar calendar = Calendar.getInstance();
	calendar.set(Calendar.DAY_OF_YEAR, dayOfYear);
 
	System.out.println("Day of year " + dayOfYear + " = "
		+ calendar.getTime());
 
	// 
	// If you want to get the date for a specific day of year and for a
	// specific year, you can also pass the year information to the calendar
	// object.
	//
 
	int year = 2004;
	calendar.set(Calendar.YEAR, year);
	System.out.println("Day of year " + dayOfYear + " in year " + year
		+ " = " + calendar.getTime());
    }
}
 
 

And here is an example result of the code above:

Day of year 112 = Sun Apr 22 16:53:03 GMT+07:00 2007
Day of year 112 in year 2004 = Wed Apr 21 16:53:03 GMT+07:00 2004
Can't find what you are looking for? Join our FORUMS and ask some questions!

Most Viewed Examples

Latest Code Examples

100 Top & Latest


Locations of visitors to this page
eXTReMe Tracker
visitor stats
Get Chitika Premium