Get date based on number of days in Java

A simple question, but Google did surprisingly little. I have a number of days since January 1 of the year. How can I convert this to date in Java?

+4
source share
5 answers

Use calendar for date arithmetic

  Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, 2013); c.set(Calendar.MONTH, Calendar.JANUARY); c.set(Calendar.DATE, 1); c.set(Calendar.HOUR, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); c.add(Calendar.DATE, numberOfDays); Date date = c.getTime(); 

Please note that the result may differ for different locales due to DST (daylight saving time). The above example uses the standard locale.

+2
source

You can simply use SimpleDateFormat to convert String to Date . Sample D can be used to represent the number of days of the year.

if you have

 int numberOfDays = 42; // Arbitrary number. 

then this is considered the number of days since January 1, 1970 ( Age )

 Date date = new SimpleDateFormat("D").parse(String.valueOf(numberOfDays)); 

alternatively, this counts the number of days since January 1 of the current year.

 Date date = new SimpleDateFormat("D yyyy").parse(numberOfDays + " " + Calendar.getInstance().get(Calendar.YEAR)); 
+7
source

I find JodaTime very elegant to use when it comes to handling dates. With it, you can do it as follows:

 DateTime date = new DateTime().withDayOfYear(dayOfYear); 
+1
source

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?

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 .

0
source
 Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, 2016); c.set(Calendar.MONTH, 8); c.set(Calendar.DAY_OF_YEAR, 244); c.set(Calendar.HOUR, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); Date date = c.getTime(); 
-1
source

All Articles