Here you can find the source of calculateNumberOfDays(String dateStr)
public static int calculateNumberOfDays(String dateStr)
//package com.java2s; /**/*w w w . j ava 2 s .c o m*/ * ============================================================================== Copyright (c) 2015 by www.tencent.com, * All rights reserved. ============================================================================== This software is * the confidential and proprietary information of tencent.com, Inc. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in accordance with the terms of the license agreement * you entered into with tencent.com, Inc. * ------------------------------------------------------------------------------ * <p/> * Author: faberxu Date: 2015/12/18 Description: Nothing. Function List: 1. Nothing. History: 1. Nothing. * ============================================================================== */ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static final String DEFAULT_FORMAT = "yyyy-MM-dd"; public static int calculateNumberOfDays(String dateStr) { SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_FORMAT); try { Date date = sdf.parse(dateStr); Date now = new Date(); long n1 = now.getTime(); long n2 = date.getTime(); if (n1 > n2) { long diffTime = n1 - n2; int numberOfDays = (int) (diffTime / (3600 * 1000 * 24)); return numberOfDays; } else { return 0; } } catch (ParseException e) { return 0; } } }