Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Date;
import java.util.concurrent.TimeUnit;

public class Main {
    /**
     * Get the number of days between start and end as a string.
     */
    public static String formatDurationInDays(Date start, Date end) {
        if (start != null && end != null) {
            // TODO(emmanuel): maybe there is a way to do this that accounts for current locale?
            long days = getDateDiff(start, end, TimeUnit.DAYS);
            // Add 1 since the trip starts at day zero.
            return (days + 1) + "d";
        }
        return "";
    }

    /**
     * Get a diff between two dates
     *
     * @param date1    the oldest date
     * @param date2    the newest date
     * @param timeUnit the unit in which you want the diff
     * @return the diff value, in the provided unit
     */
    public static long getDateDiff(Date date1, Date date2, TimeUnit timeUnit) {
        long diffInMillis = date2.getTime() - date1.getTime();
        return timeUnit.convert(diffInMillis, TimeUnit.MILLISECONDS);
    }
}