Use different date as UNIX epoch time in Java?
NickName:timsterc Ask DateTime:2014-07-03T03:54:12

Use different date as UNIX epoch time in Java?

I know that the standard UNIX epoch time is 1/1/70 and I can use this variable to convert current time to UNIX time:

int time = (int) ((System.currentTimeMillis())/1000);

(Note: I know it's normally stored as a 64-bit long but I need it to be 32 bit, hence the cast to int).

My question is - are there any built-in methods in Java where you can pass in a different date as the "epoch date." What I am trying to get is a timestamp in # of seconds since a particular date. I.e. I would like to define January 1, 2015 as MY epoch. And then each future date would be # of seconds since 1/1/2015.

If there's no Java library that provides that capability, any ideas on how to do this? Thanks, Tim

Copyright Notice:Content Author:「timsterc」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/24539885/use-different-date-as-unix-epoch-time-in-java

Answers
Peter Lawrey 2014-07-02T20:04:35

There is no library needed, you just have to use maths.\n\nstatic final SimpleDateFormat SDF = new SimpleDateFormat(\"yyyy/MM/dd HH:mm:ss\");\nstatic final long EPOCH = new Date(2015 - 1900, Calendar.JANUARY, 1).getTime(); // 2015/1/1 in GMT\n\npublic static int secondSinceEpoch() {\n return (int) ((System.currentTimeMillis() - EPOCH) / 1000);\n}\n\npublic static String epochToString(int secsSince2015) {\n return SDF.format(new Date(secsSince2015 * 1000 + EPOCH));\n}\n\npublic static void main(String... ignored) {\n System.out.println(new Date(EPOCH));\n System.out.println(\"Epoch is \" + epochToString(0));\n System.out.println(\"Today is \" + secondSinceEpoch() + \" secs\");\n System.out.println(\"Today is \" + secondSinceEpoch() / 86400 + \" days\");\n}\n\n\nprints\n\nThu Jan 01 00:00:00 GMT 2015\nEpoch is 2015/01/01 00:00:00\nToday is -15732850 secs\nToday is -182 days\n",


Q23 2014-07-02T19:59:31

You'd probably have to roll your own time and date methods, which would basically repackage the standard ones while subtracting the correct number of seconds to the epoch to arrive at your new epoch.\n\nSo, maybe something like\n\npublic class MyEpoch {\n public static final Long MY_EPOCH = //number of seconds betweer 1/1/70 and 1/1/2015\n private Date unixEpochDate;\n //etc\n public Long getMyEpoch() {\n return unixEpochDate.getTime() - MY_EPOCH;\n }\n}\n",


More about “Use different date as UNIX epoch time in Java?” related questions

Use different date as UNIX epoch time in Java?

I know that the standard UNIX epoch time is 1/1/70 and I can use this variable to convert current time to UNIX time: int time = (int) ((System.currentTimeMillis())/1000); (Note: I know it's norm...

Show Detail

Unix epoch time to Java Date object

I have a string containing the UNIX Epoch time, and I need to convert it to a Java Date object. String date = "1081157732"; DateFormat df = new SimpleDateFormat(""); // This line try { Date expi...

Show Detail

How to convert a date time string to long (UNIX Epoch Time) in Java 8 (Scala)

I want the UNIX Epoch Time (Posix Time, Unix Time) of a string in some pattern, the string is in normal format (so UTC). Please using Java 8, not Joda or old Java. (For milliseconds please see Ho...

Show Detail

oracle convert unix epoch time to date

The context is that there is an existing application in our product which generates and sends the EPOCH number to an existing oracle procedure & vice versa. It works in that procedure using som...

Show Detail

How to convert a 13 digit unix epoch time format to date time?

I'm trying to transform a data frame column containing a 13 digit unix epoch time log into human readable date-time log format. Here's what I've tried: alerts[date_time] = alerts['epoch'].map(l...

Show Detail

Using unix() to create Unix epoch time

I want to create jalali moment and convert it to unix epoch time , Here is my code : const jalaliMoment = require('jalali-moment') let jalaliMoment1=jalaliMoment() app.post('/users', async ...

Show Detail

Java - get unix epoch with microsecond

I am struggling to get the unix time epoch with microseconds in this format(1586420933.453254). I tried different classes. - java.utils.Calendar - java.time.Instant - org.joda.time.DateTime But I...

Show Detail

How to convert a date time string to long (UNIX Epoch Time) Milliseconds in Java 8 (Scala)

This question solves the case for seconds: How to convert a date time string to long (UNIX Epoch Time) in Java 8 (Scala) But if I want milliseconds it seems I have to use def dateTimeStringToEpoc...

Show Detail

How do I get the Unix epoch in seconds for a date without time?

In Go, how do I get the Unix epoch in seconds for a date without time? For example, if I want the Unix epoch seconds for the current time, I can use: today := time.Now().Unix() For instance, if the

Show Detail

Find out the time since unix epoch for a certain date time?

I want to find out the time in unix time (ie seconds since the unix epoch) on 9:00 BST on 1st October 2009. How can I do this on the linux command line? I know you can use date @$UNIXTIME '+%somef...

Show Detail