Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find epoch format current time of GMT using java

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?

like image 386
ArrchanaMohan Avatar asked Aug 01 '18 18:08

ArrchanaMohan


3 Answers

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. Edit: You said:

    I just want to specify the timezone for sanjose. GMT-7 is refer to sanjose current time.

    I believe many places are called San Jose. If you mean San Jose, California, USA, you are going to modify your program to use GMT-8 every time California goes back to standard time and opposite when summer time (DST) begins?? Miserable idea. Use America/Los_Angeles and your program will work all year.

  • 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).

Links

  • Oracle tutorial: Date Time explaining how to use java.time, the modern Java date and time API.
  • San Jose, California on Wikipedia
like image 72
Ole V.V. Avatar answered Nov 14 '22 23:11

Ole V.V.


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;
}
like image 29
Madhan Varadhodiyil Avatar answered Nov 15 '22 00:11

Madhan Varadhodiyil


Here is a solution using the java.time API

ZonedDateTime zdt = LocalDateTime.now().atZone(ZoneId.of("GMT-7"));
long millis = zdt.toInstant().toEpochMilli();
like image 41
gtgaxiola Avatar answered Nov 14 '22 23:11

gtgaxiola