Advertisement
onix_hoque

PrettyDateTime class

May 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.84 KB | None | 0 0
  1. import java.text.SimpleDateFormat;
  2. import java.util.Date;
  3.  
  4. public class PrettyDateTime {
  5.     static int second = 1000;   //milSec
  6.     static int fewSeconds = 5 * second;
  7.     static int aMinute = fewSeconds * 12;
  8.     static int aFewMinute = aMinute * 5;
  9.     static long anHour = aFewMinute * 12;
  10.     static long aDay = anHour * 24;
  11.     static long aWeek = aDay * 7;
  12.     static long anYear = aDay * 365;
  13.    
  14.     public static String convertToPrettyDateTime(long milSec)
  15.     {
  16.         long diff = System.currentTimeMillis() - milSec;
  17.         System.out.println(diff);
  18.         if (diff < fewSeconds)
  19.             return "Just now";
  20.         else if (diff < aMinute)
  21.             return "A few seconds ago";
  22.         else if (diff <aFewMinute)
  23.             return "A few minutes ago";
  24.         else if (diff < anHour)
  25.         {
  26.             SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss a");
  27.             return format.format(new Date(milSec));
  28.         }
  29.         else if (diff < aDay)
  30.         {
  31.             SimpleDateFormat format = new SimpleDateFormat("hh:mm a");
  32.             return format.format(new Date(milSec));
  33.         }
  34.         else if (diff < aDay * 2)
  35.         {
  36.             SimpleDateFormat format = new SimpleDateFormat("'Yesterday at' hh:mm a");
  37.             return format.format(new Date(milSec));
  38.         }
  39.         else if (diff < aWeek)
  40.         {
  41.             SimpleDateFormat format = new SimpleDateFormat("E 'at' hh:mm a");
  42.             return format.format(new Date(milSec));
  43.         }
  44.         else if (diff < anYear)
  45.         {
  46.             SimpleDateFormat format = new SimpleDateFormat("dd MMM 'at' hh:mm a");
  47.             return format.format(new Date(milSec));
  48.         }
  49.         else
  50.         {
  51.             SimpleDateFormat format = new SimpleDateFormat("dd MMM YYYY 'at' hh:mm a");
  52.             return format.format(new Date(milSec));
  53.         }
  54.     }
  55.     /*public static void main(String[] args) throws InterruptedException {
  56.        
  57.         long time = System.currentTimeMillis();
  58.           // display time and date using toString()
  59.       System.out.println(convertToPrettyDateTime(time - aDay  * 2 - 10));
  60.      
  61.     }*/
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement