TL; DR
Year.of( 2017 ) .atDay( 159 )
... or for the current year ...
Year.now( ZoneId.of( "America/Montreal" ) ) .atDay( 159 )
Going in a different direction, from the date to the day of the year.
LocalDate.now( ZoneId.of( "America/Montreal" ) .getDayOfYear()
Using java.time
The modern way to handle date-time is with the java.time classes. Problem old date-time. Sophisticated old time classes such as java.util.Date , java.util.Calendar and java.text.SimpleTextFormat now legacy superseded by java.time .
Year class represents the year, obviously. To get the current year, we need the current date.
The time zone is critical for determining the date and therefore the year. At any given moment, the date changes around the world by zone. For example, a few minutes after midnight in Paris, France is a new day, still "yesterday" in Montreal Quebec . Similarly, a few minutes after midnight in Paris on New Year's Eve, while "last year" in Quebec.
Specify the time zone name in continent/region format, such as America/Montreal , Africa/Casablanca or Pacific/Auckland . Never use the abbreviation 3-4 letters, for example EST or IST , as they are not real time zones, and are not standardized and not even unique (!).
ZoneId z = ZoneId.of( "America/Montreal" ); Year year = Year.now( z );
If you have a specific year, skip the year number.
Year year = Year.of( 2017 );
The Year class includes the atDay method to generate LocalDate when the number of days has passed from 1 to 365 or 366 in the Leap Year . The LocalDate class represents a date value only without time and without a time zone.
LocalDate localDate = year.atDay( 159 );
Moving in the other direction, you can poll LocalDate for its day of the year by calling LocalDate::getDayOfYear .
int dayOfYear = localDate.getDayOfYear() ;
About java.time
The java.time framework is built into Java 8 and later. These classes supersede the nasty old legacy time classes such as java.util.Date , Calendar and SimpleDateFormat .
The Joda-Time project, now in maintenance mode , we recommend switching to the java.time classes.
To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations. JSR 310 specification .
Where to get java.time classes?
- Java SE 8 and SE 9 and later
- Built in.
- Part of the standard Java API with integrated implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Most of the functionality of java.time is ported back to Java 6 and 7 in ThreeTen-Backport .
- Android
The ThreeTen-Extra project extends java.time with additional classes. This project is proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and more .