0

I'm trying to convert this time stamp value to date but its giving me wrong time. date is correct

TimeStamp : 1423821615
True Value : Fri, 13 Feb 2015 10:00:15 GMT
Android Code shows : Fri, 13 Feb 2015 15:30:15 IST

Here is the code I'm using to convert time stamp to date.

Date dt = new Date((long)timestampInSeconds * 1000);

I tried this code too but same result

public static Date getDateFromTimeStamp(long timestampInMilliseconds) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(timestampInMilliseconds);
    return cal.getTime();
}

Date dt = getDateFromTimeStamp((long)timestampInSeconds * 1000);

I don't know what I'm doing wrong. Please help


Now I explain the whole scenario. My client is from UK and I'm from India (+5:30 ahead). He created appointment for 10 AM in UK obviously. But now I have his database in my local PC. My .NET software it shows same time as it shows in below image of SQL server. But in mobile, it doesn't. PC and mobile both are in same time zone.

enter image description here

I use this code to convert date to time stamp and send this time stamp to mobile app through web service

SELECT DATEDIFF(SECOND,{d '1970-01-01'}, Appointments.DateTime) AS AppointmentTimeStamp FROM Appointments

Here is image of what my .NET software displays
enter image description here

does it matter that record was created when database was in UK time zone. Or I'm still doing a mistake somewhere.

Krish
  • 616
  • 9
  • 19

1 Answers1

0

I didn't understand what you wanna get. If you get time as EPOCH time, you don't have information about time zone where this time stamp was made. So, you should know time zone offset and + or minus this seconds from this time stamp.

But I think the best way to use ISO 8601 format for time stamp, it's easy to convert to any timezone what you need

for example, this code convert ISO data to local time or return current time depends on locale timezone

private long time2LocalTimeZone (String date){

        //"2016-07-29T23:07:45.120+00"
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault());

        try {

            return sdf.parse(date).getTime();

        } catch (ParseException e){

            return (new Date()).getTime();

        }
    }

this code count your offset from GMT timezone and convert to epoch time depends on locale timezone

private long time2LocalTimeZone (long date){
        TimeZone tz = TimeZone.getDefault();
        Date now = new Date();
        int offsetFromUtc = tz.getOffset(now.getTime()) / 1000;
        return date + offsetFromUtc;
    } 
koa73
  • 861
  • 2
  • 10
  • 27
  • I need to display same date with time which is stored in SQL server. You can see that in first image. and I want to convert timestamp to date not date to time stamp. – Krish Nov 23 '16 at 10:16
  • Right now i have only one way to display what i need to is to send date in string format through web service – Krish Nov 23 '16 at 10:21
  • Ok, ISO string like this yyyy-MM-dd'T'HH:mm:ss.SSSZ contain all necessary info and database can give you date in ISO format. Or If you have all rows in DB with GMT time zone time stamp you should minus 330 sec from this time bucause android think that it's Epoch time in locale timezone – koa73 Nov 23 '16 at 11:57
  • I've added more example, but it's only good way if your records in DB have constant time stamp in GMT or any other timezone – koa73 Nov 23 '16 at 12:12