get Day Difference Between Time - Java java.util

Java examples for java.util:Day

Description

get Day Difference Between Time

Demo Code


//package com.java2s;

import java.util.Calendar;

public class Main {
    private static int getDayDifferenceBetweenTime(String start, String end) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.set(getYearFromTime(start), getMonthFromTime(start),
                getDayFromTime(start));/* w  w  w .j  a va  2s  .  co m*/
        c2.set(getYearFromTime(end), getMonthFromTime(end),
                getDayFromTime(end));

        return (int) Math
                .abs(((c1.getTimeInMillis() - c2.getTimeInMillis()) / 24 / 3600000));
    }

    public static int getYearFromTime(String time) {
        return Integer.valueOf(time.substring(0, 4));
    }

    public static int getMonthFromTime(String time) {
        return Integer.valueOf(time.substring(5, 7));
    }

    public static int getDayFromTime(String time) {
        return Integer.valueOf(time.substring(8, 10));
    }
}

Related Tutorials