0

I want to display the date of a trip. In the application several trips are listed, each has a long value with the currentTimeMillis of its generation. I want to turn this long number into a date but the output is always something around 1970... Is System.currentTimeMillis() the right way to store the time of the trip generation?

Date date = new Date(trip.getStartTime());  // 195342322
SimpleDateFormat test = new SimpleDateFormat("yyyy-MM-dd"); 
startTime.setText(String.valueOf(test.format(date)));

-> Output of startTime: 1970-01-01

  • It seems you trip start time is truncated somewhere. 195342322 milliseconds it is 1970-01-03. 195342322 is very small value to be right date, today milliseconds values is 1357406971942 – Georgy Gobozov Jan 05 '13 at 17:31
  • Any chance you are internally converting from long to int then back to long? – Andrew T Finnell Jan 05 '13 at 17:41

3 Answers3

1

Try to use Calendar for simple formatting

Roman Minenok
  • 9,328
  • 4
  • 26
  • 26
0

We don't know how trip.getStartTime() is implemented, however it returns a wrong value. 195342322 is Wed, 10 Mar 1976 21:45:22 GMT (you can check it online)

Since the only non-deprecated Date constructor accepts a long argument, I guess you used that. It's better to store the date(time) in a database-friendly way, such as ISO 8601. Note that if you store it on the client in a SQLite database, SQLite doesn't have a time type, and you have to use a text field for that.

Once you switch to the text representation for serialization, you will be able to get back a Date by using SimpleDateFormat.parse()

Raffaele
  • 20,627
  • 6
  • 47
  • 86
0

use something like this:

Calendar calendar=Calendar.getInstance();
calendar.setTimeInMillis(trip.getStartTime());
Date date =calendar.getTime ();

as shown here .

android developer
  • 114,585
  • 152
  • 739
  • 1,270