Java Date Difference dateDiff(Date fromDate, Date toDate)

Here you can find the source of dateDiff(Date fromDate, Date toDate)

Description

date Diff

License

Apache License

Declaration

public static long dateDiff(Date fromDate, Date toDate) 

Method Source Code

//package com.java2s;
/*//from  w ww .  java2 s.  c  o  m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;

public class Main {
    public static final String DATE_PATTERN = "yyyy-MM-dd";
    public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static long dateDiff(Date fromDate, Date toDate) {
        return dateDiff(getDateTime(DATE_PATTERN, fromDate), getDateTime(DATE_PATTERN, toDate));
    }

    public static long dateDiff(String beginDateStr, String endDateStr) {
        long day = 0;
        java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
        java.util.Date beginDate;
        java.util.Date endDate;
        try {
            beginDate = format.parse(beginDateStr);
            endDate = format.parse(endDateStr);
            day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        return day;
    }

    public static String getDateTime(java.util.Date date) {
        return getDateTime(DATE_TIME_PATTERN, date);
    }

    public static String getDateTime(String pattern, java.util.Date date) {
        if (date == null) {
            return "";
        }
        if (pattern == null) {
            pattern = DATE_TIME_PATTERN;
        }
        SimpleDateFormat formatter = new SimpleDateFormat(pattern, Locale.getDefault());
        String ret = formatter.format(date);
        return ret;
    }
}

Related

  1. dateDiff(Date beginDate, Date endDate)
  2. dateDiff(Date d1, Date d2, int field)
  3. dateDiff(Date date1, Date date2)
  4. dateDiff(Date date1, Date date2)
  5. dateDiff(Date date1, Date date2)
  6. DateDiff(Date startDate, Date endDate)
  7. dateDiff(final Date date1, final Date date2)
  8. dateDiff(int category, Date date1, Date date2)
  9. dateDiff(int interval, Date begin, Date end)