How to find epoch format current time of GMT using java

与世无争的帅哥 提交于 2019-12-10 10:46:33

问题


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?


回答1:


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;
}



回答2:


First, you should not store time since the epoch as a timestamp in your database. Look into the date-time datatypes that your DMBS offers. In Oracle I think that a date column will be OK. For most other DBMS you would need a datetime column. timestamp and timestamp with timezone may be other and possibly even sounder options depending on your exact requirements.

However, taking your word for it: Getting the number of milliseconds since the epoch is simple when you know how:

    long millisecondsSinceEpoch = System.currentTimeMillis();
    System.out.println(millisecondsSinceEpoch);

This just printed:

1533458641714

The epoch is defined in UTC, so in this case we need to concern ourselves with no other time zones.

If you needed seconds rather than milliseconds, it’s tempting to divide by 1000. However, doing your own time conversions is a bad habit since the libraries already offers them, and using the appropriate library methods gives clearer, more explanatory and less error-prone code:

    long secondsSinceEpoch = Instant.now().getEpochSecond();
    System.out.println(secondsSinceEpoch);

1533458641

You said:

I just need to get the current time of GMT time zone…

Again taking your word:

    OffsetDateTime currentTimeInUtc = OffsetDateTime.now(ZoneOffset.UTC);
    System.out.println(currentTimeInUtc);
    long millisecondsSinceEpoch = currentTimeInUtc.toInstant().toEpochMilli();
    System.out.println(millisecondsSinceEpoch);
2018-08-05T08:44:01.719265Z
1533458641719

I know that GMT and UTC are not exactly the same, but for most applications they can be (and are) used interchangeably.

Can someone tell me (if) the above code is right?

When I ran your code just now, its output agreed with mine except the milliseconds were rounded down to whole thousands (whole seconds):

1533458641000

Your code has some issues, though:

  • You are using the old, long out-dated and poorly designed classes SimpleDateFormat, Date and TimeZone. The first in particular has a reputation for being troublesome. Instead we should use java.time, the modern Java date and time API.
  • Bug: In your format pattern string you are using lowercase hh for hour of day. hh is for hour within AM or PM, from 1 through 12, so will give you incorrect results at least half of the day. Uppercase HH is for hour of day.
  • Don’t use GMT-7 as a time zone. Use for example America/Los_Angeles. Of course select the time zone that makes sense for your situation.
  • Since you ask for time in the GMT time zone, what are you using GMT-7 for at all?
  • There is no point that I can see in formatting your Date into a string and parsing it back. Even if you did it correctly, the only result you would get would be to lose your milliseconds since there are no milliseconds in your format (it only has second precision; this also explained the rounding down I observed).

Link: Oracle tutorial: Date Time explaining how to use java.time, the modern Java date and time API.




回答3:


Here is a solution using the java.time API

ZonedDateTime zdt = LocalDateTime.now().atZone(ZoneId.of("GMT-7"));
long millis = zdt.toInstant().toEpochMilli();


来源:https://stackoverflow.com/questions/51640313/how-to-find-epoch-format-current-time-of-gmt-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!