Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import org.joda.time.DateTime;
import org.joda.time.Days;

import java.util.Calendar;
import java.util.Date;

public class Main {
    private static final long MS_PER_DAY = 86400000;

    /**
     * Calculates the number of days between Epoch and the given date.
     *
     * @param date the date.
     * @return the number of days between Epoch and the given date.
     */
    public static int daysSince1900(Date date) {
        final Calendar calendar = Calendar.getInstance();

        calendar.clear();
        calendar.set(1900, 0, 1);

        return daysBetween(calendar.getTime(), date);
    }

    /**
     * Calculates the number of days between the start and end-date. Note this
     * method is taking daylight saving time into account and has a performance
     * overhead.
     *
     * @param startDate the start date.
     * @param endDate   the end date.
     * @return the number of days between the start and end date.
     */
    public static int daysBetween(Date startDate, Date endDate) {
        final Days days = Days.daysBetween(new DateTime(startDate), new DateTime(endDate));

        return days.getDays();
    }

    /**
     * Returns the number of days since 01/01/1970. The value is rounded off to
     * the floor value and does not take daylight saving time into account.
     *
     * @param date the date.
     * @return number of days since Epoch.
     */
    public static long getDays(Date date) {
        return date.getTime() / MS_PER_DAY;
    }

    /**
     * Returns the number of days between the start date (inclusive) and end
     * date (exclusive). The value is rounded off to the floor value and does
     * not take daylight saving time into account.
     *
     * @param startDate the start-date.
     * @param endDate   the end-date.
     * @return the number of days between the start and end-date.
     */
    public static long getDays(Date startDate, Date endDate) {
        return (endDate.getTime() - startDate.getTime()) / MS_PER_DAY;
    }
}