如何使用Java查找GMT的时代格式当前时间 [英] How to find epoch format current time of GMT using java

查看:676
本文介绍了如何使用Java查找GMT的时代格式当前时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面编写了正在运行的代码,并提供输出.但是我不确定这是对的.

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;

上面的代码是我想要的,我只需要获取GMT时区的当前时间并将其转换为将在Oracle数据库中使用的纪元格式即可.

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?

推荐答案

为什么不使用Calendar类?

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

它将返回当前日期的时代/Unix时间戳.

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

基于Harald的评论:

Based on Harald's Comment:

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

这篇关于如何使用Java查找GMT的时代格式当前时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆