0

I am working on an application in which I have to convert a long value to a Date string and display. To achieve the purpose I am using following function, but it is returning me the date from 70's and 80's obviously not appropriate. I am using the following finction:

public static String convertDateFromLongToCompleteString(long date) {
    Date d = new Date(date * 1000);
    SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy hh:mm a");
    String formattedDateFromLong = dateformat.format(d);
    return formattedDateFromLong;
}

The long value is just simply System.currentTimeMillis() and when I have to show it to the user, I have to format that for which I am using above function. I have checked system and device dates, their zones and time, everything is just fine. Please update that why is this issue appearing and how can I get the exact date. Thanks!

Edit

I have also tried withoout multiplication with 1000, it gives me time and date from 1970.

Usama Sarwar
  • 8,922
  • 7
  • 54
  • 80

3 Answers3

0

If your long date is simply System.currentTimeMillis(), then multiplication with 1000 is not required.

Date d = new Date(date);

Rahul
  • 44,383
  • 11
  • 84
  • 103
0

Replace Date d = new Date(date * 1000); with Date d = new Date(date);

g00dy
  • 6,752
  • 2
  • 30
  • 43
  • The way you're using it will give you a time like the number of milliseconds since Jan. 1, 1970 GMT. * 1000 - which is around END of February year 44 970 I guess. – g00dy Feb 28 '13 at 11:31
0

In case you're using the above method only with System.currentTimeMillis(), you can call Date constructor without any parameters, it will give you the Date object that refers to the current date and time. This will be an easier way to solve your problem. Hope this helps.

Egor
  • 39,695
  • 10
  • 113
  • 130
  • I get the time, save its value in DB, when I have to show it to user then I format it. – Usama Sarwar Feb 28 '13 at 11:39
  • @UsamaSarwar, Are you sure that after you store the value in DB and then get it back it's the same value? Maybe that's the problem? Cause I don't really believe that System.currentTimeMillis() is giving you 1970, if of course you're not in 1970 :) – Egor Feb 28 '13 at 11:45
  • ye..I save this value in a table and then retrieve that saved value. – Usama Sarwar Feb 28 '13 at 11:49
  • @UsamaSarwar, And the value is still wrong? There's a mistake in your code somewhere and there are not enough details for me to help you find it. – Egor Feb 28 '13 at 11:52