Conversion of Java Date to PT_SYSTIME binary array

How can I create a PT_SYSTIME byte array from a java Date object so that I can build a MapiProperty.

Basically I need to do this:

Date date = new Date();
byte[] dateArray = new byte[8];

…somehow convert date --> dateArray…

MapiProperty prop = new MapiProperty(MapiProperty.PR_DELIVER_TIME, dateArray);

I am stuck using an older version of your API so I don’t have the ability to call “createMapiPropertyFromDateTime(long tag, Date data)”. So I have to use instead the older MapiProperty constructor with the byte array.

Thanks,
//C

@cviaggi,

Please try the following method at our end:

 private static byte[] convertDateTime(Date t)    
{
        long filetime = millisToFiletime(t.getTime());        
	byte[] d = new byte[8];        
	d[0] = (byte)(filetime & 0xFF);        
	d[1] = (byte)((filetime & 0xFF00) >> 8);        
	d[2] = (byte)((filetime & 0xFF0000) >> 16);        
	d[3] = (byte)((filetime & 0xFF000000) >> 24);        
	d[4] = (byte)((filetime & 0xFF00000000L) >> 32);        
	d[5] = (byte)((filetime & 0xFF0000000000L) >> 40);        
	d[6] = (byte)((filetime & 0xFF000000000000L) >> 48);        
	d[7] = (byte)((filetime & 0xFF00000000000000L) >> 56);        
	return d;    
}

Thanks, I tried that but the resulting data did not seem to set the appropriate date correctly.

I tried this and it seems to work, but it’s a huge workaround and gives different result than what your code does. I would assume it should do the same thing?

	Date date2set = new Date(timestamp);
	
	//Create an empty mapi message and set the client submit date
	MapiMessage msg = new MapiMessage();
	msg.setClientSubmitTime(date2set);
	
	//After setting the mapi-property, retrieve the raw data
	byte[] data = msg.getPropertyBytes(MapiPropertyTag.PR_CLIENT_SUBMIT_TIME);
	
	//Now return the data
	return data;

@cviaggi,

Following is the usage of this method that should meet your requirements.

private static final long FILETIME_EPOCH_DIFF = 11644473600000L;

// One millisecond expressed in units of 100s of nanoseconds.
private static final long FILETIME_ONE_MILLISECOND = 10 * 1000;


 public static long filetimeToMillis(final long filetime) {
        return (filetime / FILETIME_ONE_MILLISECOND) - FILETIME_EPOCH_DIFF;
    }
 
 public static long millisToFiletime(final long millis) {
        return (millis + FILETIME_EPOCH_DIFF) * FILETIME_ONE_MILLISECOND;
    }
    
public static Date getDate(int year, int month, int date) {
    java.util.Calendar c = java.util.Calendar.getInstance();
    c.set(java.util.Calendar.YEAR, year);
    // Month value is 0-based. e.g., 0 for January.
    c.set(java.util.Calendar.MONTH, month - 1);
    c.set(java.util.Calendar.DAY_OF_MONTH, date);

    c.set(java.util.Calendar.MILLISECOND, 0);
    c.set(java.util.Calendar.SECOND, 0);
    c.set(java.util.Calendar.MINUTE, 0);
    c.set(java.util.Calendar.HOUR_OF_DAY, 0);
    return c.getTime(); 
}
    
private static byte[] convertDateTime(Date t)
{
    long filetime = millisToFiletime(t.getTime());
    byte[] d = new byte[8];
    d[0] = (byte)(filetime & 0xFF);
    d[1] = (byte)((filetime & 0xFF00) >> 8);
    d[2] = (byte)((filetime & 0xFF0000) >> 16);
    d[3] = (byte)((filetime & 0xFF000000) >> 24);
    d[4] = (byte)((filetime & 0xFF00000000L) >> 32);
    d[5] = (byte)((filetime & 0xFF0000000000L) >> 40);
    d[6] = (byte)((filetime & 0xFF000000000000L) >> 48);
    d[7] = (byte)((filetime & 0xFF00000000000000L) >> 56);
    return d;
} 

Usage

MapiProperty property = new MapiProperty(MapiPropertyTag.PR_LAST_MODIFICATION_TIME, 
	            convertDateTime(getDate(2012, 4, 11)));