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;
import java.util.Date;
import java.util.Locale;

public class Main {
    public static String getFormattedDate(long timeStampInMilliSeconds) {

        Calendar smsTime = Calendar.getInstance();
        smsTime.setTimeInMillis(timeStampInMilliSeconds);
        Calendar now = Calendar.getInstance();
        if (now.get(Calendar.DATE) == smsTime.get(Calendar.DATE)) {
            return "Today " + getTime(smsTime.getTimeInMillis());
        } else if (now.get(Calendar.DATE) - smsTime.get(Calendar.DATE) == 1) {
            return "Yesterday " + getTime(smsTime.getTimeInMillis());
        }

        return dateParse(timeStampInMilliSeconds);
    }

    public static String getTime(long date) {
        final String timeFormatString = "h:mm ";
        DateFormat df = new SimpleDateFormat(timeFormatString, Locale.getDefault());
        Date d = new Date(date);
        return df.format(d);
    }

    public static String dateParse(long timeStampInMilliSeconds) {

        DateFormat df = new SimpleDateFormat("dd-MM-yyyy  HH:mm", Locale.getDefault());
        Date d = new Date(timeStampInMilliSeconds);

        return df.format(d);
    }
}