Search code examples
androidcalendarepoch

Epoch and Date Time with Picker is completely off


I have a DatePicker and a TimePicker. I am using both of them together to set a date in epoch format to my backend. But when I convert the calculated epoch its completely off.

In my onCreate

    whenToTravel = today = Calendar.getInstance();
    travelDate = (LinearLayout) findViewById(R.id.travelDate);
    travelDate.setOnClickListener(this);
    datePicker = new DatePickerDialog(this, this, today.get(Calendar
            .YEAR), today.get(Calendar.MONTH), today.get(Calendar
            .DAY_OF_MONTH));
    selectedDate = (TextView) findViewById(R.id.selectedDate);
    travelTime = (LinearLayout) findViewById(R.id.travelTime);
    travelTime.setOnClickListener(this);
    timePicker = new TimePickerDialog(this, this, today.get(Calendar
            .HOUR_OF_DAY), today.get(Calendar.MINUTE), false);
    selectedTime = (TextView) findViewById(R.id.selectedTime);

In my onDateSet and onTimeSet

@Override
public void onDateSet(DatePicker datePicker, int year, int monthOfYear,
                      int dayOfMonth) {
    whenToTravel.set(Calendar.YEAR, year);
    whenToTravel.set(Calendar.MONTH, monthOfYear);
    whenToTravel.set(Calendar.DAY_OF_MONTH, dayOfMonth);
    selectedDate.setText(DATE_FORMAT.format(whenToTravel.getTime()));
    //monthOfYear   int: The month that was set (0-11) for compatibility
    //with Calendar.
}

@Override
public void onTimeSet(TimePicker timePicker, int hour, int minute) {
    whenToTravel.set(Calendar.HOUR_OF_DAY, hour);
    whenToTravel.set(Calendar.MINUTE, minute);
    selectedTime.setText(TIME_FORMAT.format(whenToTravel.getTime()));
}

Now when I set 31-May-2016 10:01 AM I get 1464710433243 as the epoch. On using http://www.epochconverter.com/. I get

Assuming that this timestamp is in milliseconds: GMT: Tue, 31 May 2016 16:00:33.243 GMT Your time zone: 31/05/2016, 21:30:33 GMT+5:30

I was assuming that my time zone will return 31/05/2016, 10:01:33 GMT+5:30

What am I missing here ?


Solution

  • I think got this working, I made this one change and the epoch now reads

    Assuming that this timestamp is in milliseconds: GMT: Wed, 01 Jun 2016 04:31:23.256 GMT Your time zone: 01/06/2016, 10:01:23 GMT+5:30

    whenToTravel = today = Calendar.getInstance() ;
    today.setTime(new Date(System.currentTimeMillis())) ;