Link to home
Start Free TrialLog in
Avatar of Sathish David  Kumar N
Sathish David Kumar NFlag for India

asked on

how to get the GMT time zone return format as timestamp

how to get the GMT time zone return format as timestamp

static final String DATEFORMAT = "dd-MM-yyyy HH:mm:ss";
Avatar of Nicolas Lecanu
Nicolas Lecanu
Flag of Guadeloupe image

Hi Sathish David  Kumar N,

Can you try this ?

Instant instant = Instant.now();
Timestamp timestamp = Timestamp.from(instant);
assertEquals(instant.toEpochMilli(), timestamp.getTime());

Open in new window

Try

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class MachineTimestamp {
    static final String DATEFORMAT = "dd-MM-yyyy HH:mm:ss";

    public static void main(String args[]) {
        Instant machineTimestamp = Instant.now();
        System.out.println("The current machine timestamp in UTC:" + machineTimestamp);
        ZonedDateTime gmtDT = machineTimestamp.atZone(ZoneId.of("UTC"));
        System.out.println("Current ts in GMT: " + gmtDT.format(DateTimeFormatter.ofPattern(DATEFORMAT)));
    }
}

Open in new window

Avatar of Sathish David  Kumar N

ASKER

I got wrong answer @CEHJ

The current machine timestamp in UTC:2019-10-31T06:56:24.218Z
Current ts in GMT: 2019-56-31 06:56:24
I want java.sql.timestamp
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:)