Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.SimpleDateFormat;
- import java.util.Date;
- public class PrettyDateTime {
- static int second = 1000; //milSec
- static int fewSeconds = 5 * second;
- static int aMinute = fewSeconds * 12;
- static int aFewMinute = aMinute * 5;
- static long anHour = aFewMinute * 12;
- static long aDay = anHour * 24;
- static long aWeek = aDay * 7;
- static long anYear = aDay * 365;
- public static String convertToPrettyDateTime(long milSec)
- {
- long diff = System.currentTimeMillis() - milSec;
- System.out.println(diff);
- if (diff < fewSeconds)
- return "Just now";
- else if (diff < aMinute)
- return "A few seconds ago";
- else if (diff <aFewMinute)
- return "A few minutes ago";
- else if (diff < anHour)
- {
- SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss a");
- return format.format(new Date(milSec));
- }
- else if (diff < aDay)
- {
- SimpleDateFormat format = new SimpleDateFormat("hh:mm a");
- return format.format(new Date(milSec));
- }
- else if (diff < aDay * 2)
- {
- SimpleDateFormat format = new SimpleDateFormat("'Yesterday at' hh:mm a");
- return format.format(new Date(milSec));
- }
- else if (diff < aWeek)
- {
- SimpleDateFormat format = new SimpleDateFormat("E 'at' hh:mm a");
- return format.format(new Date(milSec));
- }
- else if (diff < anYear)
- {
- SimpleDateFormat format = new SimpleDateFormat("dd MMM 'at' hh:mm a");
- return format.format(new Date(milSec));
- }
- else
- {
- SimpleDateFormat format = new SimpleDateFormat("dd MMM YYYY 'at' hh:mm a");
- return format.format(new Date(milSec));
- }
- }
- /*public static void main(String[] args) throws InterruptedException {
- long time = System.currentTimeMillis();
- // display time and date using toString()
- System.out.println(convertToPrettyDateTime(time - aDay * 2 - 10));
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement