Here you can find the source of getDateDifference(String startDateString, String endDateString)
public static int getDateDifference(String startDateString, String endDateString)
//package com.java2s; /*// ww w.j av a 2s . c om * Copyright (c) 2013 The Johns Hopkins University/Applied Physics Laboratory * All rights reserved. * * This material may be used, modified, or reproduced by or for the U.S. * Government pursuant to the rights granted under the clauses at * DFARS 252.227-7013/7014 or FAR 52.227-14. * * Licensed 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 * * NO WARRANTY. THIS MATERIAL IS PROVIDED "AS IS." JHU/APL DISCLAIMS ALL * WARRANTIES IN THE MATERIAL, WHETHER EXPRESS OR IMPLIED, INCLUDING (BUT NOT * LIMITED TO) ANY AND ALL IMPLIED WARRANTIES OF PERFORMANCE, * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT OF * INTELLECTUAL PROPERTY RIGHTS. ANY USER OF THE MATERIAL ASSUMES THE ENTIRE * RISK AND LIABILITY FOR USING THE MATERIAL. IN NO EVENT SHALL JHU/APL BE * LIABLE TO ANY USER OF THE MATERIAL FOR ANY ACTUAL, INDIRECT, * CONSEQUENTIAL, SPECIAL OR OTHER DAMAGES ARISING FROM THE USE OF, OR * INABILITY TO USE, THE MATERIAL, INCLUDING, BUT NOT LIMITED TO, ANY DAMAGES * FOR LOST PROFITS. */ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.GregorianCalendar; public class Main { public static int getDateDifference(String startDateString, String endDateString) { int returnVal = 0; if ((startDateString != null) && (endDateString != null)) { java.util.Date startDate = getDate(startDateString); java.util.Date endDate = getDate(endDateString); returnVal = getDateDifference(startDate, endDate); } return returnVal; } public static int getDateDifference(java.util.Date startDate, java.util.Date endDate) { int returnVal = 0; Calendar cal = Calendar.getInstance(); cal.setTime(startDate); cal.set(Calendar.HOUR, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); startDate = cal.getTime(); cal.setTime(endDate); cal.set(Calendar.HOUR, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); endDate = cal.getTime(); int diff = (int) Math.round((endDate.getTime() - startDate.getTime()) / ((double) (1000 * 60 * 60 * 24))); returnVal = diff + 1; return returnVal; } public static java.util.Date getDate(String dateString) { java.util.Date returnDate = null; SimpleDateFormat sdf = getStandardDateFormat(); if (dateString != null) { try { returnDate = sdf.parse(dateString); } catch (ParseException pe) { System.err.println("ERROR - [DateHelper.getDate(String)] - " + pe.getMessage()); pe.printStackTrace(); } } return returnDate; } /** * Returns Date object for specified number of days ago, relative to today. Allow the ability to pass in what the * current date is. * * @return Date */ public static java.util.Date getDate(int numDaysAgo) { //BioSrvConfig config = BioSrvConfig.getInstance(); String currentDateArg = null; java.util.Date returnDate = null; Calendar cal = new GregorianCalendar(); //check currentDate Arge in biosrvconfig //currentDateArg = config.getCurrentDateArg(); if (currentDateArg == null //|| currentDateArg.equals(BioSrvConfig.today_currentDateArgValue)) { ) { cal.setTime(new java.util.Date()); } else { cal.setTime(getDate(currentDateArg)); // System.err.println("Today is " + getDate(currentDateArg)); } cal.add(Calendar.DATE, 0 - numDaysAgo); returnDate = cal.getTime(); return returnDate; } /** * Returns Date object for specified number of days ago, relative to specified Date object. */ public static java.util.Date getDate(java.util.Date fromDate, int numDaysAgo) { java.util.Date returnDate; Calendar cal = new GregorianCalendar(); cal.setTime(fromDate); cal.add(Calendar.DATE, 0 - numDaysAgo); returnDate = cal.getTime(); return returnDate; } /** * Returns String representing date a specified number of days ago, relative to supplied date String. */ public static String getDate(String fromDateString, int numDaysAgo) { String returnDateString; java.util.Date fromDate = getDate(fromDateString); Calendar cal = new GregorianCalendar(); cal.setTime(fromDate); cal.add(Calendar.DATE, 0 - numDaysAgo); java.util.Date returnDate = cal.getTime(); returnDateString = getFormattedDateString(returnDate); return returnDateString; } public static java.util.Date getDate(String day, String month, String year) { java.util.Date returnDate = null; Calendar calendar = new GregorianCalendar(); try { calendar.set(Calendar.YEAR, Integer.parseInt(year)); calendar.set(Calendar.MONTH, Integer.parseInt(month)); calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day)); returnDate = calendar.getTime(); } catch (Exception e) { System.out.println("DateHelper.getDate(int, int, int): Illegal date"); } return returnDate; } public static SimpleDateFormat getStandardDateFormat() { // cas 07-13-03 //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //SimpleDateFormat sdf = new SimpleDateFormat("MMddyy"); SimpleDateFormat sdf = new SimpleDateFormat("ddMMMyy"); return sdf; } public static String getFormattedDateString(java.util.Date date) { String returnString = ""; SimpleDateFormat sdf = getStandardDateFormat(); returnString = sdf.format(date); return returnString; } public static String getFormattedDateString(int numDaysAgo) { String returnString = ""; java.util.Date returnDate = getDate(numDaysAgo); SimpleDateFormat sdf = getStandardDateFormat(); returnString = sdf.format(returnDate); return returnString; } }