DateUtil.java Source code

Java tutorial

Introduction

Here is the source code for DateUtil.java

Source

//package com.ctrcv.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
    static SimpleDateFormat dateFormater = new SimpleDateFormat("MM/dd/yyyy");

    public static String getTimeDistance(Date from) {
        long minDist = (System.currentTimeMillis() - from.getTime()) / 60000;
        if (minDist <= 0) {
            return (System.currentTimeMillis() - from.getTime()) / 1000 + " seconds";
        } else if (minDist >= 60) {
            return minDist / 60 + " hours";
        } else {
            return minDist + " minutes";
        }
    }

    public static Date fromDateStr(String date) {
        try {
            return dateFormater.parse(date);
        } catch (Exception e) {
            return null;
        }
    }
}