Convert Unix epoch time
NickName:Ryan B Ask DateTime:2022-09-14T09:32:13

Convert Unix epoch time

I am trying to convert Unix epoch time to an ISO 8601 (https://en.wikipedia.org/wiki/ISO_8601) with the time zone. I have it working but only if it's local time. How can I pass in the time zone as an offset?

Input 1: 1483772400000

Input 2: -0700 (but I can convert as needed)

Desired output: 2017-01-07T00:00:00-07:00 or simply 2017-01-07T00:00:00-07

Current output: 2017-01-06 23:00:00-08 as I'm in PST

I'm using IntelliJ IDEA, JDK 1.8u202 .

    package util.date;
    
    import java.util.Date;
    import java.util.Locale;
    import java.util.TimeZone;
    import java.text.SimpleDateFormat;
    
    public class EpochToISO {
        public String epochToIso8601(long time) {
            String format = "yyyy-MM-dd HH:mm:ssX";
            SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
            sdf.setTimeZone(TimeZone.getDefault());
            return sdf.format(new Date(time));
        }
    
        public static void main(String[] args) {
            EpochToISO convert = new EpochToISO();
            long epoch = 1483772400000; // Example
            String iso8601 = convert.epochToIso8601(epoch);
            System.out.println(epoch + " -> " + iso8601);
        }
    }

Copyright Notice:Content Author:「Ryan B」,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/73710571/convert-unix-epoch-time

More about “Convert Unix epoch time” related questions

convert unix epoch time to human readable time

First I have an Unix epoch time 1270787111. Then based on this website: http://untangible.com/2009/01/covert-unix-epoch-dates-in-microsoft-excel-including-timezone-examples.html, I convert the nu...

Show Detail

How to convert Unix epoch time in SQLite

Could you help me convert UNIX epoch time into format yyyy-mm-dd hh:mm:ss (24h) in SQLite? (GMT+7 would be appreciated). Example: from 1319017136629 to Wednesday, October 19, 2011 4:38:56 PM GMT+7...

Show Detail

How to convert Unix epoch time in SQLite

Could you help me convert UNIX epoch time into format yyyy-mm-dd hh:mm:ss (24h) in SQLite? (GMT+7 would be appreciated). Example: from 1319017136629 to Wednesday, October 19, 2011 4:38:56 PM GMT+7...

Show Detail

How to convert Unix epoch time in SQLite

Could you help me convert UNIX epoch time into format yyyy-mm-dd hh:mm:ss (24h) in SQLite? (GMT+7 would be appreciated). Example: from 1319017136629 to Wednesday, October 19, 2011 4:38:56 PM GMT+7...

Show Detail

Convert Unix epoch time

I am trying to convert Unix epoch time to an ISO 8601 (https://en.wikipedia.org/wiki/ISO_8601) with the time zone. I have it working but only if it's local time. How can I pass in the time zone as...

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

Perl convert localtime to unix (epoch) time

using perl, I am trying to estimate the time since a file was created. I would like to convert the local time to unix time (epoch), then take unix time of the file & subtract. The problem I fa...

Show Detail

Conversion of unix epoch time to windows epoch time in python

Quick question: Is there a pythonic (whether in the standard libraries or not) way to convert unix 32-bit epoch time to windows 64-bit epoch time and back again?

Show Detail

Convert UNIX epoch time to SYSTEMTIME

In my C++ windows app I am converting a SYSTEMTIME to a formatted string like so: SYSTEMTIME systemTime; GetSystemTime(&systemTime); char pSystemTime[50]; sprintf_s(pSystemTime...

Show Detail

How to convert epoch unix time to TOD clock (STCK)?

Please can you help me to convert epoch Unix time to TOD clock (STCK) 8 bytes. e.g : EPOCH time in Hex = '175BC71AFE2'X EPOCH time in Decimal = 1,605,184,368,610 and I want to convert this decim...

Show Detail