public Time convertToTime ( long millis , TimeZone tz ) { if ( tz == null ) { tz = getDefaultTz ( ) ; } if ( isSimpleTimeZone ( tz . getID ( ) ) ) { // Leave just time part of the day. // Suppose the input date is 2015 7 Jan 15:40 GMT+02:00 (that is 13:40 UTC) // We want it to become 1970 1 Jan 15:40 GMT+02:00 // 1) Make sure millis becomes 15:40 in UTC, so add offset int offset = tz . getRawOffset ( ) ; millis += offset ; // 2) Truncate year, month, day. Day is always 86400 seconds, no matter what leap seconds are millis = millis % ONEDAY ; // 2) Now millis is 1970 1 Jan 15:40 UTC, however we need that in GMT+02:00, so subtract some // offset millis -= offset ; // Now we have brand-new 1970 1 Jan 15:40 GMT+02:00 return new Time ( millis ) ; } Calendar cal = calendarWithUserTz ; cal . setTimeZone ( tz ) ; cal . setTimeInMillis ( millis ) ; cal . set ( Calendar . ERA , GregorianCalendar . AD ) ; cal . set ( Calendar . YEAR , 1970 ) ; cal . set ( Calendar . MONTH , 0 ) ; cal . set ( Calendar . DAY_OF_MONTH , 1 ) ; return new Time ( cal . getTimeInMillis ( ) ) ; }
Extracts the time part from a timestamp . This method ensures the date part of output timestamp looks like 1970 - 01 - 01 in given timezone .