Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java time since the epoch

In Java, how can I print out the time since the epoch given in seconds and nanoseconds in the following format :

java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

My input is:

long mnSeconds;
long mnNanoseconds;

Where the total of the two is the elapsed time since the epoch 1970-01-01 00:00:00.0.

like image 428
user1585643 Avatar asked Aug 08 '12 18:08

user1585643


People also ask

What is Java epoch time?

The epoch-seconds are measured from the standard Java epoch of 1970-01-01T00:00:00Z where instants after the epoch have positive values, and earlier instants have negative values. For both the epoch-second and nanosecond parts, a larger value is always later on the time-line than a smaller value.

How is epoch time calculated?

Epoch Time Difference FormulaMultiply the two dates' absolute difference by 86400 to get the Epoch Time in seconds – using the example dates above, is 319080600.

How do you convert date to epoch time?

Convert from human-readable date to epochlong epoch = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse("01/01/1970 01:00:00").getTime() / 1000; Timestamp in seconds, remove '/1000' for milliseconds. date +%s -d"Jan 1, 1980 00:00:01" Replace '-d' with '-ud' to input in GMT/UTC time.


Video Answer


2 Answers

Use this and divide by 1000

long epoch = System.currentTimeMillis();

System.out.println("Epoch : " + (epoch / 1000));
like image 172
ProfessionalAmateur Avatar answered Sep 20 '22 08:09

ProfessionalAmateur


tl;dr

    Instant                        // Represent a moment in UTC.
    .ofEpochSecond( mnSeconds )   // Determine a moment from a count of whole seconds since the Unix epoch of the first moment of 1970 in UTC (1970-01-01T00:00Z). 
    .plusNanos( mnNanoseconds )    // Add on a fractional second as a count of nanoseconds. Returns another `Instant` object, per Immutable Objects pattern.
    .toString()                    // Generate text representing this `Instant` object in standard ISO 8601 format.
    .replace( "T" , " " )          // Replace the `T` in the middle with a SPACE. 
    .replace "Z" , "" )            // Remove the `Z` on the end (indicating UTC).

java.time

The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date, .Calendar, java.text.SimpleDateFormat, java.sql.Date, and more. The Joda-Time team also advises migration to java.time.

Instant

The Instant class represents a moment on the timeline in UTC with a resolution up to nanoseconds.

long mnSeconds = … ;
long mnNanoseconds = … ;

Instant instant = Instant.ofEpochSecond( mnSeconds ).plusNanos( mnNanoseconds );

Or pass both numbers to the of, as two arguments. Different syntax, same result.

Instant instant = Instant.ofEpochSecond( mnSeconds , mnNanoseconds );

To get a String representing this date-time value, call Instant::toString.

String output = instant.toString();

You will get a value such as 2011-12-03T10:15:30.987654321Z, standard ISO 8601 format. Replace the T with a SPACE if you wish. For other formats, search Stack Overflow to learn about DateTimeFormatter.


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?

  • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

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.

like image 24
Basil Bourque Avatar answered Sep 19 '22 08:09

Basil Bourque