Skip to content

Instantly share code, notes, and snippets.

@mariuswatz
Created May 29, 2014 22:40
Show Gist options
  • Save mariuswatz/3cc272106d1e6e83e739 to your computer and use it in GitHub Desktop.
Save mariuswatz/3cc272106d1e6e83e739 to your computer and use it in GitHub Desktop.
Convert iOS timestamp to Java Date time (milliseconds)
import java.util.Calendar;
long IOSOFFSET=-1;
// parameter "t" is a iOS timestamp, stored as seconds since Jan 1, 2001.
long iOStoEpochTime(long t) {
// calculate IOSOFFSET if necessary
if (IOSOFFSET<0) {
// Objective-C timestamps are in seconds since Jan 1, 2001, while
// Java timestamps are in milliseconds since Jan 1, 1970. Hence we
// need to calculate a timestamp IOSOFFSET (in msec) between those two
// dates.
Calendar cal=Calendar.getInstance();
cal.clear();
// get start value
cal.set(1970, Calendar.JANUARY, 1);
long tmp=cal.getTimeInMillis();
// get final value
cal.set(2001, Calendar.JANUARY, 1);
// offset is the difference
IOSOFFSET=cal.getTimeInMillis()-tmp;
}
// t is in seconds, so multiply x1000 to get millis,
// then add IOSOFFSET to get. "1000l" means "1000 as
// a long"
return t*1000l+IOSOFFSET;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment