Java Day Compare compareDay(Date first, Date second)

Here you can find the source of compareDay(Date first, Date second)

Description

Description:Get Monday of the week the day specified by parameter belongs to

License

Open Source License

Parameter

Parameter Description
Date date any date

Return

boolean

Declaration

public static int compareDay(Date first, Date second) 

Method Source Code

//package com.java2s;
/*/*  ww w .  ja  v a 2s. com*/
 * $RCSfile: DatetimeUtil,v $$
 * $Revision: 1.0  $
 * $Date: 2011  $
 *
 * Copyright (C) 2011 GyTech, Inc. All rights reserved.
 *
 * This software is the proprietary information of GyTech, Inc.
 * Use is subject to license terms.
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Calendar;
import java.util.Date;

public class Main {
    /**
     *<p>Description:Get Monday of the week the day specified by parameter belongs to</p>
     * @param Date date any date
     * @return boolean
     */
    public static int compareDay(String timeComparing, String timeCompared, String format) {
        Date timeComparingDate = stringToDate(timeComparing, format);
        Date timeComparedDate = stringToDate(timeComparing, format);

        return compareDay(timeComparingDate, timeComparedDate);
    }

    /**
     *<p>Description:Get Monday of the week the day specified by parameter belongs to</p>
     * @param Date date any date
     * @return boolean
     */
    public static int compareDay(Date first, Date second) {
        Calendar cf = Calendar.getInstance();
        cf.setTime(first);
        Calendar cs = Calendar.getInstance();
        cs.setTime(second);
        int offset = (cf.get(Calendar.YEAR) - cs.get(Calendar.YEAR)) * 12 * 30
                + (cf.get(Calendar.MONTH) - cs.get(Calendar.MONTH)) * 30
                + (cf.get(Calendar.DATE) - cs.get(Calendar.DATE));
        return offset;
    }

    /**
     * <p>Description:Get the date according to specific String content
     *                      The default format is "yyyy-MM-dd"
     * </p>
     * @param String
     * @return Date
     */
    public static Date stringToDate(String datecontent, String format) {
        if (format == null || format.equals(""))
            format = "yyyy-MM-dd";

        try {
            SimpleDateFormat bartDateFormat = new SimpleDateFormat(format);
            Date date = bartDateFormat.parse(datecontent);
            return date;
        } catch (ParseException pe) {
            @SuppressWarnings("unused")
            String message = "Exception occurs in Parse progress.";
        } catch (Exception e) {
            @SuppressWarnings("unused")
            String message = "Exception occurs during the string converting to Date.";
        }
        return null;
    }
}

Related

  1. afterNDay(int n)
  2. afterNDaysDate(String theDate, int nDayNum, String format)
  3. compareDateByDay(Date date1, Date date2)
  4. compareInSameDay(Date date1, Date date2)
  5. isSameDay(Date a, Date b)
  6. isSameDay(Date c1, Date c2)
  7. isSameDay(Date date, Date date2)