Here you can find the source of dateDiff(Date fromDate, Date toDate)
public static long dateDiff(Date fromDate, Date toDate)
//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; } }