public static String fancyTimestamp ( long epoch ) { // First, check to see if it's within 1 minute of the current date if ( System . currentTimeMillis ( ) - epoch < 60000 ) { return "Just now" ; } // Get calendar for just now Calendar now = Calendar . getInstance ( ) ; // Generate Calendar for this time Calendar cal = Calendar . getInstance ( ) ; cal . setTimeInMillis ( epoch ) ; // Based on the date, determine what to print out // 1) Determine if time is the same day if ( cal . get ( YEAR ) == now . get ( YEAR ) ) { if ( cal . get ( MONTH ) == now . get ( MONTH ) ) { if ( cal . get ( DAY_OF_MONTH ) == now . get ( DAY_OF_MONTH ) ) { // Return just the time SimpleDateFormat format = new SimpleDateFormat ( "h:mm a" ) ; return format . format ( cal . getTime ( ) ) ; } else { // Return the day and time SimpleDateFormat format = new SimpleDateFormat ( "EEE, h:mm a" ) ; return format . format ( cal . getTime ( ) ) ; } } else { SimpleDateFormat format = new SimpleDateFormat ( "EEE, MMM d, h:mm a" ) ; return format . format ( cal . getTime ( ) ) ; } } else { SimpleDateFormat format = new SimpleDateFormat ( "M/d/yy" ) ; return format . format ( cal . getTime ( ) ) ; } }
Generate a fancy timestamp based on unix epoch time that is more user friendly than just a raw output by collapsing the time into manageable formats based on how much time has elapsed since epoch