How to find epoch format current time of GMT using java

ArrchanaMohan :

I have written below code which is running, and giving output. But I'm not sure It's a right one.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = new Date();
sdf.setTimeZone(TimeZone.getTimeZone("GMT-7"));
String value = sdf.format(date);
System.out.println(value);
Date date2 = sdf.parse(value);
long result = date2.getTime();
System.out.println(result);
return result;

The above code what I'm trying is, I just need to get the current time of GMT time zone and convert it as epoch format which is gonna used in Oracle db.

Can someone tell me that format, and the above code is right?

Madhan Varadhodiyil :

Why not use Calendar class?

public long getEpochTime(){
    return Calendar.getInstance(TimeZone.getTimeZone("GMT-7")).getTime().getTime()/1000; //(  milliseconds to seconds)
}

It'll return the current Date's Epoch/Unix Timestamp.

Based on Harald's Comment:

 public static long getEpochTime(){
    return Clock.system(TimeZone.getTimeZone("GMT-7").toZoneId() ).millis()/1000;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=86503&siteId=1