Java Day Between calculateNumberOfDays(String dateStr)

Here you can find the source of calculateNumberOfDays(String dateStr)

Description

calculate Number Of Days

License

Open Source License

Declaration

public static int calculateNumberOfDays(String dateStr) 

Method Source Code

//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;
        }
    }
}

Related

  1. calculateDayDifference(final Date a, final Date b)
  2. calculateDays(Date beginDate, Date endDate)
  3. calculateDays(Date dateEarly, Date dateLater)
  4. calculateDifference(Date d1, Date d2)
  5. calculateDifferMonths(Date date1, Date date2, boolean isTruncate)
  6. countDiffDay(String beginDateBase, String endDateBase)
  7. dayDiff(Date d1, Date d2)
  8. dayDiff(Date start, Date end)
  9. daysDiff(final Date data1, final Date data2)