Search code examples
android.netdatetimeandroid-date

Transfer DateTime to .NET WebAPI


I would like simply get Android current time and pass to .NET WebAPI. That is it. But there are many approaches...

I tried this one

Android side

String TimeOfRequest =  Long.toString(new Date().getTime());

.NET side

var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
t = epoch.AddMilliseconds(Convert.ToInt64(locationItem.TimeOfRequest));

But it gives this result

0001-01-02 18:01:34.0366112

Please suggest about simplest approach to do it.


Solution

  • Try this:

    private String formatDate(long milliseconds) /* This is your topStory.getTime()*1000 */ {
        DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy' 'HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(milliseconds);
        TimeZone tz = TimeZone.getDefault();
        sdf.setTimeZone(tz);
        return sdf.format(calendar.getTime());
    }