Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.Calendar;

public class Main {
    public static String findDateDifference(Long timestamp) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm a");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm a");
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        calendar1.setTimeInMillis(timestamp);
        calendar2.setTimeInMillis(System.currentTimeMillis());
        try {

            //in milliseconds
            long diff = calendar2.getTimeInMillis() - calendar1.getTimeInMillis();

            long diffSeconds = diff / 1000 % 60;
            long diffMinutes = diff / (60 * 1000) % 60;
            long diffHours = diff / (60 * 60 * 1000) % 24;
            long diffDays = diff / (24 * 60 * 60 * 1000);
            long diffYears = diffDays / 365;
            //            System.out.print(diffDays + " days, ");
            //            System.out.print(diffHours + " hours, ");
            //            System.out.print(diffMinutes + " minutes, ");
            //            System.out.print(diffSeconds + " seconds.");
            //            Log.e("difference is", diffDays + " days, " + diffHours + " hours, " + diffMinutes + " minutes, ");
            if (diffDays == 0) {
                if (diffHours > 0)
                    return diffHours + "HOURS AGO";
                else if (diffMinutes > 0) {
                    return diffMinutes + " MINS AGO";
                    //return timeFormat.format(calendar1.getTime());
                } else
                    return "Just Now";
            } else if (diffDays == 1) {
                return "Yesterday";
            } else if (diffDays <= 30) {
                return "" + diffDays + " DAYS AGO";
            } //else
              //return format.format(calendar1.getTime());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}