Here you can find the source of datesEqualToSecond(Date date1, Date date2)
Parameter | Description |
---|---|
date1 | first date |
date2 | second date |
true
if the dates equal to the second, false
otherwise
public static boolean datesEqualToSecond(Date date1, Date date2)
//package com.java2s; /*/*from w w w . j a v a2 s .com*/ * Copyright 2006-2008 Appcelerator, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.TimeZone; public class Main { /** * Greenwich mean time timezone (GMT). */ public static final TimeZone GMT_TZ = TimeZone.getTimeZone("GMT"); /** * compares two datess down to the second * * @param date1 first date * @param date2 second date * @return <code>true</code> if the dates equal to the second, <code>false</code> otherwise */ public static boolean datesEqualToSecond(Date date1, Date date2) { Calendar calendar1 = new GregorianCalendar(GMT_TZ); calendar1.setTime(date1); Calendar calendar2 = new GregorianCalendar(GMT_TZ); calendar2.setTime(date2); if (calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) { if (calendar1.get(Calendar.DAY_OF_YEAR) == calendar2.get(Calendar.DAY_OF_YEAR)) { if (calendar1.get(Calendar.HOUR) == calendar2.get(Calendar.HOUR)) { if (calendar1.get(Calendar.MINUTE) == calendar2.get(Calendar.MINUTE)) { if (calendar1.get(Calendar.SECOND) == calendar2.get(Calendar.SECOND)) { return true; } } } } } return false; } }