Skip to content
Advertisement

How to convert date time in hex in android

I need to convert date and time in hex code for writing it on IOT device. Here is my code

private String getDateTimeToHexa() { 

Calendar mCalendar = Calendar.getInstance();
TimeZone gmtTime =  TimeZone.getTimeZone(TimeZone.getDefault().getDisplayName());

    mCalendar.setTimeZone(gmtTime);
    final Date date = mCalendar.getTime();
    return Long.toHexString(date.getTime());
}

It is returning 11 digits hex code I need 8 digits, just like date and time in hex is 47C7EDE0 for this date 12:34:56 29/Feb/2008

Please help

Advertisement

Answer

Try this:

 private String getDateTimeToHexa() {
        Calendar mCalendar = Calendar.getInstance();
        TimeZone gmtTime =  TimeZone.getTimeZone(TimeZone.getDefault().getDisplayName());
        mCalendar.setTimeZone(gmtTime);
        final Date date = mCalendar.getTime();
        return Long.toHexString(date.getTime()/1000);
    }
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement