how to get a java date in "packed byte" format?

569 Views Asked by At

I'm trying to manually enter a forum user into an smf database but can't seem to figure out how to get the current date in the proper format, which I found out is something called "packed byte".

can anyone point me to some info to help out?

2

There are 2 best solutions below

0
On

According IBM's SFM Wiki the 'byte packed date format' is defined as follows:

0x[0C][YY][DDD][+]
     where:
       C = centuries since 1900 (e.g. 1 for the 21st century)
      YY = year
     DDD = day (1 for Jan. 1 366 for Dec. 31)
       + = 0xC (Hardcoded)

Example: Aug. 31. 2014 = 0x01 14 244 C

In Java you could use the java.util.Calendar to create a hex string containing all required values and use Long.valueOf(...,16) to get a number out of it.

0
On

If I interpreted the spec you linked in comments correctly - you can use a combination of String formatting and parsing to get what you need. I chose to use String formatting because although the output expected is a base-16 number, it appears to encode values as base-10 values within the base-16 number.

Calendar toPack = Calendar.getInstance(); 

int century = (toPack.get(Calendar.YEAR) - 1900) / 100; 
int year = toPack.get(Calendar.YEAR) % 100; 
int dayOfYear = toPack.get(Calendar.DAY_OF_YEAR); 

String packedDate = String.format("%02d%02d%03dC", century, year, dayOfYear);
int packed = Integer.parseInt(packedDate, 16); 

System.out.printf("0x%x%n", packed);

Output:

0x114238c