Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

import java.util.concurrent.TimeUnit;
import org.apache.http.impl.cookie.DateUtils;

import android.util.Log;

public class Main {
    private static final String TAG = "WareNinjaUtils";

    public static String getDaysBetween(String date1, String date2) {
        // input is expected to be exactly like; 2011-01-05
        // date2 must be before date1
        String result = "";

        try {

            Date dateOne = DateUtils.parseDate(date1, new String[] { "yyyy-MM-dd" });
            Calendar cal1 = Calendar.getInstance();
            cal1.setTime(dateOne);

            Date dateTwo = DateUtils.parseDate(date2, new String[] { "yyyy-MM-dd" });
            Calendar cal2 = Calendar.getInstance();
            cal2.setTime(dateTwo);

            long diff = dateOne.getTime() - dateTwo.getTime();
            Log.d(TAG, "days in between:" + (TimeUnit.MILLISECONDS.toSeconds(diff) / 60 / 60 / 24));
        } catch (Exception ex) {
            Log.w(TAG, ex.toString());
        }

        return result;
    }
}