Convert timestamp to date

I get the date from sms message in android as a timestamp in seconds or milliseconds from an era. I need to display it as a standard date and time using java.

int date      = (cursor.getColumnIndex(SmsReceiver.DATE));

this returns some number, for example 1308114404722.

which process to use this number and display as the current date

+1
source share
6 answers
int date = ...
Date dateObj = new Date(date);

To format an object Date, use, for example SimpleDateFormat:

DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
String text = df.format(dateObj);

In addition, you must store the number-in milliseconds from the moment-epoch in long, not in int, because it is intnot large enough. (In fact, the number 1308114404722 does not even fit into 32-bit int).

+6

SimpleDateFormat formatter = new SimpleDateFormat("HH:mm dd.MM.yyyy");
Date date = new Date(1308114404722);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
+1

public static java.util.Date toDate(java.sql.Timestamp timestamp) {
long millisec = timestamp.getTime() + (timestamp.getNanos() / 1000000);
return new Date(millisec);
}
+1

Try:

DateFormat df = DateFormat.getInstance();
String dateStr = df.format( new Date( date ) );
0
source

I believe that this is a temporary mark of the era in order to transform it into a form that is understandable to humans.

int date = (cursor.getColumnIndex(SmsReceiver.DATE));
String date = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new java.util.Date (date*1000));

hope this will work both on converting this value: 1308114404722

the time is now Wed, 15 Jun 2011 05:06:44 GMT

0
source

To convert the unix timestamp you get to a regular date, use this piece of code:

// date here must be long

long date = (cursor.getColumnIndex (SmsReceiver.DATE));

String standardDate = DateFormat.getDateInstance (). format (new date (date);

-1
source

All Articles