By Sam


2012-06-19 17:51:49 8 Comments

I am using an API which requires a date parameter as a number of seconds, an int.

My problem is that I currently store this time in java.util.date and I was wondering if there is some way to convert the java.util.date variable to seconds so that I can fit it into the int parameter which the API requires?

4 comments

@マルちゃん だよ 2018-06-05 07:08:40

Since Java 8 and onwards there's this elegant method which returns the Epoch time in seconds (seconds since 0:00:0 January 1st 1970). You can then store this value as an numeric value: a "long" in this case.

long timestamp = java.time.Instant.now().getEpochSecond();

@Edwin Buck 2012-06-19 18:01:00

Number of seconds by itself doesn't mean much. Number of seconds within the current minute? Number of seconds since 0:00:00 Janurary 1st, 1970? Number of seconds since lunch? Could you be more specific.

Put it into the API also doesn't mean much, unless you specify exactly which API you are using, and where you are attempting to put these seconds.

@Subhrajyoti Majumder 2012-06-19 17:58:51

java.util.Date.getTime() it returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

    java.util.Date date=new Date();
    System.out.println(date.getTime());

Output: 1340128712111

@Karthik Kumar Viswanathan 2012-06-19 17:58:37

import java.util.Date;

... long secs = (new Date().getTime())/1000; ...

Please see - http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#getTime()

Related Questions

Sponsored Content

27 Answered Questions

[SOLVED] How to get an enum value from a string value in Java?

  • 2009-03-02 22:56:34
  • Malachi
  • 1104570 View
  • 1951 Score
  • 27 Answer
  • Tags:   java enums

49 Answered Questions

[SOLVED] Does a finally block always get executed in Java?

23 Answered Questions

[SOLVED] How to install Java 8 on Mac

14 Answered Questions

[SOLVED] Java string to date conversion

11 Answered Questions

[SOLVED] Dealing with "Xerces hell" in Java/Maven?

5 Answered Questions

36 Answered Questions

19 Answered Questions

[SOLVED] How do I make the method return type generic?

Sponsored Content