tl;dr
Duration.between( // Represent a span-of-time unattached to the timeline.
Instant.parse( "1989-12-31T00:00:00Z" ) , // Garmin epoch reference date.
Instant.now() // Capture the current moment in UTC.
) // Returns a `Duration` object.
.toSeconds() // This span-of-time as a total number of whole seconds. Beware of data loss: Any fractional second is ignored.
Details
The other Answers use outmoded classes.
java.time
Java 8 and later comes with the java.time framework built-in. Supplants the old troublesome date-time classes such as java.util.Date
& .Calendar
.
long
vs int
As others mentioned, Java has no unsigned integer – only signed. So here we use a 64-bit long
rather than a 32-bit int
. Java 8 lets you perform some operations on int
values while pretending they are unsigned, but we do not actually have an unsigned int
type. See this other Question, Declaring an unsigned int in java, for more info including a couple of classes designed for unsigned integers in libraries such as Google Guava.
Instant
In java.time, an Instant
is a moment on the timeline in UTC. A Duration
represents a span of time as a total number of seconds plus a fraction of a second as nanoseconds.
First moment of the day
Not sure if “12:00 am December 31, 1989 UTC” meant the first moment of the day (00:00:00.0
) or the end of the day. I will go with first moment as the epoch. Indeed, this PDF document Garmin Device Interface Specification, supposedly published by Garmin in 2004-09-16, states:
7.3.14 time_type
The time_type is used in some data structures to indicate an absolute time. It is an unsigned 32 bit integer and its value is the number of seconds since 12:00 am December 31, 1989 UTC.
Code.
Instant garminEpoch = Instant.parse( "1989-12-31T00:00:00Z" );
Instant now = Instant.now();
Duration duration = Duration.between( garminEpoch , now );
long seconds = duration.getSeconds(); // This span of time as a total number of seconds.
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.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
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.