tl;dr
java.util.concurrent.TimeUnit.DAYS.toSeconds( // Assuming generic 24-hour days.
Year.of( 2018 ).length() // Get number of days in year, either 365 or 366 (leap year).
) // Return a `long`, a count of seconds in year assuming 24-hour days.
31536000
That result is approximate, assuming generic 24-hour days. For a more accurate number, see Exact number of seconds section below.
java.time
The modern approach uses the java.time classes. Note that java.time uses a resolution of nanoseconds, much finer than Joda-Time’s milliseconds.
The Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes.
Generic number of seconds
If you want a general count of seconds assuming generic 24-hour days, use TimeUnit
enum, Year
class, and a little math. This approach ignores leap second.
long secondsPerGenericDay = TimeUnit.DAYS.toSeconds( 1 ) ; // Assuming 24-hour days.
long days = Year.of( 2018 ).length() ; // Either 365 or 366 depending on leap year.
long seconds = ( days * secondsPerGenericDay ) ;
…or…
long secondsInYear = TimeUnit.DAYS.toSeconds( Year.of( 2018 ).length() ) ;
See this code run live at IdeOne.com.
seconds: 31536000 = secondsInYear: 31536000
See also correct Answer by Elliott Frisch.
Exact number of seconds
If you want exact number of seconds in a year, that can vary by time zone. Anomalies such as Daylight Saving Time (DST) or other changes to a zone’s definition made by politicians mean that days can vary in length. They are not always exactly 24 hours in length.
The approach below does ignore leap seconds, when using a common implementation of Java. Leap seconds are unpredictable, so you must look to a historical record if you want to add them into your calculation. In advance, they are scheduled out only a few months.
LocalDate
The LocalDate
class represents a date-only value without time-of-day and without time zone.
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
Year
The Year
class can assist.
Year y = Year.now( z ) ;
LocalDate start = y.atDay( 1 ) ;
LocalDate stop = start.plusYears( 1 ) ;
First moment of the day
We need specific moments to calculate seconds. So need the first moment of each date.
Let java.time determine the first moment. Anomalies such as DST mean the day may start at some time other than 00:00.
ZonedDateTime zdtStart = start.atStartOfDay( z ) ;
ZonedDateTime zdtStop = stop.atStartOfDay( z ) ;
Elapsed time
We can represent a span of time unattached to the timeline with a resolution of seconds plus fractional second using the Duration
class.
Duration d = Duration.between( zdtStart , zdtStop ) ;
Interrogate for the number of whole seconds.
long seconds = d.getSeconds() ;
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.