Java Date Between daysBetween(String beginDate, String endDate)

Here you can find the source of daysBetween(String beginDate, String endDate)

Description

days Between

License

Open Source License

Declaration

public static int daysBetween(String beginDate, String endDate) 

Method Source Code

//package com.java2s;
/**/*ww w. ja  v  a  2s . c o  m*/
The MIT License (MIT) * Copyright (c) 2015 ????
    
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
    
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
    
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Main {
    private int year;
    private int month;
    private int day;
    private int hour;
    private int minute;
    private int second;

    public static int daysBetween(String beginDate, String endDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        try {
            cal.setTime(sdf.parse(beginDate));
            cal2.setTime(sdf.parse(endDate));
            long time1 = cal.getTimeInMillis();
            long time2 = cal2.getTimeInMillis();
            long between_days = (time2 - time1) / (1000 * 3600 * 24);
            return (int) between_days;
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return 1;
    }

    public long getTimeInMillis() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(this.year, this.month - 1, this.day, this.hour, this.minute, this.second);
        return calendar.getTime().getTime();
    }
}

Related

  1. daysBetween(Date smdate, Date bdate)
  2. daysBetween(Date startDate, Date endDate)
  3. daysBetween(final Date date1, final Date date2)
  4. daysBetween(final Date dateA, final Date dateB)
  5. daysBetween(String bdate, String edate)
  6. daysBetween(String from, String to)
  7. daysBetween(String from, String to, String form)
  8. daysBetween(String from, String to, String format)
  9. daysBetween(String from, String to, String format)