简体   繁体   中英

How is this int formatted to represent date and time?

I am studying a method I have found in an application called 'getDateTime'. It returns a date and time as one integer. This is then converted by the app and displayed on a label in 24 hour format.

I am not sure how the method is converting the integer it returns - it will not be possible to see this code.

What I can do is override the returned integer and see what date and time is displayed to try and figure out how it is formatted. I would like to be able to convert any date and time to an int of the same format.

Here are some random overrides I did, and what the corresponding printed result was. I have also recorded when the app crashed to give an idea of when the format may be invalid:

  • 0 - 00 January 2000 0:00
  • 1 - 01 January 2000 0:00
  • 2 - 02 January 2000 0:00
  • 3 - 03 January 2000 0:00
  • 33 - 01 February 2000 0:00
  • 65 - 01 March 2000 0:00
  • 97 - 01 April 2000 0:00
  • 513 - 01 January 2001 0:00
  • 545 - 01 February 2001 0:00
  • 1025 - 01 January 2002 0:00
  • 1057 - 01 February 2002 0:00
  • 2002 - Crashed app
  • 65537 - 01 January 2000 0:01
  • 131073 - 01 January 2000 0:02
  • 4194305 - 01 January 2000 1:00
  • 8388609 - 01 January 2000 2:00
  • 100001 - 01 June 2067 0:01
  • 1000001 - 01 March 2033 0:15
  • 1029601 - Crashed app
  • 1129601 - 01 March 2030 0:17
  • 1838382 - 14 Octoboer 2006 0:28
  • 35000106551 - 23 February 2031 24:43
  • 45000106551 - 23 February 2017 8:55
  • 45000106552 - 24 February 2017 8:55

If you need anymore examples to figure out the format, please ask away! I just can't see any correlation, other than the fact that changing the right most digit can be used to change the current day.

It seems that the components of the date (year, month, day, hour, minute) are packed into fields of specific width (similar to the way dates were stored in a FAT directory entry ).

This method will pack the information into the required format:

public static int packTimeFields(int year, int month, int day, int hour, int minute) {
    checkRange("year", year, 2000, 2127);
    checkRange("month", month, 1, 12);
    checkRange("day", day, 1, 31);
    checkRange("hour", hour, 0, 23);
    checkRange("minute", minute, 0, 59);
    return day + (month-1) * 32 + (year-2000) * 512 + minute * 65536 + hour * 65536 * 64;
}

private static void checkRange(String name, int value, int min, int max) {
    if (value < min || value > max)
        throw new IllegalArgumentException(name+" "+value+" is not in valid range "+min+"..."+max);
}

This code nicely explains the shown examples except

1129601 - 01 March 2030 0:17

I would expect this to be

1129601 - 01 May 2030 0:17

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM