-2

how to convert the current date to time-stamp through code

    String timeFormat = "dd.MM.yy, h:mm a";
    Calendar c = Calendar.getInstance();
    SimpleDateFormat df = new SimpleDateFormat(timeFormat);
    String formattedDate = df.format(c.getTime());
    Log.d("print"+c.getTime());

is it possible to convert the c.getTime to timestamp

this is my code but i dont know how to convert it to timestamp.

Martin j
  • 511
  • 1
  • 9
  • 31
  • What do you mean by timestamp, that is, in what format do you want it? In what sense doesn’t you `formattedDate` qualify as a timestamp? Maybe best if you give an example of your desired result. The question is easy enough, only not when we don’t know what you want. – Ole V.V. Jan 19 '18 at 09:31
  • I am sure this question has been asked and answered more than once before. Your search engine will give you a good answer faster than anyone can enter one here. – Ole V.V. Jan 19 '18 at 09:32
  • Possible duplicate of [How can I get the current milliseconds from the current time](https://stackoverflow.com/questions/11785200/how-can-i-get-the-current-milliseconds-from-the-current-time) – Ole V.V. Jan 19 '18 at 09:37

3 Answers3

2

As said in the documentation,
https://docs.oracle.com/javase/7/docs/api/java/util/Date.html#getTime()
the function getTime() of Date object returns the

time in milliseconds since January 1, 1970, 00:00:00 GMT

which is the timestamp that you want, so

Date d = c.getTime();
long timestamp = d.getTime();

would be enough

regev avraham
  • 1,102
  • 1
  • 9
  • 24
0

Have 2 way to get current Timestamp. In your code c.getTime() is enough

Date date = new Date();
long timestamp = date.getTime();

OR

long timestamp = System.currentTimeMillis();
Tung Duong
  • 1,156
  • 7
  • 19
-1

System.currentTimeMillis()

this will return the timestamp of cutrrent date time

Martin j
  • 511
  • 1
  • 9
  • 31