Convert timestamp to date in Android?

I am using one Android app in which I want to convert the timestamp to date, but I could not succeed. I tried things below. Please check this if I am doing something wrong.

I pass this value:

myTimestamp=1328015914;
DateFormat.getDateFormat(mContext).format(new Date(myTimestamp * 1000));

He returns 11/1/1970, but its wrong.

Please help me in this matter.

Thanks Mantan Dalal

+5
source share
3 answers

You may need to specify the time zone. This is the method I use.

private String getDate(long timeStamp){
    DateFormat objFormatter = new SimpleDateFormat("dd-MM-yyyy");
    objFormatter.setTimeZone(TimeZone.getTimeZone(timezone));

    Calendar objCalendar =    
            Calendar.getInstance(TimeZone.getTimeZone(timezone));
    objCalendar.setTimeInMillis(timeStamp*1000);//edit
    String result = objFormatter.format(objCalendar.getTime());
    objCalendar.clear();
    return result;         
}
+4
source

Hey, how about creating a method that converts your timestamp to date. as -

public static Date convertTimestampToDate(Timestamp timestamp) {
    return new Date(timestamp.getTime());
}

Let me know if this works.

0
source
Calendar calendar = Calendar.getInstance();
TimeZone tz = TimeZone.getDefault();
calendar.add(Calendar.MILLISECOND,     
tz.getOffset(calendar.getTimeInMillis()));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date currenTimeZone=new java.util.Date((long)1379487711*1000);
Toast.makeText(TimeStampChkActivity.this, sdf.format(currenTimeZone), Toast.LENGTH_SHORT).show();
0

All Articles