Here you can find the source of getDayCount(String from, String to)
public static int getDayCount(String from, String to) throws ParseException
//package com.java2s; /*// www .ja va 2s.c o m * M2M ServiceFOTA ONM version 1.0 * * Copyright ? 2014 kt corp. All rights reserved. * * This is a proprietary software of kt corp, and you may not use this file except in * compliance with license agreement with kt corp. Any redistribution or use of this * software, with or without modification shall be strictly prohibited without prior written * approval of kt corp, and the copyright notice above does not evidence any actual or * intended publication of such software. */ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class Main { public static int getDayCount(String from, String to) throws ParseException { return getDayCountWithFormatter(from, to, "yyyyMMdd"); } public static int getDayCountWithFormatter(String from, String to, String format) throws ParseException { long duration = getTimeCount(from, to, format); return (int) (duration / (1000 * 60 * 60 * 24)); } public static long getTimeCount(String from, String to) throws ParseException { String format = getFormatStringWithDate(from); return getTimeCount(from, to, format); } public static long getTimeCount(String from, String to, String format) throws ParseException { Date d1 = dateFormatCheck(from, format); Date d2 = dateFormatCheck(to, format); long duration = d2.getTime() - d1.getTime(); return duration; } protected static String getFormatStringWithDate(String date) throws ParseException { String format = null; if (date.length() == 4) { format = "HHmm"; } else if (date.length() == 8) { format = "yyyyMMdd"; } else if (date.length() == 12) { format = "yyyyMMddHHmm"; } else if (date.length() == 14) { format = "yyyyMMddHHmmss"; } else if (date.length() == 17) { format = "yyyyMMddHHmmssSSS"; } else if (date.length() == 19) { format = "yyyy/MM/dd HH:mm:ss"; } else { throw new ParseException(" wrong date format!:\"" + format + "\"", 0); } return format; } public static Date dateFormatCheck(String source) throws ParseException { return dateFormatCheck(source, "yyyyMMdd"); } public static Date dateFormatCheck(String source, String format) throws ParseException { if (source == null) { throw new ParseException("date string to check is null", 0); } if (format == null) { throw new ParseException("format string to check date is null", 0); } SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.KOREA); Date date = null; try { date = formatter.parse(source); } catch (ParseException e) { throw new ParseException(" wrong date:\"" + source + "\" with format \"" + format + "\"", 0); } if (!formatter.format(date).equals(source)) { throw new ParseException("Out of bound date:\"" + source + "\" with format \"" + format + "\"", 0); } return date; } }