Java examples for java.util:Month
Check if the two dates are equals comparing only those fields that are given in the array. Example: Date a = new Date(); Date b = new Date(); int[] fields = { Calendar.MONTH, Calendar.DAY_OF_MONTH }; boolean equality = equals(fields, a, b);
//package com.java2s; import java.util.Calendar; import java.util.Date; public class Main { public static void main(String[] argv) throws Exception { int[] fields = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 }; Date dateA = new Date(); Date dateB = new Date(); System.out.println(equals(fields, dateA, dateB)); }//from w ww . j av a2 s . co m /** * Check if the two dates are equals comparing only those fields that are * given in the array.<br/> * Example:<br/> * <code>Date a = new Date();</code><br/> * <code>Date b = new Date();</code><br/> * <code>int[] fields = { Calendar.MONTH, Calendar.DAY_OF_MONTH };</code><br/> * <code>boolean equality = equals(fields, a, b);</code><br/> * * @param fields * the fields to compare (Constants from Calendar) * @param dateA * the first date to be compared with the second * @param dateB * the second date to be compared with the first * @return <code>true</code> if they equal, <code>false</code> if not. */ public static boolean equals(int[] fields, Date dateA, Date dateB) { Calendar calendar = Calendar.getInstance(); // if both are null return true if (dateA == null && dateB == null) { return true; } // if either one is null then return false if (dateA == null || dateB == null) { return false; } boolean b = true; int[] values = new int[fields.length]; calendar.setTime(dateA); for (int i = 0; i < fields.length; i++) { values[i] = calendar.get(fields[i]); } calendar.setTime(dateB); for (int i = 0; i < fields.length; i++) { int x = calendar.get(fields[i]); if (values[i] != x) { b = false; break; } } return b; } }