Java Date Difference diff_in_date(Date d1, Date d2, String type)

Here you can find the source of diff_in_date(Date d1, Date d2, String type)

Description

difidate

License

Open Source License

Declaration

public static int diff_in_date(Date d1, Date d2, String type) 

Method Source Code

//package com.java2s;
/*//from  w ww.j a  v  a  2 s  . c o m
 *  DateUtils.java
 *  
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *  
 *  Author: Winter Lau (javayou@gmail.com)
 *  http://dlog4j.sourceforge.net
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {

    public static int diff_in_date(Date d1, Date d2, String type) {
        if (type.endsWith("day"))
            return (int) (d1.getTime() - d2.getTime()) / 86400000;
        else if (type.endsWith("hour"))
            return (int) (d1.getTime() - d2.getTime()) / 3600000;
        else if (type.endsWith("min"))
            return (int) (d1.getTime() - d2.getTime()) / 60000;
        else
            return (int) (d1.getTime() - d2.getTime()) / 86400000;
    }

    public static int diff_in_date(Date d1, Date d2) {
        d1 = toDate("yyyy-MM-dd", new SimpleDateFormat("yyyy-MM-dd").format(d1));
        d2 = toDate("yyyy-MM-dd", new SimpleDateFormat("yyyy-MM-dd").format(d2));
        return (int) ((d1.getTime() - d2.getTime()) / (1000 * 60 * 60 * 24));
        //return (int)((d2.getTime()-d1.getTime())/(1000*60*60*24));
        //return (int)(d1.getTime() - d2.getTime())/86400000;
    }

    public static Date toDate(String pattern, String date) {
        try {
            return new SimpleDateFormat(pattern).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String format(String format, Date d) {
        if (d == null) {
            return "";
        }
        return new java.text.SimpleDateFormat(format).format(d);
    }

    public static Date parse(String format, String d) {
        try {
            return new java.text.SimpleDateFormat(format).parse(d);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. diff(Date date1, Date date2)
  2. diff(Date endDate, Date startDate)
  3. diff(Date sDate, Date fDate)
  4. diff(Date subtrahend, Date minuend, long diffField)
  5. diff(int type, Date date1, Date date2)
  6. diffBetweenMonth(Date d1, Date d2)
  7. diffCommercial(Date dateUntil, Date dateFrom, boolean bAddDay)
  8. diffDate(Date date, Date date1)
  9. diffDate(java.util.Date date, java.util.Date date1)