diff --git a/src/main/java/org/codetracker/FileTrackerImpl.java b/src/main/java/org/codetracker/FileTrackerImpl.java index 727b1527f7b..2a95a086655 100644 --- a/src/main/java/org/codetracker/FileTrackerImpl.java +++ b/src/main/java/org/codetracker/FileTrackerImpl.java @@ -269,10 +269,17 @@ public void blame() throws Exception { boolean annotationChanged = false; if(leftClass == null) { leftClass = getClass(leftModel, parentVersion, rightClass::equalIdentifierIgnoringVersionAndAnnotation); - annotationChanged = true; + if(leftClass != null) + annotationChanged = true; + } + boolean modifiersChanged = false; + if(leftClass == null) { + leftClass = getClass(leftModel, parentVersion, rightClass::equalIdentifierIgnoringVersionAndModifiers); + if(leftClass != null) + modifiersChanged = true; } // No class signature change - if (leftClass != null && !annotationChanged) { + if (leftClass != null && !annotationChanged && !modifiersChanged) { UMLType leftSuperclass = leftClass.getUmlClass().getSuperclass(); UMLType rightSuperclass = rightClass.getUmlClass().getSuperclass(); if (leftSuperclass != null && rightSuperclass != null) { @@ -314,7 +321,7 @@ else if (leftSuperclass == null && rightSuperclass != null) { } continue; } - else if (leftClass != null && annotationChanged) { + else if (leftClass != null && (annotationChanged || modifiersChanged)) { UMLModelDiff umlModelDiffLocal = leftModel.diff(rightModel); List refactorings = umlModelDiffLocal.getRefactorings(); Set classRefactored = startClassChangeHistory.analyseClassRefactorings(refactorings, currentVersion, parentVersion, rightClass::equalIdentifierIgnoringVersion); diff --git a/src/main/java/org/codetracker/FileTrackerWithLocalFilesImpl.java b/src/main/java/org/codetracker/FileTrackerWithLocalFilesImpl.java index d0ce5018d23..579f90cfb08 100644 --- a/src/main/java/org/codetracker/FileTrackerWithLocalFilesImpl.java +++ b/src/main/java/org/codetracker/FileTrackerWithLocalFilesImpl.java @@ -277,10 +277,17 @@ public void blame() throws Exception { boolean annotationChanged = false; if(leftClass == null) { leftClass = getClass(leftModel, parentVersion, rightClass::equalIdentifierIgnoringVersionAndAnnotation); - annotationChanged = true; + if(leftClass != null) + annotationChanged = true; + } + boolean modifiersChanged = false; + if(leftClass == null) { + leftClass = getClass(leftModel, parentVersion, rightClass::equalIdentifierIgnoringVersionAndModifiers); + if(leftClass != null) + modifiersChanged = true; } // No class signature change - if (leftClass != null && !annotationChanged) { + if (leftClass != null && !annotationChanged && !modifiersChanged) { UMLType leftSuperclass = leftClass.getUmlClass().getSuperclass(); UMLType rightSuperclass = rightClass.getUmlClass().getSuperclass(); if (leftSuperclass != null && rightSuperclass != null) { @@ -322,7 +329,7 @@ else if (leftSuperclass == null && rightSuperclass != null) { } continue; } - else if (leftClass != null && annotationChanged) { + else if (leftClass != null && (annotationChanged || modifiersChanged)) { UMLModelDiff umlModelDiffLocal = leftModel.diff(rightModel); List refactorings = umlModelDiffLocal.getRefactorings(); Set classRefactored = startClassChangeHistory.analyseClassRefactorings(refactorings, currentVersion, parentVersion, rightClass::equalIdentifierIgnoringVersion); diff --git a/src/main/java/org/codetracker/element/Attribute.java b/src/main/java/org/codetracker/element/Attribute.java index 6fdea9127b6..f4fd5c080e1 100644 --- a/src/main/java/org/codetracker/element/Attribute.java +++ b/src/main/java/org/codetracker/element/Attribute.java @@ -15,6 +15,7 @@ import gr.uom.java.xmi.decomposition.StatementObject; import org.codetracker.api.Version; +import org.refactoringminer.util.PrefixSuffixUtils; import static org.codetracker.util.Util.annotationsToString; import static org.codetracker.util.Util.getPath; @@ -58,7 +59,19 @@ public boolean differInFormatting(Attribute other) { String thisSignature = umlAttribute.getVariableDeclaration().getActualSignature(); String otherSignature = other.umlAttribute.getVariableDeclaration().getActualSignature(); if (thisSignature != null && otherSignature != null) { - return !thisSignature.equals(otherSignature) && thisSignature.replaceAll("\\s+","").equals(otherSignature.replaceAll("\\s+","")); + if(!thisSignature.equals(otherSignature)) { + //whitespace change + if(thisSignature.replaceAll("\\s+","").equals(otherSignature.replaceAll("\\s+",""))) { + return true; + } + else { + String prefix = PrefixSuffixUtils.longestCommonPrefix(thisSignature, otherSignature); + String suffix = PrefixSuffixUtils.longestCommonSuffix(thisSignature, otherSignature); + if(!prefix.isEmpty() && !suffix.isEmpty()) { + return true; + } + } + } } return false; } diff --git a/src/main/java/org/codetracker/element/Class.java b/src/main/java/org/codetracker/element/Class.java index 13dbac43d5f..ef31134ed0a 100644 --- a/src/main/java/org/codetracker/element/Class.java +++ b/src/main/java/org/codetracker/element/Class.java @@ -20,11 +20,13 @@ public class Class extends BaseCodeElement { private final UMLAbstractClass umlClass; private final String identifierIgnoringVersionAndAnnotation; + private final String identifierIgnoringVersionAndModifiers; - private Class(UMLAbstractClass umlClass, String identifierExcludeVersion, String identifierExcludeVersionAndAnnotation, String name, String filePath, Version version) { + private Class(UMLAbstractClass umlClass, String identifierExcludeVersion, String identifierExcludeVersionAndAnnotation, String identifierIgnoringVersionAndModifiers, String name, String filePath, Version version) { super(identifierExcludeVersion, name, filePath, version); this.umlClass = umlClass; this.identifierIgnoringVersionAndAnnotation = identifierExcludeVersionAndAnnotation; + this.identifierIgnoringVersionAndModifiers = identifierIgnoringVersionAndModifiers; } public BaseCodeElement of(Version version) { @@ -43,8 +45,9 @@ public static Class of(UMLAbstractClass umlClass, Version version) { String visibility = umlClass.getVisibility().toString(); String identifierExcludeVersion = String.format("%s%s.(%s)%s(%s)%s%s", sourceFolder, packageName, visibility, modifiersString, umlClass.getTypeDeclarationKind(), name, annotationsToString(umlClass.getAnnotations())); String identifierExcludeVersionAndAnnotation = String.format("%s%s.(%s)%s(%s)%s", sourceFolder, packageName, visibility, modifiersString, umlClass.getTypeDeclarationKind(), name); + String identifierExcludeVersionAndModifiers = String.format("%s%s.(%s)(%s)%s%s", sourceFolder, packageName, visibility, umlClass.getTypeDeclarationKind(), name, annotationsToString(umlClass.getAnnotations())); String className = String.format("%s%s.(%s)%s%s(%d)", sourceFolder, packageName, visibility, modifiersString, name, umlClass.getLocationInfo().getStartLine()); - return new Class(umlClass, identifierExcludeVersion, identifierExcludeVersionAndAnnotation, className, umlClass.getLocationInfo().getFilePath(), version); + return new Class(umlClass, identifierExcludeVersion, identifierExcludeVersionAndAnnotation, identifierExcludeVersionAndModifiers, className, umlClass.getLocationInfo().getFilePath(), version); } public boolean differInFormatting(Class other) { @@ -153,6 +156,10 @@ public boolean equalIdentifierIgnoringVersionAndAnnotation(Class clazz) { return this.identifierIgnoringVersionAndAnnotation.equals(clazz.identifierIgnoringVersionAndAnnotation); } + public boolean equalIdentifierIgnoringVersionAndModifiers(Class clazz) { + return this.identifierIgnoringVersionAndModifiers.equals(clazz.identifierIgnoringVersionAndModifiers); + } + public void checkClosingBracket(int lineNumber) { if (getLocation().getEndLine() == lineNumber) { setClosingCurlyBracket(true); diff --git a/src/test/java/org/codetracker/blame/CodeTrackerBlameTest.java b/src/test/java/org/codetracker/blame/CodeTrackerBlameTest.java index d933d2c35ef..d1d4669cd27 100644 --- a/src/test/java/org/codetracker/blame/CodeTrackerBlameTest.java +++ b/src/test/java/org/codetracker/blame/CodeTrackerBlameTest.java @@ -65,6 +65,7 @@ private static Stream testBlamerInputProvider(){ "https://github.com/hibernate/hibernate-orm/commit/8bd79b29cfa7b2d539a746dc356d60b66e1e596b, hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java, /src/test/resources/blame/blameTestWithLocalRepo8.txt", "https://github.com/apache/flink/commit/9e936a5f8198b0059e9b5fba33163c2bbe3efbdd, flink-streaming-java/src/main/java/org/apache/flink/streaming/api/datastream/DataStream.java, /src/test/resources/blame/blameTestWithLocalRepo9.txt", "https://github.com/checkstyle/checkstyle/commit/119fd4fb33bef9f5c66fc950396669af842c21a3, src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java, /src/test/resources/blame/blameTestWithLocalRepo10.txt", + "https://github.com/apache/commons-lang/commit/a36c903d4f1065bc59f5e6d2bb0f9d92a5e71d83, src/main/java/org/apache/commons/lang3/time/DateUtils.java, /src/test/resources/blame/blameTestWithLocalRepo11.txt", "https://github.com/eclipse/jgit/commit/bd1a82502680b5de5bf86f6c4470185fd1602386, org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/pack/PackWriter.java, /src/test/resources/blame/blameTestUntilCommitZero.txt", "https://github.com/JetBrains/intellij-community/commit/ecb1bb9d4d484ae63ee77f8ad45bdce154db9356, java/compiler/impl/src/com/intellij/compiler/CompilerManagerImpl.java, /src/test/resources/blame/blameTestUntilCommitZero2.txt", "https://github.com/JetBrains/intellij-community/commit/ecb1bb9d4d484ae63ee77f8ad45bdce154db9356, java/compiler/impl/src/com/intellij/compiler/actions/CompileDirtyAction.java, /src/test/resources/blame/blameTestUntilCommitZero3.txt" diff --git a/src/test/java/org/codetracker/blame/CodeTrackerBlameWithLocalFilesTest.java b/src/test/java/org/codetracker/blame/CodeTrackerBlameWithLocalFilesTest.java index 005c131fa69..9037155b0af 100644 --- a/src/test/java/org/codetracker/blame/CodeTrackerBlameWithLocalFilesTest.java +++ b/src/test/java/org/codetracker/blame/CodeTrackerBlameWithLocalFilesTest.java @@ -28,6 +28,7 @@ public class CodeTrackerBlameWithLocalFilesTest { "https://github.com/hibernate/hibernate-orm/commit/8bd79b29cfa7b2d539a746dc356d60b66e1e596b, hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java, /src/test/resources/blame/blameTestWithLocalRepo8.txt", //"https://github.com/apache/flink/commit/9e936a5f8198b0059e9b5fba33163c2bbe3efbdd, flink-streaming-java/src/main/java/org/apache/flink/streaming/api/datastream/DataStream.java, /src/test/resources/blame/blameTestWithLocalRepo9.txt", "https://github.com/checkstyle/checkstyle/commit/119fd4fb33bef9f5c66fc950396669af842c21a3, src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java, /src/test/resources/blame/blameTestWithLocalRepo10.txt", + "https://github.com/apache/commons-lang/commit/a36c903d4f1065bc59f5e6d2bb0f9d92a5e71d83, src/main/java/org/apache/commons/lang3/time/DateUtils.java, /src/test/resources/blame/blameTestWithLocalRepo11.txt", "https://github.com/eclipse/jgit/commit/bd1a82502680b5de5bf86f6c4470185fd1602386, org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/pack/PackWriter.java, /src/test/resources/blame/blameTestUntilCommitZero.txt", "https://github.com/JetBrains/intellij-community/commit/ecb1bb9d4d484ae63ee77f8ad45bdce154db9356, java/compiler/impl/src/com/intellij/compiler/CompilerManagerImpl.java, /src/test/resources/blame/blameTestUntilCommitZero2.txt", "https://github.com/JetBrains/intellij-community/commit/ecb1bb9d4d484ae63ee77f8ad45bdce154db9356, java/compiler/impl/src/com/intellij/compiler/actions/CompileDirtyAction.java, /src/test/resources/blame/blameTestUntilCommitZero3.txt" diff --git a/src/test/resources/blame/blameTestWithLocalRepo11.txt b/src/test/resources/blame/blameTestWithLocalRepo11.txt new file mode 100644 index 00000000000..3a8a1f6ee8d --- /dev/null +++ b/src/test/resources/blame/blameTestWithLocalRepo11.txt @@ -0,0 +1,1845 @@ +43e94f47b src/java/org/apache/commons/lang/time/DateUtils.java (Gary D. Gregory 2004-02-18 23:06:54 +0000 1) /* +a5a4f9067 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-08-28 06:21:41 +0000 2) * Licensed to the Apache Software Foundation (ASF) under one or more +a5a4f9067 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-08-28 06:21:41 +0000 3) * contributor license agreements. See the NOTICE file distributed with +a5a4f9067 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-08-28 06:21:41 +0000 4) * this work for additional information regarding copyright ownership. +a5a4f9067 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-08-28 06:21:41 +0000 5) * The ASF licenses this file to You under the Apache License, Version 2.0 +a5a4f9067 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-08-28 06:21:41 +0000 6) * (the "License"); you may not use this file except in compliance with +a5a4f9067 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-08-28 06:21:41 +0000 7) * the License. You may obtain a copy of the License at +26c6a7c28 src/java/org/apache/commons/lang/DateUtils.java (Henri Yandell 2002-12-16 21:48:41 +0000 8) * +43e94f47b src/java/org/apache/commons/lang/time/DateUtils.java (Gary D. Gregory 2004-02-18 23:06:54 +0000 9) * http://www.apache.org/licenses/LICENSE-2.0 +26c6a7c28 src/java/org/apache/commons/lang/DateUtils.java (Henri Yandell 2002-12-16 21:48:41 +0000 10) * +43e94f47b src/java/org/apache/commons/lang/time/DateUtils.java (Gary D. Gregory 2004-02-18 23:06:54 +0000 11) * Unless required by applicable law or agreed to in writing, software +43e94f47b src/java/org/apache/commons/lang/time/DateUtils.java (Gary D. Gregory 2004-02-18 23:06:54 +0000 12) * distributed under the License is distributed on an "AS IS" BASIS, +43e94f47b src/java/org/apache/commons/lang/time/DateUtils.java (Gary D. Gregory 2004-02-18 23:06:54 +0000 13) * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +43e94f47b src/java/org/apache/commons/lang/time/DateUtils.java (Gary D. Gregory 2004-02-18 23:06:54 +0000 14) * See the License for the specific language governing permissions and +43e94f47b src/java/org/apache/commons/lang/time/DateUtils.java (Gary D. Gregory 2004-02-18 23:06:54 +0000 15) * limitations under the License. +26c6a7c28 src/java/org/apache/commons/lang/DateUtils.java (Henri Yandell 2002-12-16 21:48:41 +0000 16) */ +c6e8f706d src/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2009-12-10 11:56:38 +0000 17) package org.apache.commons.lang3.time; + 18) +ac517f7c0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 21:06:18 +0000 19) import java.text.ParseException; +ac517f7c0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 21:06:18 +0000 20) import java.text.ParsePosition; +26c6a7c28 src/java/org/apache/commons/lang/DateUtils.java (Henri Yandell 2002-12-16 21:48:41 +0000 21) import java.util.Calendar; +26c6a7c28 src/java/org/apache/commons/lang/DateUtils.java (Henri Yandell 2002-12-16 21:48:41 +0000 22) import java.util.Date; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 23) import java.util.Iterator; +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 24) import java.util.Locale; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 25) import java.util.NoSuchElementException; +bea1ae92a src/main/java/org/apache/commons/lang3/time/DateUtils.java (Chas Honton 2015-04-30 23:19:38 +0000 26) import java.util.TimeZone; +3ee44535b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-03-14 13:23:25 +0000 27) import java.util.concurrent.TimeUnit; + 28) +ff6b0ada7 src/main/java/org/apache/commons/lang3/time/DateUtils.java (pascalschumacher 2017-04-19 19:07:05 +0000 29) import org.apache.commons.lang3.Validate; + 30) +26c6a7c28 src/java/org/apache/commons/lang/DateUtils.java (Henri Yandell 2002-12-16 21:48:41 +0000 31) /** +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 32) *

A suite of utilities surrounding the use of the +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 33) * {@link java.util.Calendar} and {@link java.util.Date} object.

+26c6a7c28 src/java/org/apache/commons/lang/DateUtils.java (Henri Yandell 2002-12-16 21:48:41 +0000 34) * +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 35) *

DateUtils contains a lot of common methods considering manipulations +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 36) * of Dates or Calendars. Some methods require some extra explanation. +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 37) * The truncate, ceiling and round methods could be considered the Math.floor(), +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 38) * Math.ceil() or Math.round versions for dates +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 39) * This way date-fields will be ignored in bottom-up order. +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 40) * As a complement to these methods we've introduced some fragment-methods. +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 41) * With these methods the Date-fields will be ignored in top-down order. +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 42) * Since a date without a year is not a valid date, you have to decide in what +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 43) * kind of date-field you want your result, for instance milliseconds or days. +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 44) *

+bdb1a684a src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-26 11:19:32 +0000 45) *

+26c6a7c28 src/java/org/apache/commons/lang/DateUtils.java (Henri Yandell 2002-12-16 21:48:41 +0000 46) * Several methods are provided for adding to {@code Date} objects, of the form +26c6a7c28 src/java/org/apache/commons/lang/DateUtils.java (Henri Yandell 2002-12-16 21:48:41 +0000 47) * {@code addXXX(Date date, int amount)}. It is important to note these methods +bdb1a684a src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-26 11:19:32 +0000 48) * use a {@code Calendar} internally (with default timezone and locale) and may +bdb1a684a src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-26 11:19:32 +0000 49) * be affected by changes to daylight saving time (DST). +bdb1a684a src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-26 11:19:32 +0000 50) *

+26c6a7c28 src/java/org/apache/commons/lang/DateUtils.java (Henri Yandell 2002-12-16 21:48:41 +0000 51) * +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 52) * @since 2.0 +26c6a7c28 src/java/org/apache/commons/lang/DateUtils.java (Henri Yandell 2002-12-16 21:48:41 +0000 53) */ +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 54) public class DateUtils { + 55) +e2225defe src/java/org/apache/commons/lang/time/DateUtils.java (Phil Steitz 2003-12-23 03:54:14 +0000 56) /** +e2225defe src/java/org/apache/commons/lang/time/DateUtils.java (Phil Steitz 2003-12-23 03:54:14 +0000 57) * Number of milliseconds in a standard second. +c9e1d2936 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-08 00:11:00 +0000 58) * @since 2.1 +e2225defe src/java/org/apache/commons/lang/time/DateUtils.java (Phil Steitz 2003-12-23 03:54:14 +0000 59) */ +e2225defe src/java/org/apache/commons/lang/time/DateUtils.java (Phil Steitz 2003-12-23 03:54:14 +0000 60) public static final long MILLIS_PER_SECOND = 1000; +e2225defe src/java/org/apache/commons/lang/time/DateUtils.java (Phil Steitz 2003-12-23 03:54:14 +0000 61) /** +e2225defe src/java/org/apache/commons/lang/time/DateUtils.java (Phil Steitz 2003-12-23 03:54:14 +0000 62) * Number of milliseconds in a standard minute. +c9e1d2936 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-08 00:11:00 +0000 63) * @since 2.1 +e2225defe src/java/org/apache/commons/lang/time/DateUtils.java (Phil Steitz 2003-12-23 03:54:14 +0000 64) */ +e5e5fb3eb src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-09-07 22:50:45 +0000 65) public static final long MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND; +e2225defe src/java/org/apache/commons/lang/time/DateUtils.java (Phil Steitz 2003-12-23 03:54:14 +0000 66) /** +e2225defe src/java/org/apache/commons/lang/time/DateUtils.java (Phil Steitz 2003-12-23 03:54:14 +0000 67) * Number of milliseconds in a standard hour. +c9e1d2936 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-08 00:11:00 +0000 68) * @since 2.1 +e2225defe src/java/org/apache/commons/lang/time/DateUtils.java (Phil Steitz 2003-12-23 03:54:14 +0000 69) */ +e5e5fb3eb src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-09-07 22:50:45 +0000 70) public static final long MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE; +e2225defe src/java/org/apache/commons/lang/time/DateUtils.java (Phil Steitz 2003-12-23 03:54:14 +0000 71) /** +e2225defe src/java/org/apache/commons/lang/time/DateUtils.java (Phil Steitz 2003-12-23 03:54:14 +0000 72) * Number of milliseconds in a standard day. +c9e1d2936 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-08 00:11:00 +0000 73) * @since 2.1 +e2225defe src/java/org/apache/commons/lang/time/DateUtils.java (Phil Steitz 2003-12-23 03:54:14 +0000 74) */ +e5e5fb3eb src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-09-07 22:50:45 +0000 75) public static final long MILLIS_PER_DAY = 24 * MILLIS_PER_HOUR; + 76) +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 77) /** +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 78) * This is half a month, so this represents whether a date is in the top +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 79) * or bottom half of the month. +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 80) */ +bbc7fd046 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:00:05 +0000 81) public static final int SEMI_MONTH = 1001; + 82) +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 83) private static final int[][] fields = { +228957a0e src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2005-02-04 03:16:58 +0000 84) {Calendar.MILLISECOND}, +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 85) {Calendar.SECOND}, +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 86) {Calendar.MINUTE}, +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 87) {Calendar.HOUR_OF_DAY, Calendar.HOUR}, +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 88) {Calendar.DATE, Calendar.DAY_OF_MONTH, Calendar.AM_PM +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 89) /* Calendar.DAY_OF_YEAR, Calendar.DAY_OF_WEEK, Calendar.DAY_OF_WEEK_IN_MONTH */ +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 90) }, +b610707cd src/main/java/org/apache/commons/lang3/time/DateUtils.java (Igor Curdvanovschi 2018-06-20 11:28:10 +0000 91) {Calendar.MONTH, SEMI_MONTH}, +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 92) {Calendar.YEAR}, +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 93) {Calendar.ERA}}; + 94) +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 95) /** +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 96) * A week range, starting on Sunday. +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 97) */ +bbc7fd046 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:00:05 +0000 98) public static final int RANGE_WEEK_SUNDAY = 1; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 99) /** +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 100) * A week range, starting on Monday. +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 101) */ +bbc7fd046 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:00:05 +0000 102) public static final int RANGE_WEEK_MONDAY = 2; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 103) /** +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 104) * A week range, starting on the day focused. +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 105) */ +bbc7fd046 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:00:05 +0000 106) public static final int RANGE_WEEK_RELATIVE = 3; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 107) /** +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 108) * A week range, centered around the day focused. +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 109) */ +bbc7fd046 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:00:05 +0000 110) public static final int RANGE_WEEK_CENTER = 4; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 111) /** +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 112) * A month range, the week starting on Sunday. +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 113) */ +bbc7fd046 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:00:05 +0000 114) public static final int RANGE_MONTH_SUNDAY = 5; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 115) /** +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 116) * A month range, the week starting on Monday. +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 117) */ +bbc7fd046 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:00:05 +0000 118) public static final int RANGE_MONTH_MONDAY = 6; + 119) +52e854e3b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-27 15:03:26 +0000 120) /** +52e854e3b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-27 15:03:26 +0000 121) * Calendar modification types. +52e854e3b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-27 15:03:26 +0000 122) */ +2aae22de2 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-27 17:33:00 +0000 123) private enum ModifyType { +52e854e3b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-27 15:03:26 +0000 124) /** +52e854e3b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-27 15:03:26 +0000 125) * Truncation. +52e854e3b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-27 15:03:26 +0000 126) */ +52e854e3b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-27 15:03:26 +0000 127) TRUNCATE, + 128) +52e854e3b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-27 15:03:26 +0000 129) /** +52e854e3b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-27 15:03:26 +0000 130) * Rounding. +52e854e3b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-27 15:03:26 +0000 131) */ +52e854e3b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-27 15:03:26 +0000 132) ROUND, + 133) +52e854e3b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-27 15:03:26 +0000 134) /** +52e854e3b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-27 15:03:26 +0000 135) * Ceiling. +52e854e3b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-27 15:03:26 +0000 136) */ +52e854e3b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-27 15:03:26 +0000 137) CEILING +52e854e3b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-27 15:03:26 +0000 138) } + 139) +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 140) /** +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 141) *

{@code DateUtils} instances should NOT be constructed in +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 142) * standard programming. Instead, the static methods on the class should +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 143) * be used, such as {@code DateUtils.parseDate(str);}.

+402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 144) * +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 145) *

This constructor is public to permit tools that require a JavaBean +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 146) * instance to operate.

+402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 147) */ +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 148) public DateUtils() { +8abba8d53 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2005-07-06 01:12:46 +0000 149) super(); +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 150) } + 151) +4d7e4b5e7 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-13 21:48:00 +0000 152) //----------------------------------------------------------------------- +4d7e4b5e7 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-13 21:48:00 +0000 153) /** +4d7e4b5e7 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-13 21:48:00 +0000 154) *

Checks if two date objects are on the same day ignoring time.

+4d7e4b5e7 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-13 21:48:00 +0000 155) * +4d7e4b5e7 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-13 21:48:00 +0000 156) *

28 Mar 2002 13:45 and 28 Mar 2002 06:01 would return true. +4d7e4b5e7 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-13 21:48:00 +0000 157) * 28 Mar 2002 13:45 and 12 Mar 2002 13:45 would return false. +4d7e4b5e7 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-13 21:48:00 +0000 158) *

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 159) * +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 160) * @param date1 the first date, not altered, not null +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 161) * @param date2 the second date, not altered, not null +4d7e4b5e7 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-13 21:48:00 +0000 162) * @return true if they represent the same day +4d7e4b5e7 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-13 21:48:00 +0000 163) * @throws IllegalArgumentException if either date is null +4d7e4b5e7 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-13 21:48:00 +0000 164) * @since 2.1 +4d7e4b5e7 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-13 21:48:00 +0000 165) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 166) public static boolean isSameDay(final Date date1, final Date date2) { +4d7e4b5e7 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-13 21:48:00 +0000 167) if (date1 == null || date2 == null) { +4d7e4b5e7 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-13 21:48:00 +0000 168) throw new IllegalArgumentException("The date must not be null"); +4d7e4b5e7 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-13 21:48:00 +0000 169) } +5292526e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:09:45 +0000 170) final Calendar cal1 = Calendar.getInstance(); +4d7e4b5e7 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-13 21:48:00 +0000 171) cal1.setTime(date1); +5292526e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:09:45 +0000 172) final Calendar cal2 = Calendar.getInstance(); +4d7e4b5e7 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-13 21:48:00 +0000 173) cal2.setTime(date2); +4d7e4b5e7 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-13 21:48:00 +0000 174) return isSameDay(cal1, cal2); +4d7e4b5e7 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-13 21:48:00 +0000 175) } + 176) +fc40d1cb6 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-09-07 23:03:37 +0000 177) /** +fc40d1cb6 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-09-07 23:03:37 +0000 178) *

Checks if two calendar objects are on the same day ignoring time.

+fc40d1cb6 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-09-07 23:03:37 +0000 179) * +fc40d1cb6 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-09-07 23:03:37 +0000 180) *

28 Mar 2002 13:45 and 28 Mar 2002 06:01 would return true. +fc40d1cb6 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-09-07 23:03:37 +0000 181) * 28 Mar 2002 13:45 and 12 Mar 2002 13:45 would return false. +fc40d1cb6 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-09-07 23:03:37 +0000 182) *

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 183) * +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 184) * @param cal1 the first calendar, not altered, not null +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 185) * @param cal2 the second calendar, not altered, not null +fc40d1cb6 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-09-07 23:03:37 +0000 186) * @return true if they represent the same day +fc40d1cb6 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-09-07 23:03:37 +0000 187) * @throws IllegalArgumentException if either calendar is null +c9e1d2936 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-08 00:11:00 +0000 188) * @since 2.1 +fc40d1cb6 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-09-07 23:03:37 +0000 189) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 190) public static boolean isSameDay(final Calendar cal1, final Calendar cal2) { +fc40d1cb6 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-09-07 23:03:37 +0000 191) if (cal1 == null || cal2 == null) { +fc40d1cb6 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-09-07 23:03:37 +0000 192) throw new IllegalArgumentException("The date must not be null"); +fc40d1cb6 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-09-07 23:03:37 +0000 193) } +35f1cc002 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2015-04-06 19:48:22 +0000 194) return cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) && +35f1cc002 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2015-04-06 19:48:22 +0000 195) cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && +35f1cc002 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2015-04-06 19:48:22 +0000 196) cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR); +fc40d1cb6 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-09-07 23:03:37 +0000 197) } + 198) +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 199) //----------------------------------------------------------------------- +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 200) /** +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 201) *

Checks if two date objects represent the same instant in time.

+61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 202) * +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 203) *

This method compares the long millisecond time of the two objects.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 204) * +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 205) * @param date1 the first date, not altered, not null +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 206) * @param date2 the second date, not altered, not null +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 207) * @return true if they represent the same millisecond instant +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 208) * @throws IllegalArgumentException if either date is null +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 209) * @since 2.1 +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 210) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 211) public static boolean isSameInstant(final Date date1, final Date date2) { +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 212) if (date1 == null || date2 == null) { +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 213) throw new IllegalArgumentException("The date must not be null"); +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 214) } +b0053ca69 src/java/org/apache/commons/lang/time/DateUtils.java (Gary D. Gregory 2005-04-13 22:36:48 +0000 215) return date1.getTime() == date2.getTime(); +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 216) } + 217) +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 218) /** +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 219) *

Checks if two calendar objects represent the same instant in time.

+61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 220) * +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 221) *

This method compares the long millisecond time of the two objects.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 222) * +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 223) * @param cal1 the first calendar, not altered, not null +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 224) * @param cal2 the second calendar, not altered, not null +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 225) * @return true if they represent the same millisecond instant +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 226) * @throws IllegalArgumentException if either date is null +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 227) * @since 2.1 +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 228) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 229) public static boolean isSameInstant(final Calendar cal1, final Calendar cal2) { +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 230) if (cal1 == null || cal2 == null) { +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 231) throw new IllegalArgumentException("The date must not be null"); +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 232) } +b0053ca69 src/java/org/apache/commons/lang/time/DateUtils.java (Gary D. Gregory 2005-04-13 22:36:48 +0000 233) return cal1.getTime().getTime() == cal2.getTime().getTime(); +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 234) } + 235) +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 236) //----------------------------------------------------------------------- +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 237) /** +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 238) *

Checks if two calendar objects represent the same local time.

+61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 239) * +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 240) *

This method compares the values of the fields of the two objects. +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 241) * In addition, both calendars must be the same of the same type.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 242) * +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 243) * @param cal1 the first calendar, not altered, not null +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 244) * @param cal2 the second calendar, not altered, not null +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 245) * @return true if they represent the same millisecond instant +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 246) * @throws IllegalArgumentException if either date is null +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 247) * @since 2.1 +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 248) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 249) public static boolean isSameLocalTime(final Calendar cal1, final Calendar cal2) { +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 250) if (cal1 == null || cal2 == null) { +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 251) throw new IllegalArgumentException("The date must not be null"); +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 252) } +35f1cc002 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2015-04-06 19:48:22 +0000 253) return cal1.get(Calendar.MILLISECOND) == cal2.get(Calendar.MILLISECOND) && +35f1cc002 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2015-04-06 19:48:22 +0000 254) cal1.get(Calendar.SECOND) == cal2.get(Calendar.SECOND) && +35f1cc002 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2015-04-06 19:48:22 +0000 255) cal1.get(Calendar.MINUTE) == cal2.get(Calendar.MINUTE) && +35f1cc002 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2015-04-06 19:48:22 +0000 256) cal1.get(Calendar.HOUR_OF_DAY) == cal2.get(Calendar.HOUR_OF_DAY) && +35f1cc002 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2015-04-06 19:48:22 +0000 257) cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR) && +35f1cc002 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2015-04-06 19:48:22 +0000 258) cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && +35f1cc002 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2015-04-06 19:48:22 +0000 259) cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) && +35f1cc002 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2015-04-06 19:48:22 +0000 260) cal1.getClass() == cal2.getClass(); +61cccd3e0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:08:42 +0000 261) } + 262) +ac517f7c0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 21:06:18 +0000 263) //----------------------------------------------------------------------- +ac517f7c0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 21:06:18 +0000 264) /** +ac517f7c0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 21:06:18 +0000 265) *

Parses a string representing a date by trying a variety of different parsers.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 266) * +ac517f7c0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 21:06:18 +0000 267) *

The parse will try each parse pattern in turn. +1a433d2ec src/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2010-01-01 19:01:18 +0000 268) * A parse is only deemed successful if it parses the whole of the input string. +ac517f7c0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 21:06:18 +0000 269) * If no parse patterns match, a ParseException is thrown.

+1a433d2ec src/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2010-01-01 19:01:18 +0000 270) * The parser will be lenient toward the parsed date. +ac517f7c0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 21:06:18 +0000 271) * +ac517f7c0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 21:06:18 +0000 272) * @param str the date to parse, not null +ac517f7c0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 21:06:18 +0000 273) * @param parsePatterns the date format patterns to use, see SimpleDateFormat, not null +ac517f7c0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 21:06:18 +0000 274) * @return the parsed date +ac517f7c0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 21:06:18 +0000 275) * @throws IllegalArgumentException if the date string or pattern array is null +950def5b6 src/java/org/apache/commons/lang/time/DateUtils.java (Sebastian Bazley 2009-03-16 02:11:38 +0000 276) * @throws ParseException if none of the date patterns were suitable (or there were none) +ac517f7c0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 21:06:18 +0000 277) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 278) public static Date parseDate(final String str, final String... parsePatterns) throws ParseException { +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 279) return parseDate(str, null, parsePatterns); +ac517f7c0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 21:06:18 +0000 280) } + 281) +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 282) //----------------------------------------------------------------------- +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 283) /** +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 284) *

Parses a string representing a date by trying a variety of different parsers, +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 285) * using the default date format symbols for the given locale.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 286) * +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 287) *

The parse will try each parse pattern in turn. +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 288) * A parse is only deemed successful if it parses the whole of the input string. +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 289) * If no parse patterns match, a ParseException is thrown.

+ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 290) * The parser will be lenient toward the parsed date. +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 291) * +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 292) * @param str the date to parse, not null +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 293) * @param locale the locale whose date format symbols should be used. If null, +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 294) * the system locale is used (as per {@link #parseDate(String, String...)}). +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 295) * @param parsePatterns the date format patterns to use, see SimpleDateFormat, not null +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 296) * @return the parsed date +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 297) * @throws IllegalArgumentException if the date string or pattern array is null +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 298) * @throws ParseException if none of the date patterns were suitable (or there were none) +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 299) * @since 3.2 +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 300) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 301) public static Date parseDate(final String str, final Locale locale, final String... parsePatterns) throws ParseException { +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 302) return parseDateWithLeniency(str, locale, parsePatterns, true); +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 303) } + 304) +1a433d2ec src/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2010-01-01 19:01:18 +0000 305) //----------------------------------------------------------------------- +1a433d2ec src/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2010-01-01 19:01:18 +0000 306) /** +1a433d2ec src/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2010-01-01 19:01:18 +0000 307) *

Parses a string representing a date by trying a variety of different parsers.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 308) * +1a433d2ec src/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2010-01-01 19:01:18 +0000 309) *

The parse will try each parse pattern in turn. +1a433d2ec src/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2010-01-01 19:01:18 +0000 310) * A parse is only deemed successful if it parses the whole of the input string. +1a433d2ec src/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2010-01-01 19:01:18 +0000 311) * If no parse patterns match, a ParseException is thrown.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 312) * The parser parses strictly - it does not allow for dates such as "February 942, 1996". +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 313) * +1a433d2ec src/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2010-01-01 19:01:18 +0000 314) * @param str the date to parse, not null +1a433d2ec src/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2010-01-01 19:01:18 +0000 315) * @param parsePatterns the date format patterns to use, see SimpleDateFormat, not null +1a433d2ec src/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2010-01-01 19:01:18 +0000 316) * @return the parsed date +1a433d2ec src/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2010-01-01 19:01:18 +0000 317) * @throws IllegalArgumentException if the date string or pattern array is null +1a433d2ec src/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2010-01-01 19:01:18 +0000 318) * @throws ParseException if none of the date patterns were suitable +43fca0d74 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Niall Pemberton 2010-02-25 01:28:13 +0000 319) * @since 2.5 +1a433d2ec src/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2010-01-01 19:01:18 +0000 320) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 321) public static Date parseDateStrictly(final String str, final String... parsePatterns) throws ParseException { +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 322) return parseDateStrictly(str, null, parsePatterns); +1a433d2ec src/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2010-01-01 19:01:18 +0000 323) } + 324) +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 325) /** +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 326) *

Parses a string representing a date by trying a variety of different parsers, +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 327) * using the default date format symbols for the given locale..

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 328) * +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 329) *

The parse will try each parse pattern in turn. +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 330) * A parse is only deemed successful if it parses the whole of the input string. +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 331) * If no parse patterns match, a ParseException is thrown.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 332) * The parser parses strictly - it does not allow for dates such as "February 942, 1996". +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 333) * +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 334) * @param str the date to parse, not null +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 335) * @param locale the locale whose date format symbols should be used. If null, +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 336) * the system locale is used (as per {@link #parseDateStrictly(String, String...)}). +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 337) * @param parsePatterns the date format patterns to use, see SimpleDateFormat, not null +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 338) * @return the parsed date +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 339) * @throws IllegalArgumentException if the date string or pattern array is null +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 340) * @throws ParseException if none of the date patterns were suitable +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 341) * @since 3.2 +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 342) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 343) public static Date parseDateStrictly(final String str, final Locale locale, final String... parsePatterns) throws ParseException { +c99a78775 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Chas Honton 2016-04-24 00:44:48 +0000 344) return parseDateWithLeniency(str, locale, parsePatterns, false); +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 345) } + 346) +381cb097a src/main/java/org/apache/commons/lang3/time/DateUtils.java (Niall Pemberton 2010-02-01 12:00:11 +0000 347) /** +381cb097a src/main/java/org/apache/commons/lang3/time/DateUtils.java (Niall Pemberton 2010-02-01 12:00:11 +0000 348) *

Parses a string representing a date by trying a variety of different parsers.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 349) * +381cb097a src/main/java/org/apache/commons/lang3/time/DateUtils.java (Niall Pemberton 2010-02-01 12:00:11 +0000 350) *

The parse will try each parse pattern in turn. +381cb097a src/main/java/org/apache/commons/lang3/time/DateUtils.java (Niall Pemberton 2010-02-01 12:00:11 +0000 351) * A parse is only deemed successful if it parses the whole of the input string. +381cb097a src/main/java/org/apache/commons/lang3/time/DateUtils.java (Niall Pemberton 2010-02-01 12:00:11 +0000 352) * If no parse patterns match, a ParseException is thrown.

+381cb097a src/main/java/org/apache/commons/lang3/time/DateUtils.java (Niall Pemberton 2010-02-01 12:00:11 +0000 353) * +381cb097a src/main/java/org/apache/commons/lang3/time/DateUtils.java (Niall Pemberton 2010-02-01 12:00:11 +0000 354) * @param str the date to parse, not null +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 355) * @param locale the locale to use when interpretting the pattern, can be null in which +ebdef1b95 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2012-09-22 13:22:30 +0000 356) * case the default system locale is used +381cb097a src/main/java/org/apache/commons/lang3/time/DateUtils.java (Niall Pemberton 2010-02-01 12:00:11 +0000 357) * @param parsePatterns the date format patterns to use, see SimpleDateFormat, not null +381cb097a src/main/java/org/apache/commons/lang3/time/DateUtils.java (Niall Pemberton 2010-02-01 12:00:11 +0000 358) * @param lenient Specify whether or not date/time parsing is to be lenient. +381cb097a src/main/java/org/apache/commons/lang3/time/DateUtils.java (Niall Pemberton 2010-02-01 12:00:11 +0000 359) * @return the parsed date +381cb097a src/main/java/org/apache/commons/lang3/time/DateUtils.java (Niall Pemberton 2010-02-01 12:00:11 +0000 360) * @throws IllegalArgumentException if the date string or pattern array is null +381cb097a src/main/java/org/apache/commons/lang3/time/DateUtils.java (Niall Pemberton 2010-02-01 12:00:11 +0000 361) * @throws ParseException if none of the date patterns were suitable +3af7ba8e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2014-01-15 18:56:57 +0000 362) * @see java.util.Calendar#isLenient() +381cb097a src/main/java/org/apache/commons/lang3/time/DateUtils.java (Niall Pemberton 2010-02-01 12:00:11 +0000 363) */ +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 364) private static Date parseDateWithLeniency( +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 365) final String str, final Locale locale, final String[] parsePatterns, final boolean lenient) throws ParseException { +ac517f7c0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 21:06:18 +0000 366) if (str == null || parsePatterns == null) { +ac517f7c0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 21:06:18 +0000 367) throw new IllegalArgumentException("Date and Patterns must not be null"); +ac517f7c0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 21:06:18 +0000 368) } + 369) +bea1ae92a src/main/java/org/apache/commons/lang3/time/DateUtils.java (Chas Honton 2015-04-30 23:19:38 +0000 370) final TimeZone tz = TimeZone.getDefault(); +bea1ae92a src/main/java/org/apache/commons/lang3/time/DateUtils.java (Chas Honton 2015-04-30 23:19:38 +0000 371) final Locale lcl = locale==null ?Locale.getDefault() : locale; +5292526e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:09:45 +0000 372) final ParsePosition pos = new ParsePosition(0); +94faa31bc src/main/java/org/apache/commons/lang3/time/DateUtils.java (Chas Honton 2015-06-12 03:07:13 +0000 373) final Calendar calendar = Calendar.getInstance(tz, lcl); +94faa31bc src/main/java/org/apache/commons/lang3/time/DateUtils.java (Chas Honton 2015-06-12 03:07:13 +0000 374) calendar.setLenient(lenient); + 375) +5292526e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:09:45 +0000 376) for (final String parsePattern : parsePatterns) { +eb2b89efb src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary Gregory 2016-10-23 17:55:56 +0000 377) final FastDateParser fdp = new FastDateParser(parsePattern, tz, lcl); +94faa31bc src/main/java/org/apache/commons/lang3/time/DateUtils.java (Chas Honton 2015-06-12 03:07:13 +0000 378) calendar.clear(); +bea1ae92a src/main/java/org/apache/commons/lang3/time/DateUtils.java (Chas Honton 2015-04-30 23:19:38 +0000 379) try { +94faa31bc src/main/java/org/apache/commons/lang3/time/DateUtils.java (Chas Honton 2015-06-12 03:07:13 +0000 380) if (fdp.parse(str, pos, calendar) && pos.getIndex()==str.length()) { +94faa31bc src/main/java/org/apache/commons/lang3/time/DateUtils.java (Chas Honton 2015-06-12 03:07:13 +0000 381) return calendar.getTime(); +ac517f7c0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 21:06:18 +0000 382) } +eb2b89efb src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary Gregory 2016-10-23 17:55:56 +0000 383) } catch(final IllegalArgumentException ignore) { +94faa31bc src/main/java/org/apache/commons/lang3/time/DateUtils.java (Chas Honton 2015-06-12 03:07:13 +0000 384) // leniency is preventing calendar from being set +bea1ae92a src/main/java/org/apache/commons/lang3/time/DateUtils.java (Chas Honton 2015-04-30 23:19:38 +0000 385) } +ac517f7c0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 21:06:18 +0000 386) pos.setIndex(0); +ac517f7c0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 21:06:18 +0000 387) } +ac517f7c0 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 21:06:18 +0000 388) throw new ParseException("Unable to parse the date: " + str, -1); +1a433d2ec src/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2010-01-01 19:01:18 +0000 389) } + 390) +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 391) //----------------------------------------------------------------------- +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 392) /** +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 393) * Adds a number of years to a date returning a new object. +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 394) * The original {@code Date} is unchanged. +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 395) * +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 396) * @param date the date, not null +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 397) * @param amount the amount to add, may be negative +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 398) * @return the new {@code Date} with the amount added +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 399) * @throws IllegalArgumentException if the date is null +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 400) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 401) public static Date addYears(final Date date, final int amount) { +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 402) return add(date, Calendar.YEAR, amount); +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 403) } + 404) +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 405) //----------------------------------------------------------------------- +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 406) /** +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 407) * Adds a number of months to a date returning a new object. +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 408) * The original {@code Date} is unchanged. +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 409) * +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 410) * @param date the date, not null +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 411) * @param amount the amount to add, may be negative +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 412) * @return the new {@code Date} with the amount added +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 413) * @throws IllegalArgumentException if the date is null +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 414) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 415) public static Date addMonths(final Date date, final int amount) { +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 416) return add(date, Calendar.MONTH, amount); +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 417) } + 418) +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 419) //----------------------------------------------------------------------- +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 420) /** +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 421) * Adds a number of weeks to a date returning a new object. +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 422) * The original {@code Date} is unchanged. +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 423) * +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 424) * @param date the date, not null +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 425) * @param amount the amount to add, may be negative +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 426) * @return the new {@code Date} with the amount added +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 427) * @throws IllegalArgumentException if the date is null +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 428) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 429) public static Date addWeeks(final Date date, final int amount) { +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 430) return add(date, Calendar.WEEK_OF_YEAR, amount); +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 431) } + 432) +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 433) //----------------------------------------------------------------------- +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 434) /** +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 435) * Adds a number of days to a date returning a new object. +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 436) * The original {@code Date} is unchanged. +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 437) * +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 438) * @param date the date, not null +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 439) * @param amount the amount to add, may be negative +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 440) * @return the new {@code Date} with the amount added +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 441) * @throws IllegalArgumentException if the date is null +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 442) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 443) public static Date addDays(final Date date, final int amount) { +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 444) return add(date, Calendar.DAY_OF_MONTH, amount); +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 445) } + 446) +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 447) //----------------------------------------------------------------------- +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 448) /** +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 449) * Adds a number of hours to a date returning a new object. +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 450) * The original {@code Date} is unchanged. +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 451) * +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 452) * @param date the date, not null +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 453) * @param amount the amount to add, may be negative +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 454) * @return the new {@code Date} with the amount added +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 455) * @throws IllegalArgumentException if the date is null +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 456) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 457) public static Date addHours(final Date date, final int amount) { +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 458) return add(date, Calendar.HOUR_OF_DAY, amount); +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 459) } + 460) +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 461) //----------------------------------------------------------------------- +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 462) /** +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 463) * Adds a number of minutes to a date returning a new object. +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 464) * The original {@code Date} is unchanged. +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 465) * +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 466) * @param date the date, not null +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 467) * @param amount the amount to add, may be negative +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 468) * @return the new {@code Date} with the amount added +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 469) * @throws IllegalArgumentException if the date is null +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 470) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 471) public static Date addMinutes(final Date date, final int amount) { +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 472) return add(date, Calendar.MINUTE, amount); +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 473) } + 474) +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 475) //----------------------------------------------------------------------- +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 476) /** +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 477) * Adds a number of seconds to a date returning a new object. +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 478) * The original {@code Date} is unchanged. +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 479) * +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 480) * @param date the date, not null +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 481) * @param amount the amount to add, may be negative +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 482) * @return the new {@code Date} with the amount added +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 483) * @throws IllegalArgumentException if the date is null +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 484) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 485) public static Date addSeconds(final Date date, final int amount) { +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 486) return add(date, Calendar.SECOND, amount); +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 487) } + 488) +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 489) //----------------------------------------------------------------------- +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 490) /** +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 491) * Adds a number of milliseconds to a date returning a new object. +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 492) * The original {@code Date} is unchanged. +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 493) * +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 494) * @param date the date, not null +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 495) * @param amount the amount to add, may be negative +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 496) * @return the new {@code Date} with the amount added +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 497) * @throws IllegalArgumentException if the date is null +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 498) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 499) public static Date addMilliseconds(final Date date, final int amount) { +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 500) return add(date, Calendar.MILLISECOND, amount); +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 501) } + 502) +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 503) //----------------------------------------------------------------------- +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 504) /** +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 505) * Adds to a date returning a new object. +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 506) * The original {@code Date} is unchanged. +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 507) * +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 508) * @param date the date, not null +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 509) * @param calendarField the calendar field to add to +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 510) * @param amount the amount to add, may be negative +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 511) * @return the new {@code Date} with the amount added +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 512) * @throws IllegalArgumentException if the date is null +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 513) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 514) private static Date add(final Date date, final int calendarField, final int amount) { +ff6b0ada7 src/main/java/org/apache/commons/lang3/time/DateUtils.java (pascalschumacher 2017-04-19 19:07:05 +0000 515) validateDateNotNull(date); +5292526e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:09:45 +0000 516) final Calendar c = Calendar.getInstance(); +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 517) c.setTime(date); +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 518) c.add(calendarField, amount); +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 519) return c.getTime(); +72f58cfd2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:29:57 +0000 520) } + 521) +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 522) //----------------------------------------------------------------------- +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 523) /** +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 524) * Sets the years field to a date returning a new object. +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 525) * The original {@code Date} is unchanged. +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 526) * +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 527) * @param date the date, not null +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 528) * @param amount the amount to set +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 529) * @return a new {@code Date} set with the specified value +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 530) * @throws IllegalArgumentException if the date is null +987884580 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:42:51 +0000 531) * @since 2.4 +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 532) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 533) public static Date setYears(final Date date, final int amount) { +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 534) return set(date, Calendar.YEAR, amount); +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 535) } + 536) +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 537) //----------------------------------------------------------------------- +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 538) /** +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 539) * Sets the months field to a date returning a new object. +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 540) * The original {@code Date} is unchanged. +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 541) * +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 542) * @param date the date, not null +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 543) * @param amount the amount to set +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 544) * @return a new {@code Date} set with the specified value +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 545) * @throws IllegalArgumentException if the date is null +987884580 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:42:51 +0000 546) * @since 2.4 +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 547) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 548) public static Date setMonths(final Date date, final int amount) { +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 549) return set(date, Calendar.MONTH, amount); +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 550) } + 551) +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 552) //----------------------------------------------------------------------- +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 553) /** +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 554) * Sets the day of month field to a date returning a new object. +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 555) * The original {@code Date} is unchanged. +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 556) * +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 557) * @param date the date, not null +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 558) * @param amount the amount to set +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 559) * @return a new {@code Date} set with the specified value +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 560) * @throws IllegalArgumentException if the date is null +987884580 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:42:51 +0000 561) * @since 2.4 +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 562) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 563) public static Date setDays(final Date date, final int amount) { +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 564) return set(date, Calendar.DAY_OF_MONTH, amount); +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 565) } + 566) +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 567) //----------------------------------------------------------------------- +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 568) /** +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 569) * Sets the hours field to a date returning a new object. Hours range +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 570) * from 0-23. +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 571) * The original {@code Date} is unchanged. +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 572) * +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 573) * @param date the date, not null +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 574) * @param amount the amount to set +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 575) * @return a new {@code Date} set with the specified value +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 576) * @throws IllegalArgumentException if the date is null +987884580 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:42:51 +0000 577) * @since 2.4 +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 578) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 579) public static Date setHours(final Date date, final int amount) { +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 580) return set(date, Calendar.HOUR_OF_DAY, amount); +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 581) } + 582) +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 583) //----------------------------------------------------------------------- +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 584) /** +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 585) * Sets the minute field to a date returning a new object. +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 586) * The original {@code Date} is unchanged. +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 587) * +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 588) * @param date the date, not null +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 589) * @param amount the amount to set +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 590) * @return a new {@code Date} set with the specified value +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 591) * @throws IllegalArgumentException if the date is null +987884580 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:42:51 +0000 592) * @since 2.4 +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 593) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 594) public static Date setMinutes(final Date date, final int amount) { +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 595) return set(date, Calendar.MINUTE, amount); +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 596) } + 597) +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 598) //----------------------------------------------------------------------- +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 599) /** +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 600) * Sets the seconds field to a date returning a new object. +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 601) * The original {@code Date} is unchanged. +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 602) * +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 603) * @param date the date, not null +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 604) * @param amount the amount to set +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 605) * @return a new {@code Date} set with the specified value +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 606) * @throws IllegalArgumentException if the date is null +987884580 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:42:51 +0000 607) * @since 2.4 +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 608) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 609) public static Date setSeconds(final Date date, final int amount) { +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 610) return set(date, Calendar.SECOND, amount); +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 611) } + 612) +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 613) //----------------------------------------------------------------------- +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 614) /** +a6d27fd89 src/main/java/org/apache/commons/lang3/time/DateUtils.java (ggregory 2016-04-13 19:26:24 +0000 615) * Sets the milliseconds field to a date returning a new object. +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 616) * The original {@code Date} is unchanged. +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 617) * +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 618) * @param date the date, not null +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 619) * @param amount the amount to set +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 620) * @return a new {@code Date} set with the specified value +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 621) * @throws IllegalArgumentException if the date is null +987884580 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:42:51 +0000 622) * @since 2.4 +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 623) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 624) public static Date setMilliseconds(final Date date, final int amount) { +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 625) return set(date, Calendar.MILLISECOND, amount); +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 626) } + 627) +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 628) //----------------------------------------------------------------------- +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 629) /** +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 630) * Sets the specified field to a date returning a new object. +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 631) * This does not use a lenient calendar. +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 632) * The original {@code Date} is unchanged. +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 633) * +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 634) * @param date the date, not null +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 635) * @param calendarField the {@code Calendar} field to set the amount to +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 636) * @param amount the amount to set +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 637) * @return a new {@code Date} set with the specified value +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 638) * @throws IllegalArgumentException if the date is null +987884580 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:42:51 +0000 639) * @since 2.4 +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 640) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 641) private static Date set(final Date date, final int calendarField, final int amount) { +ff6b0ada7 src/main/java/org/apache/commons/lang3/time/DateUtils.java (pascalschumacher 2017-04-19 19:07:05 +0000 642) validateDateNotNull(date); +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 643) // getInstance() returns a new object, so this method is thread safe. +5292526e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:09:45 +0000 644) final Calendar c = Calendar.getInstance(); +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 645) c.setLenient(false); +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 646) c.setTime(date); +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 647) c.set(calendarField, amount); +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 648) return c.getTime(); +85dda0d25 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-01-10 04:41:22 +0000 649) } + 650) +434575ed0 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2010-07-13 05:17:48 +0000 651) //----------------------------------------------------------------------- +434575ed0 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2010-07-13 05:17:48 +0000 652) /** +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 653) * Converts a {@code Date} into a {@code Calendar}. +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 654) * +434575ed0 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2010-07-13 05:17:48 +0000 655) * @param date the date to convert to a Calendar +434575ed0 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2010-07-13 05:17:48 +0000 656) * @return the created Calendar +434575ed0 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2010-07-13 05:17:48 +0000 657) * @throws NullPointerException if null is passed in +711337c3a src/main/java/org/apache/commons/lang3/time/DateUtils.java (Joerg Schaible 2010-08-12 06:55:49 +0000 658) * @since 3.0 +434575ed0 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2010-07-13 05:17:48 +0000 659) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 660) public static Calendar toCalendar(final Date date) { +5292526e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:09:45 +0000 661) final Calendar c = Calendar.getInstance(); +434575ed0 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2010-07-13 05:17:48 +0000 662) c.setTime(date); +434575ed0 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2010-07-13 05:17:48 +0000 663) return c; +434575ed0 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2010-07-13 05:17:48 +0000 664) } + 665) +1eecfc948 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Kaiyuan Wang 2016-09-21 04:58:36 +0000 666) //----------------------------------------------------------------------- +1eecfc948 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Kaiyuan Wang 2016-09-21 04:58:36 +0000 667) /** +1eecfc948 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Kaiyuan Wang 2016-09-21 04:58:36 +0000 668) * Converts a {@code Date} of a given {@code TimeZone} into a {@code Calendar} +1eecfc948 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Kaiyuan Wang 2016-09-21 04:58:36 +0000 669) * @param date the date to convert to a Calendar +ebcef2d16 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Kürten 2017-02-03 19:00:04 +0000 670) * @param tz the time zone of the {@code date} +d9a2c69a9 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Kaiyuan Wang 2016-09-24 15:01:41 +0000 671) * @return the created Calendar +d9a2c69a9 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Kaiyuan Wang 2016-09-24 15:01:41 +0000 672) * @throws NullPointerException if {@code date} or {@code tz} is null +1eecfc948 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Kaiyuan Wang 2016-09-21 04:58:36 +0000 673) */ +1eecfc948 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Kaiyuan Wang 2016-09-21 04:58:36 +0000 674) public static Calendar toCalendar(final Date date, final TimeZone tz) { +1eecfc948 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Kaiyuan Wang 2016-09-21 04:58:36 +0000 675) final Calendar c = Calendar.getInstance(tz); +1eecfc948 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Kaiyuan Wang 2016-09-21 04:58:36 +0000 676) c.setTime(date); +1eecfc948 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Kaiyuan Wang 2016-09-21 04:58:36 +0000 677) return c; +1eecfc948 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Kaiyuan Wang 2016-09-21 04:58:36 +0000 678) } + 679) +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 680) //----------------------------------------------------------------------- +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 681) /** +d79d88439 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-24 12:48:59 +0000 682) *

Rounds a date, leaving the field specified as the most +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 683) * significant field.

+eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 684) * +b27adc258 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-24 12:42:44 +0000 685) *

For example, if you had the date-time of 28 Mar 2002 +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 686) * 13:45:01.231, if this was passed with HOUR, it would return +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 687) * 28 Mar 2002 14:00:00.000. If this was passed with MONTH, it +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 688) * would return 1 April 2002 0:00:00.000.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 689) * +c4df48e56 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-05 17:54:48 +0000 690) *

For a date in a timezone that handles the change to daylight +0016bcc38 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-05 18:07:44 +0000 691) * saving time, rounding to Calendar.HOUR_OF_DAY will behave as follows. +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 692) * Suppose daylight saving time begins at 02:00 on March 30. Rounding a +c4df48e56 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-05 17:54:48 +0000 693) * date that crosses this time would produce the following values: +0e2e37bb8 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Niall Pemberton 2014-03-31 22:54:57 +0000 694) *

+c4df48e56 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-05 17:54:48 +0000 695) *
    +c4df48e56 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-05 17:54:48 +0000 696) *
  • March 30, 2003 01:10 rounds to March 30, 2003 01:00
  • +c4df48e56 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-05 17:54:48 +0000 697) *
  • March 30, 2003 01:40 rounds to March 30, 2003 03:00
  • +c4df48e56 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-05 17:54:48 +0000 698) *
  • March 30, 2003 02:10 rounds to March 30, 2003 03:00
  • +c4df48e56 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-05 17:54:48 +0000 699) *
  • March 30, 2003 02:40 rounds to March 30, 2003 04:00
  • +c4df48e56 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-05 17:54:48 +0000 700) *
+73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 701) * +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 702) * @param date the date to work with, not null +dbd36d9f5 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2011-03-04 17:31:27 +0000 703) * @param field the field from {@code Calendar} or {@code SEMI_MONTH} +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 704) * @return the different rounded date, not null +d19d4a852 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:43:05 +0000 705) * @throws ArithmeticException if the year is over 280 million +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 706) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 707) public static Date round(final Date date, final int field) { +ff6b0ada7 src/main/java/org/apache/commons/lang3/time/DateUtils.java (pascalschumacher 2017-04-19 19:07:05 +0000 708) validateDateNotNull(date); +5292526e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:09:45 +0000 709) final Calendar gval = Calendar.getInstance(); +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 710) gval.setTime(date); +52e854e3b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-27 15:03:26 +0000 711) modify(gval, field, ModifyType.ROUND); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 712) return gval.getTime(); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 713) } + 714) +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 715) /** +d79d88439 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-24 12:48:59 +0000 716) *

Rounds a date, leaving the field specified as the most +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 717) * significant field.

+eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 718) * +b27adc258 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-24 12:42:44 +0000 719) *

For example, if you had the date-time of 28 Mar 2002 +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 720) * 13:45:01.231, if this was passed with HOUR, it would return +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 721) * 28 Mar 2002 14:00:00.000. If this was passed with MONTH, it +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 722) * would return 1 April 2002 0:00:00.000.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 723) * +c4df48e56 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-05 17:54:48 +0000 724) *

For a date in a timezone that handles the change to daylight +0016bcc38 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-05 18:07:44 +0000 725) * saving time, rounding to Calendar.HOUR_OF_DAY will behave as follows. +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 726) * Suppose daylight saving time begins at 02:00 on March 30. Rounding a +c4df48e56 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-05 17:54:48 +0000 727) * date that crosses this time would produce the following values: +0e2e37bb8 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Niall Pemberton 2014-03-31 22:54:57 +0000 728) *

+c4df48e56 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-05 17:54:48 +0000 729) *
    +c4df48e56 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-05 17:54:48 +0000 730) *
  • March 30, 2003 01:10 rounds to March 30, 2003 01:00
  • +c4df48e56 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-05 17:54:48 +0000 731) *
  • March 30, 2003 01:40 rounds to March 30, 2003 03:00
  • +c4df48e56 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-05 17:54:48 +0000 732) *
  • March 30, 2003 02:10 rounds to March 30, 2003 03:00
  • +c4df48e56 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-05 17:54:48 +0000 733) *
  • March 30, 2003 02:40 rounds to March 30, 2003 04:00
  • +c4df48e56 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-05 17:54:48 +0000 734) *
+73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 735) * +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 736) * @param date the date to work with, not null +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 737) * @param field the field from {@code Calendar} or SEMI_MONTH +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 738) * @return the different rounded date, not null +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 739) * @throws IllegalArgumentException if the date is null +d19d4a852 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:43:05 +0000 740) * @throws ArithmeticException if the year is over 280 million +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 741) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 742) public static Calendar round(final Calendar date, final int field) { +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 743) if (date == null) { +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 744) throw new IllegalArgumentException("The date must not be null"); +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 745) } +5292526e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:09:45 +0000 746) final Calendar rounded = (Calendar) date.clone(); +52e854e3b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-27 15:03:26 +0000 747) modify(rounded, field, ModifyType.ROUND); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 748) return rounded; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 749) } + 750) +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 751) /** +d79d88439 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-24 12:48:59 +0000 752) *

Rounds a date, leaving the field specified as the most +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 753) * significant field.

+eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 754) * +b27adc258 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-24 12:42:44 +0000 755) *

For example, if you had the date-time of 28 Mar 2002 +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 756) * 13:45:01.231, if this was passed with HOUR, it would return +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 757) * 28 Mar 2002 14:00:00.000. If this was passed with MONTH, it +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 758) * would return 1 April 2002 0:00:00.000.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 759) * +c4df48e56 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-05 17:54:48 +0000 760) *

For a date in a timezone that handles the change to daylight +0016bcc38 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-05 18:07:44 +0000 761) * saving time, rounding to Calendar.HOUR_OF_DAY will behave as follows. +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 762) * Suppose daylight saving time begins at 02:00 on March 30. Rounding a +c4df48e56 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-05 17:54:48 +0000 763) * date that crosses this time would produce the following values: +0e2e37bb8 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Niall Pemberton 2014-03-31 22:54:57 +0000 764) *

+c4df48e56 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-05 17:54:48 +0000 765) *
    +c4df48e56 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-05 17:54:48 +0000 766) *
  • March 30, 2003 01:10 rounds to March 30, 2003 01:00
  • +c4df48e56 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-05 17:54:48 +0000 767) *
  • March 30, 2003 01:40 rounds to March 30, 2003 03:00
  • +c4df48e56 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-05 17:54:48 +0000 768) *
  • March 30, 2003 02:10 rounds to March 30, 2003 03:00
  • +c4df48e56 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-05 17:54:48 +0000 769) *
  • March 30, 2003 02:40 rounds to March 30, 2003 04:00
  • +c4df48e56 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-05 17:54:48 +0000 770) *
+73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 771) * +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 772) * @param date the date to work with, either {@code Date} or {@code Calendar}, not null +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 773) * @param field the field from {@code Calendar} or SEMI_MONTH +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 774) * @return the different rounded date, not null +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 775) * @throws IllegalArgumentException if the date is null +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 776) * @throws ClassCastException if the object type is not a {@code Date} or {@code Calendar} +d19d4a852 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:43:05 +0000 777) * @throws ArithmeticException if the year is over 280 million +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 778) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 779) public static Date round(final Object date, final int field) { +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 780) if (date == null) { +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 781) throw new IllegalArgumentException("The date must not be null"); +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 782) } +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 783) if (date instanceof Date) { +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 784) return round((Date) date, field); +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 785) } else if (date instanceof Calendar) { +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 786) return round((Calendar) date, field).getTime(); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 787) } else { +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 788) throw new ClassCastException("Could not round " + date); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 789) } +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 790) } + 791) +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 792) //----------------------------------------------------------------------- +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 793) /** +d79d88439 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-24 12:48:59 +0000 794) *

Truncates a date, leaving the field specified as the most +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 795) * significant field.

+eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 796) * +b27adc258 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-24 12:42:44 +0000 797) *

For example, if you had the date-time of 28 Mar 2002 +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 798) * 13:45:01.231, if you passed with HOUR, it would return 28 Mar +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 799) * 2002 13:00:00.000. If this was passed with MONTH, it would +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 800) * return 1 Mar 2002 0:00:00.000.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 801) * +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 802) * @param date the date to work with, not null +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 803) * @param field the field from {@code Calendar} or SEMI_MONTH +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 804) * @return the different truncated date, not null +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 805) * @throws IllegalArgumentException if the date is null +d19d4a852 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:43:05 +0000 806) * @throws ArithmeticException if the year is over 280 million +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 807) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 808) public static Date truncate(final Date date, final int field) { +ff6b0ada7 src/main/java/org/apache/commons/lang3/time/DateUtils.java (pascalschumacher 2017-04-19 19:07:05 +0000 809) validateDateNotNull(date); +5292526e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:09:45 +0000 810) final Calendar gval = Calendar.getInstance(); +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 811) gval.setTime(date); +52e854e3b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-27 15:03:26 +0000 812) modify(gval, field, ModifyType.TRUNCATE); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 813) return gval.getTime(); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 814) } + 815) +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 816) /** +d79d88439 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-24 12:48:59 +0000 817) *

Truncates a date, leaving the field specified as the most +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 818) * significant field.

+eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 819) * +b27adc258 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-24 12:42:44 +0000 820) *

For example, if you had the date-time of 28 Mar 2002 +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 821) * 13:45:01.231, if you passed with HOUR, it would return 28 Mar +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 822) * 2002 13:00:00.000. If this was passed with MONTH, it would +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 823) * return 1 Mar 2002 0:00:00.000.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 824) * +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 825) * @param date the date to work with, not null +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 826) * @param field the field from {@code Calendar} or SEMI_MONTH +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 827) * @return the different truncated date, not null +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 828) * @throws IllegalArgumentException if the date is null +d19d4a852 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:43:05 +0000 829) * @throws ArithmeticException if the year is over 280 million +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 830) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 831) public static Calendar truncate(final Calendar date, final int field) { +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 832) if (date == null) { +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 833) throw new IllegalArgumentException("The date must not be null"); +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 834) } +5292526e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:09:45 +0000 835) final Calendar truncated = (Calendar) date.clone(); +52e854e3b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-27 15:03:26 +0000 836) modify(truncated, field, ModifyType.TRUNCATE); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 837) return truncated; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 838) } + 839) +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 840) /** +d79d88439 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-24 12:48:59 +0000 841) *

Truncates a date, leaving the field specified as the most +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 842) * significant field.

+eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 843) * +b27adc258 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-24 12:42:44 +0000 844) *

For example, if you had the date-time of 28 Mar 2002 +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 845) * 13:45:01.231, if you passed with HOUR, it would return 28 Mar +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 846) * 2002 13:00:00.000. If this was passed with MONTH, it would +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 847) * return 1 Mar 2002 0:00:00.000.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 848) * +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 849) * @param date the date to work with, either {@code Date} or {@code Calendar}, not null +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 850) * @param field the field from {@code Calendar} or SEMI_MONTH +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 851) * @return the different truncated date, not null +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 852) * @throws IllegalArgumentException if the date is null +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 853) * @throws ClassCastException if the object type is not a {@code Date} or {@code Calendar} +d19d4a852 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:43:05 +0000 854) * @throws ArithmeticException if the year is over 280 million +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 855) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 856) public static Date truncate(final Object date, final int field) { +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 857) if (date == null) { +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 858) throw new IllegalArgumentException("The date must not be null"); +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 859) } +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 860) if (date instanceof Date) { +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 861) return truncate((Date) date, field); +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 862) } else if (date instanceof Calendar) { +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 863) return truncate((Calendar) date, field).getTime(); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 864) } else { +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 865) throw new ClassCastException("Could not truncate " + date); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 866) } +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 867) } + 868) +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 869) //----------------------------------------------------------------------- +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 870) /** +d79d88439 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-24 12:48:59 +0000 871) *

Gets a date ceiling, leaving the field specified as the most +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 872) * significant field.

+49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 873) * +b27adc258 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-24 12:42:44 +0000 874) *

For example, if you had the date-time of 28 Mar 2002 +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 875) * 13:45:01.231, if you passed with HOUR, it would return 28 Mar +9c6d3ea78 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2011-01-23 05:25:03 +0000 876) * 2002 14:00:00.000. If this was passed with MONTH, it would +9c6d3ea78 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2011-01-23 05:25:03 +0000 877) * return 1 Apr 2002 0:00:00.000.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 878) * +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 879) * @param date the date to work with, not null +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 880) * @param field the field from {@code Calendar} or SEMI_MONTH +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 881) * @return the different ceil date, not null +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 882) * @throws IllegalArgumentException if the date is null +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 883) * @throws ArithmeticException if the year is over 280 million +43fca0d74 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Niall Pemberton 2010-02-25 01:28:13 +0000 884) * @since 2.5 +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 885) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 886) public static Date ceiling(final Date date, final int field) { +ff6b0ada7 src/main/java/org/apache/commons/lang3/time/DateUtils.java (pascalschumacher 2017-04-19 19:07:05 +0000 887) validateDateNotNull(date); +5292526e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:09:45 +0000 888) final Calendar gval = Calendar.getInstance(); +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 889) gval.setTime(date); +52e854e3b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-27 15:03:26 +0000 890) modify(gval, field, ModifyType.CEILING); +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 891) return gval.getTime(); +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 892) } + 893) +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 894) /** +d79d88439 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-24 12:48:59 +0000 895) *

Gets a date ceiling, leaving the field specified as the most +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 896) * significant field.

+49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 897) * +b27adc258 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-24 12:42:44 +0000 898) *

For example, if you had the date-time of 28 Mar 2002 +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 899) * 13:45:01.231, if you passed with HOUR, it would return 28 Mar +575965b1b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-24 12:52:56 +0000 900) * 2002 14:00:00.000. If this was passed with MONTH, it would +575965b1b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-24 12:52:56 +0000 901) * return 1 Apr 2002 0:00:00.000.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 902) * +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 903) * @param date the date to work with, not null +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 904) * @param field the field from {@code Calendar} or SEMI_MONTH +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 905) * @return the different ceil date, not null +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 906) * @throws IllegalArgumentException if the date is null +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 907) * @throws ArithmeticException if the year is over 280 million +43fca0d74 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Niall Pemberton 2010-02-25 01:28:13 +0000 908) * @since 2.5 +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 909) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 910) public static Calendar ceiling(final Calendar date, final int field) { +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 911) if (date == null) { +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 912) throw new IllegalArgumentException("The date must not be null"); +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 913) } +5292526e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:09:45 +0000 914) final Calendar ceiled = (Calendar) date.clone(); +52e854e3b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-27 15:03:26 +0000 915) modify(ceiled, field, ModifyType.CEILING); +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 916) return ceiled; +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 917) } + 918) +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 919) /** +d79d88439 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-24 12:48:59 +0000 920) *

Gets a date ceiling, leaving the field specified as the most +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 921) * significant field.

+49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 922) * +b27adc258 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-24 12:42:44 +0000 923) *

For example, if you had the date-time of 28 Mar 2002 +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 924) * 13:45:01.231, if you passed with HOUR, it would return 28 Mar +575965b1b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-24 12:52:56 +0000 925) * 2002 14:00:00.000. If this was passed with MONTH, it would +575965b1b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-24 12:52:56 +0000 926) * return 1 Apr 2002 0:00:00.000.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 927) * +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 928) * @param date the date to work with, either {@code Date} or {@code Calendar}, not null +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 929) * @param field the field from {@code Calendar} or SEMI_MONTH +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 930) * @return the different ceil date, not null +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 931) * @throws IllegalArgumentException if the date is null +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 932) * @throws ClassCastException if the object type is not a {@code Date} or {@code Calendar} +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 933) * @throws ArithmeticException if the year is over 280 million +43fca0d74 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Niall Pemberton 2010-02-25 01:28:13 +0000 934) * @since 2.5 +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 935) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 936) public static Date ceiling(final Object date, final int field) { +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 937) if (date == null) { +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 938) throw new IllegalArgumentException("The date must not be null"); +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 939) } +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 940) if (date instanceof Date) { +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 941) return ceiling((Date) date, field); +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 942) } else if (date instanceof Calendar) { +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 943) return ceiling((Calendar) date, field).getTime(); +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 944) } else { +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 945) throw new ClassCastException("Could not find ceiling of for type: " + date.getClass()); +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 946) } +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 947) } + 948) +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 949) //----------------------------------------------------------------------- +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 950) /** +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 951) *

Internal calculation method.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 952) * +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 953) * @param val the calendar, not null +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 954) * @param field the field constant +49e68a3f9 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2009-07-19 19:21:04 +0000 955) * @param modType type to truncate, round or ceiling +d19d4a852 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:43:05 +0000 956) * @throws ArithmeticException if the year is over 280 million +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 957) */ +52e854e3b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-27 15:03:26 +0000 958) private static void modify(final Calendar val, final int field, final ModifyType modType) { +d19d4a852 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:43:05 +0000 959) if (val.get(Calendar.YEAR) > 280000000) { +d19d4a852 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:43:05 +0000 960) throw new ArithmeticException("Calendar value too large for accurate calculations"); +d19d4a852 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2004-10-16 17:43:05 +0000 961) } + 962) +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 963) if (field == Calendar.MILLISECOND) { +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 964) return; +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 965) } + 966) +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 967) // ----------------- Fix for LANG-59 ---------------------- START --------------- +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 968) // see http://issues.apache.org/jira/browse/LANG-59 +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 969) // +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 970) // Manually truncate milliseconds, seconds and minutes, rather than using +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 971) // Calendar methods. + 972) +5292526e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:09:45 +0000 973) final Date date = val.getTime(); +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 974) long time = date.getTime(); +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 975) boolean done = false; + 976) +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 977) // truncate milliseconds +5292526e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:09:45 +0000 978) final int millisecs = val.get(Calendar.MILLISECOND); +52e854e3b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-27 15:03:26 +0000 979) if (ModifyType.TRUNCATE == modType || millisecs < 500) { +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 980) time = time - millisecs; +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 981) } +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 982) if (field == Calendar.SECOND) { +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 983) done = true; +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 984) } + 985) +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 986) // truncate seconds +5292526e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:09:45 +0000 987) final int seconds = val.get(Calendar.SECOND); +52e854e3b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-27 15:03:26 +0000 988) if (!done && (ModifyType.TRUNCATE == modType || seconds < 30)) { +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 989) time = time - (seconds * 1000L); +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 990) } +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 991) if (field == Calendar.MINUTE) { +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 992) done = true; +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 993) } + 994) +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 995) // truncate minutes +5292526e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:09:45 +0000 996) final int minutes = val.get(Calendar.MINUTE); +52e854e3b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-09-27 15:03:26 +0000 997) if (!done && (ModifyType.TRUNCATE == modType || minutes < 30)) { +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 998) time = time - (minutes * 60000L); +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 999) } + 1000) +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 1001) // reset time +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 1002) if (date.getTime() != time) { +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 1003) date.setTime(time); +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 1004) val.setTime(date); +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 1005) } +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 1006) // ----------------- Fix for LANG-59 ----------------------- END ---------------- + 1007) +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1008) boolean roundUp = false; +5292526e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:09:45 +0000 1009) for (final int[] aField : fields) { +5292526e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:09:45 +0000 1010) for (final int element : aField) { +14373ad1b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2011-07-11 00:49:04 +0000 1011) if (element == field) { +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1012) //This is our field... we stop looping +35f1cc002 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2015-04-06 19:48:22 +0000 1013) if (modType == ModifyType.CEILING || modType == ModifyType.ROUND && roundUp) { +b610707cd src/main/java/org/apache/commons/lang3/time/DateUtils.java (Igor Curdvanovschi 2018-06-20 11:28:10 +0000 1014) if (field == SEMI_MONTH) { +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1015) //This is a special case that's hard to generalize +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1016) //If the date is 1, we round up to 16, otherwise +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1017) // we subtract 15 days and add 1 month +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1018) if (val.get(Calendar.DATE) == 1) { +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1019) val.add(Calendar.DATE, 15); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1020) } else { +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1021) val.add(Calendar.DATE, -15); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1022) val.add(Calendar.MONTH, 1); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1023) } +db5c7106a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-06-09 07:17:24 +0000 1024) // ----------------- Fix for LANG-440 ---------------------- START --------------- +db5c7106a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-06-09 07:17:24 +0000 1025) } else if (field == Calendar.AM_PM) { +db5c7106a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-06-09 07:17:24 +0000 1026) // This is a special case +db5c7106a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-06-09 07:17:24 +0000 1027) // If the time is 0, we round up to 12, otherwise +db5c7106a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-06-09 07:17:24 +0000 1028) // we subtract 12 hours and add 1 day +db5c7106a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-06-09 07:17:24 +0000 1029) if (val.get(Calendar.HOUR_OF_DAY) == 0) { +db5c7106a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-06-09 07:17:24 +0000 1030) val.add(Calendar.HOUR_OF_DAY, 12); +db5c7106a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-06-09 07:17:24 +0000 1031) } else { +db5c7106a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-06-09 07:17:24 +0000 1032) val.add(Calendar.HOUR_OF_DAY, -12); +db5c7106a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-06-09 07:17:24 +0000 1033) val.add(Calendar.DATE, 1); +db5c7106a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-06-09 07:17:24 +0000 1034) } +db5c7106a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-06-09 07:17:24 +0000 1035) // ----------------- Fix for LANG-440 ---------------------- END --------------- +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1036) } else { +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1037) //We need at add one to this field since the +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1038) // last number causes us to round up +fab64bbdc src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2011-07-10 18:26:16 +0000 1039) val.add(aField[0], 1); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1040) } +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1041) } +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1042) return; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1043) } +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1044) } +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1045) //We have various fields that are not easy roundings +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1046) int offset = 0; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1047) boolean offsetSet = false; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1048) //These are special types of fields that require different rounding rules +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1049) switch (field) { +b610707cd src/main/java/org/apache/commons/lang3/time/DateUtils.java (Igor Curdvanovschi 2018-06-20 11:28:10 +0000 1050) case SEMI_MONTH: +fab64bbdc src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2011-07-10 18:26:16 +0000 1051) if (aField[0] == Calendar.DATE) { +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1052) //If we're going to drop the DATE field's value, +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1053) // we want to do this our own way. +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1054) //We need to subtrace 1 since the date has a minimum of 1 +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1055) offset = val.get(Calendar.DATE) - 1; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1056) //If we're above 15 days adjustment, that means we're in the +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1057) // bottom half of the month and should stay accordingly. +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1058) if (offset >= 15) { +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1059) offset -= 15; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1060) } +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1061) //Record whether we're in the top or bottom half of that range +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1062) roundUp = offset > 7; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1063) offsetSet = true; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1064) } +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1065) break; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1066) case Calendar.AM_PM: +fab64bbdc src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2011-07-10 18:26:16 +0000 1067) if (aField[0] == Calendar.HOUR_OF_DAY) { +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1068) //If we're going to drop the HOUR field's value, +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1069) // we want to do this our own way. +0c40162c2 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2004-07-12 00:04:32 +0000 1070) offset = val.get(Calendar.HOUR_OF_DAY); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1071) if (offset >= 12) { +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1072) offset -= 12; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1073) } +a6a2d0487 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-11-05 17:00:43 +0000 1074) roundUp = offset >= 6; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1075) offsetSet = true; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1076) } +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1077) break; +d6a556dad src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2014-02-06 13:51:45 +0000 1078) default: +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1079) break; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1080) } +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1081) if (!offsetSet) { +5292526e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:09:45 +0000 1082) final int min = val.getActualMinimum(aField[0]); +5292526e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:09:45 +0000 1083) final int max = val.getActualMaximum(aField[0]); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1084) //Calculate the offset from the minimum allowed value +fab64bbdc src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2011-07-10 18:26:16 +0000 1085) offset = val.get(aField[0]) - min; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1086) //Set roundUp if this is more than half way between the minimum and maximum +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1087) roundUp = offset > ((max - min) / 2); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1088) } +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1089) //We need to remove this field +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 1090) if (offset != 0) { +fab64bbdc src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2011-07-10 18:26:16 +0000 1091) val.set(aField[0], val.get(aField[0]) - offset); +9cbf70d82 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2006-07-21 05:47:07 +0000 1092) } +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1093) } +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1094) throw new IllegalArgumentException("The field " + field + " is not supported"); + 1095) +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1096) } + 1097) +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1098) //----------------------------------------------------------------------- +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1099) /** +d79d88439 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-24 12:48:59 +0000 1100) *

Constructs an Iterator over each day in a date +12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1101) * range defined by a focus date and range style.

+12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1102) * +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 1103) *

For instance, passing Thursday, July 4, 2002 and a +12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1104) * RANGE_MONTH_SUNDAY will return an Iterator +12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1105) * that starts with Sunday, June 30, 2002 and ends with Saturday, August 3, +12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1106) * 2002, returning a Calendar instance for each intermediate day.

+73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1107) * +12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1108) *

This method provides an iterator that returns Calendar objects. +12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1109) * The days are progressed using {@link Calendar#add(int, int)}.

+73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1110) * +12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1111) * @param focus the date to work with, not null +12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1112) * @param rangeStyle the style constant to use. Must be one of +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1113) * {@link DateUtils#RANGE_MONTH_SUNDAY}, +12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1114) * {@link DateUtils#RANGE_MONTH_MONDAY}, +12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1115) * {@link DateUtils#RANGE_WEEK_SUNDAY}, +12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1116) * {@link DateUtils#RANGE_WEEK_MONDAY}, +12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1117) * {@link DateUtils#RANGE_WEEK_RELATIVE}, +12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1118) * {@link DateUtils#RANGE_WEEK_CENTER} +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 1119) * @return the date iterator, not null, not null +12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1120) * @throws IllegalArgumentException if the date is null +12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1121) * @throws IllegalArgumentException if the rangeStyle is invalid +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1122) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 1123) public static Iterator iterator(final Date focus, final int rangeStyle) { +ff6b0ada7 src/main/java/org/apache/commons/lang3/time/DateUtils.java (pascalschumacher 2017-04-19 19:07:05 +0000 1124) validateDateNotNull(focus); +5292526e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:09:45 +0000 1125) final Calendar gval = Calendar.getInstance(); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1126) gval.setTime(focus); +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1127) return iterator(gval, rangeStyle); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1128) } + 1129) +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1130) /** +d79d88439 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-24 12:48:59 +0000 1131) *

Constructs an Iterator over each day in a date +12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1132) * range defined by a focus date and range style.

+eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 1133) * +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 1134) *

For instance, passing Thursday, July 4, 2002 and a +12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1135) * RANGE_MONTH_SUNDAY will return an Iterator +12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1136) * that starts with Sunday, June 30, 2002 and ends with Saturday, August 3, +12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1137) * 2002, returning a Calendar instance for each intermediate day.

+12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1138) * +12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1139) *

This method provides an iterator that returns Calendar objects. +12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1140) * The days are progressed using {@link Calendar#add(int, int)}.

+12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1141) * +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 1142) * @param focus the date to work with, not null +0b3fe95c2 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2003-08-04 00:19:43 +0000 1143) * @param rangeStyle the style constant to use. Must be one of +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1144) * {@link DateUtils#RANGE_MONTH_SUNDAY}, +f491166ad src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2003-08-05 21:15:49 +0000 1145) * {@link DateUtils#RANGE_MONTH_MONDAY}, +f491166ad src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2003-08-05 21:15:49 +0000 1146) * {@link DateUtils#RANGE_WEEK_SUNDAY}, +f491166ad src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2003-08-05 21:15:49 +0000 1147) * {@link DateUtils#RANGE_WEEK_MONDAY}, +f491166ad src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2003-08-05 21:15:49 +0000 1148) * {@link DateUtils#RANGE_WEEK_RELATIVE}, +f491166ad src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2003-08-05 21:15:49 +0000 1149) * {@link DateUtils#RANGE_WEEK_CENTER} +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 1150) * @return the date iterator, not null +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 1151) * @throws IllegalArgumentException if the date is null +12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1152) * @throws IllegalArgumentException if the rangeStyle is invalid +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1153) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 1154) public static Iterator iterator(final Calendar focus, final int rangeStyle) { +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1155) if (focus == null) { +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1156) throw new IllegalArgumentException("The date must not be null"); +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1157) } +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1158) Calendar start = null; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1159) Calendar end = null; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1160) int startCutoff = Calendar.SUNDAY; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1161) int endCutoff = Calendar.SATURDAY; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1162) switch (rangeStyle) { +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1163) case RANGE_MONTH_SUNDAY: +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1164) case RANGE_MONTH_MONDAY: +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1165) //Set start to the first of the month +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1166) start = truncate(focus, Calendar.MONTH); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1167) //Set end to the last of the month +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1168) end = (Calendar) start.clone(); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1169) end.add(Calendar.MONTH, 1); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1170) end.add(Calendar.DATE, -1); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1171) //Loop start back to the previous sunday or monday +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1172) if (rangeStyle == RANGE_MONTH_MONDAY) { +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1173) startCutoff = Calendar.MONDAY; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1174) endCutoff = Calendar.SUNDAY; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1175) } +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1176) break; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1177) case RANGE_WEEK_SUNDAY: +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1178) case RANGE_WEEK_MONDAY: +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1179) case RANGE_WEEK_RELATIVE: +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1180) case RANGE_WEEK_CENTER: +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1181) //Set start and end to the current date +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1182) start = truncate(focus, Calendar.DATE); +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1183) end = truncate(focus, Calendar.DATE); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1184) switch (rangeStyle) { +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1185) case RANGE_WEEK_SUNDAY: +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1186) //already set by default +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1187) break; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1188) case RANGE_WEEK_MONDAY: +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1189) startCutoff = Calendar.MONDAY; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1190) endCutoff = Calendar.SUNDAY; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1191) break; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1192) case RANGE_WEEK_RELATIVE: +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1193) startCutoff = focus.get(Calendar.DAY_OF_WEEK); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1194) endCutoff = startCutoff - 1; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1195) break; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1196) case RANGE_WEEK_CENTER: +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1197) startCutoff = focus.get(Calendar.DAY_OF_WEEK) - 3; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1198) endCutoff = focus.get(Calendar.DAY_OF_WEEK) + 3; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1199) break; +d6a556dad src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2014-02-06 13:51:45 +0000 1200) default: +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1201) break; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1202) } +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1203) break; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1204) default: +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1205) throw new IllegalArgumentException("The range style " + rangeStyle + " is not valid."); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1206) } +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1207) if (startCutoff < Calendar.SUNDAY) { +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1208) startCutoff += 7; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1209) } +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1210) if (startCutoff > Calendar.SATURDAY) { +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1211) startCutoff -= 7; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1212) } +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1213) if (endCutoff < Calendar.SUNDAY) { +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1214) endCutoff += 7; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1215) } +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1216) if (endCutoff > Calendar.SATURDAY) { +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1217) endCutoff -= 7; +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1218) } +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1219) while (start.get(Calendar.DAY_OF_WEEK) != startCutoff) { +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1220) start.add(Calendar.DATE, -1); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1221) } +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1222) while (end.get(Calendar.DAY_OF_WEEK) != endCutoff) { +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1223) end.add(Calendar.DATE, 1); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1224) } +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1225) return new DateIterator(start, end); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1226) } + 1227) +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1228) /** +d79d88439 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-24 12:48:59 +0000 1229) *

Constructs an Iterator over each day in a date +12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1230) * range defined by a focus date and range style.

+eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 1231) * +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 1232) *

For instance, passing Thursday, July 4, 2002 and a +12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1233) * RANGE_MONTH_SUNDAY will return an Iterator +12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1234) * that starts with Sunday, June 30, 2002 and ends with Saturday, August 3, +12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1235) * 2002, returning a Calendar instance for each intermediate day.

+12491a680 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2006-04-16 20:55:25 +0000 1236) * +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 1237) * @param focus the date to work with, either {@code Date} or {@code Calendar}, not null +0b3fe95c2 src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2003-08-04 00:19:43 +0000 1238) * @param rangeStyle the style constant to use. Must be one of the range +f491166ad src/java/org/apache/commons/lang/time/DateUtils.java (Steven Caswell 2003-08-05 21:15:49 +0000 1239) * styles listed for the {@link #iterator(Calendar, int)} method. +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 1240) * @return the date iterator, not null +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 1241) * @throws IllegalArgumentException if the date is null +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 1242) * @throws ClassCastException if the object type is not a {@code Date} or {@code Calendar} +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1243) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 1244) public static Iterator iterator(final Object focus, final int rangeStyle) { +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1245) if (focus == null) { +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1246) throw new IllegalArgumentException("The date must not be null"); +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1247) } +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1248) if (focus instanceof Date) { +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1249) return iterator((Date) focus, rangeStyle); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1250) } else if (focus instanceof Calendar) { +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1251) return iterator((Calendar) focus, rangeStyle); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1252) } else { +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1253) throw new ClassCastException("Could not iterate based on " + focus); +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1254) } +73ee6c3d2 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-08 23:14:23 +0000 1255) } + 1256) +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1257) /** +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1258) *

Returns the number of milliseconds within the +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1259) * fragment. All datefields greater than the fragment will be ignored.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1260) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1261) *

Asking the milliseconds of any date will only return the number of milliseconds +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1262) * of the current second (resulting in a number between 0 and 999). This +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1263) * method will retrieve the number of milliseconds for any fragment. +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1264) * For example, if you want to calculate the number of milliseconds past today, +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1265) * your fragment is Calendar.DATE or Calendar.DAY_OF_YEAR. The result will +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1266) * be all milliseconds of the past hour(s), minutes(s) and second(s).

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1267) * +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1268) *

Valid fragments are: Calendar.YEAR, Calendar.MONTH, both +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1269) * Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1270) * Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1271) * A fragment less than or equal to a SECOND field will return 0.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1272) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1273) *
    +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1274) *
  • January 1, 2008 7:15:10.538 with Calendar.SECOND as fragment will return 538
  • +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1275) *
  • January 6, 2008 7:15:10.538 with Calendar.SECOND as fragment will return 538
  • +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1276) *
  • January 6, 2008 7:15:10.538 with Calendar.MINUTE as fragment will return 10538 (10*1000 + 538)
  • +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1277) *
  • January 16, 2008 7:15:10.538 with Calendar.MILLISECOND as fragment will return 0 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1278) * (a millisecond cannot be split in milliseconds)
  • +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1279) *
+16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1280) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1281) * @param date the date to work with, not null +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1282) * @param fragment the {@code Calendar} field part of date to calculate +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1283) * @return number of milliseconds within the fragment of date +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1284) * @throws IllegalArgumentException if the date is null or +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1285) * fragment is not supported +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1286) * @since 2.4 +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1287) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 1288) public static long getFragmentInMilliseconds(final Date date, final int fragment) { +3ee44535b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-03-14 13:23:25 +0000 1289) return getFragment(date, fragment, TimeUnit.MILLISECONDS); +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1290) } + 1291) +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1292) /** +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1293) *

Returns the number of seconds within the +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1294) * fragment. All datefields greater than the fragment will be ignored.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1295) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1296) *

Asking the seconds of any date will only return the number of seconds +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1297) * of the current minute (resulting in a number between 0 and 59). This +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1298) * method will retrieve the number of seconds for any fragment. +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1299) * For example, if you want to calculate the number of seconds past today, +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1300) * your fragment is Calendar.DATE or Calendar.DAY_OF_YEAR. The result will +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1301) * be all seconds of the past hour(s) and minutes(s).

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1302) * +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1303) *

Valid fragments are: Calendar.YEAR, Calendar.MONTH, both +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1304) * Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1305) * Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1306) * A fragment less than or equal to a SECOND field will return 0.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1307) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1308) *
    +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1309) *
  • January 1, 2008 7:15:10.538 with Calendar.MINUTE as fragment will return 10 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1310) * (equivalent to deprecated date.getSeconds())
  • +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1311) *
  • January 6, 2008 7:15:10.538 with Calendar.MINUTE as fragment will return 10 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1312) * (equivalent to deprecated date.getSeconds())
  • +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1313) *
  • January 6, 2008 7:15:10.538 with Calendar.DAY_OF_YEAR as fragment will return 26110 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1314) * (7*3600 + 15*60 + 10)
  • +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1315) *
  • January 16, 2008 7:15:10.538 with Calendar.MILLISECOND as fragment will return 0 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1316) * (a millisecond cannot be split in seconds)
  • +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1317) *
+16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1318) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1319) * @param date the date to work with, not null +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1320) * @param fragment the {@code Calendar} field part of date to calculate +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1321) * @return number of seconds within the fragment of date +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1322) * @throws IllegalArgumentException if the date is null or +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1323) * fragment is not supported +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1324) * @since 2.4 +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1325) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 1326) public static long getFragmentInSeconds(final Date date, final int fragment) { +3ee44535b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-03-14 13:23:25 +0000 1327) return getFragment(date, fragment, TimeUnit.SECONDS); +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1328) } + 1329) +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1330) /** +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1331) *

Returns the number of minutes within the +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1332) * fragment. All datefields greater than the fragment will be ignored.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1333) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1334) *

Asking the minutes of any date will only return the number of minutes +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1335) * of the current hour (resulting in a number between 0 and 59). This +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1336) * method will retrieve the number of minutes for any fragment. +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1337) * For example, if you want to calculate the number of minutes past this month, +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1338) * your fragment is Calendar.MONTH. The result will be all minutes of the +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1339) * past day(s) and hour(s).

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1340) * +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1341) *

Valid fragments are: Calendar.YEAR, Calendar.MONTH, both +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1342) * Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1343) * Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1344) * A fragment less than or equal to a MINUTE field will return 0.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1345) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1346) *
    +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1347) *
  • January 1, 2008 7:15:10.538 with Calendar.HOUR_OF_DAY as fragment will return 15 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1348) * (equivalent to deprecated date.getMinutes())
  • +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1349) *
  • January 6, 2008 7:15:10.538 with Calendar.HOUR_OF_DAY as fragment will return 15 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1350) * (equivalent to deprecated date.getMinutes())
  • +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1351) *
  • January 1, 2008 7:15:10.538 with Calendar.MONTH as fragment will return 15
  • +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1352) *
  • January 6, 2008 7:15:10.538 with Calendar.MONTH as fragment will return 435 (7*60 + 15)
  • +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1353) *
  • January 16, 2008 7:15:10.538 with Calendar.MILLISECOND as fragment will return 0 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1354) * (a millisecond cannot be split in minutes)
  • +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1355) *
+16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1356) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1357) * @param date the date to work with, not null +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1358) * @param fragment the {@code Calendar} field part of date to calculate +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1359) * @return number of minutes within the fragment of date +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1360) * @throws IllegalArgumentException if the date is null or +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1361) * fragment is not supported +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1362) * @since 2.4 +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1363) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 1364) public static long getFragmentInMinutes(final Date date, final int fragment) { +3ee44535b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-03-14 13:23:25 +0000 1365) return getFragment(date, fragment, TimeUnit.MINUTES); +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1366) } + 1367) +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1368) /** +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1369) *

Returns the number of hours within the +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1370) * fragment. All datefields greater than the fragment will be ignored.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1371) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1372) *

Asking the hours of any date will only return the number of hours +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1373) * of the current day (resulting in a number between 0 and 23). This +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1374) * method will retrieve the number of hours for any fragment. +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1375) * For example, if you want to calculate the number of hours past this month, +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1376) * your fragment is Calendar.MONTH. The result will be all hours of the +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1377) * past day(s).

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1378) * +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1379) *

Valid fragments are: Calendar.YEAR, Calendar.MONTH, both +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1380) * Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1381) * Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1382) * A fragment less than or equal to a HOUR field will return 0.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1383) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1384) *
    +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1385) *
  • January 1, 2008 7:15:10.538 with Calendar.DAY_OF_YEAR as fragment will return 7 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1386) * (equivalent to deprecated date.getHours())
  • +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1387) *
  • January 6, 2008 7:15:10.538 with Calendar.DAY_OF_YEAR as fragment will return 7 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1388) * (equivalent to deprecated date.getHours())
  • +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1389) *
  • January 1, 2008 7:15:10.538 with Calendar.MONTH as fragment will return 7
  • +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1390) *
  • January 6, 2008 7:15:10.538 with Calendar.MONTH as fragment will return 127 (5*24 + 7)
  • +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1391) *
  • January 16, 2008 7:15:10.538 with Calendar.MILLISECOND as fragment will return 0 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1392) * (a millisecond cannot be split in hours)
  • +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1393) *
+16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1394) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1395) * @param date the date to work with, not null +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1396) * @param fragment the {@code Calendar} field part of date to calculate +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1397) * @return number of hours within the fragment of date +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1398) * @throws IllegalArgumentException if the date is null or +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1399) * fragment is not supported +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1400) * @since 2.4 +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1401) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 1402) public static long getFragmentInHours(final Date date, final int fragment) { +3ee44535b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-03-14 13:23:25 +0000 1403) return getFragment(date, fragment, TimeUnit.HOURS); +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1404) } + 1405) +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1406) /** +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1407) *

Returns the number of days within the +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1408) * fragment. All datefields greater than the fragment will be ignored.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1409) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1410) *

Asking the days of any date will only return the number of days +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1411) * of the current month (resulting in a number between 1 and 31). This +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1412) * method will retrieve the number of days for any fragment. +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1413) * For example, if you want to calculate the number of days past this year, +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1414) * your fragment is Calendar.YEAR. The result will be all days of the +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1415) * past month(s).

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1416) * +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1417) *

Valid fragments are: Calendar.YEAR, Calendar.MONTH, both +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1418) * Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1419) * Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1420) * A fragment less than or equal to a DAY field will return 0.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1421) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1422) *
    +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1423) *
  • January 28, 2008 with Calendar.MONTH as fragment will return 28 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1424) * (equivalent to deprecated date.getDay())
  • +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1425) *
  • February 28, 2008 with Calendar.MONTH as fragment will return 28 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1426) * (equivalent to deprecated date.getDay())
  • +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1427) *
  • January 28, 2008 with Calendar.YEAR as fragment will return 28
  • +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1428) *
  • February 28, 2008 with Calendar.YEAR as fragment will return 59
  • +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1429) *
  • January 28, 2008 with Calendar.MILLISECOND as fragment will return 0 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1430) * (a millisecond cannot be split in days)
  • +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1431) *
+16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1432) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1433) * @param date the date to work with, not null +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1434) * @param fragment the {@code Calendar} field part of date to calculate +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1435) * @return number of days within the fragment of date +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1436) * @throws IllegalArgumentException if the date is null or +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1437) * fragment is not supported +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1438) * @since 2.4 +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1439) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 1440) public static long getFragmentInDays(final Date date, final int fragment) { +3ee44535b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-03-14 13:23:25 +0000 1441) return getFragment(date, fragment, TimeUnit.DAYS); +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1442) } + 1443) +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1444) /** +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1445) *

Returns the number of milliseconds within the +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1446) * fragment. All datefields greater than the fragment will be ignored.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1447) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1448) *

Asking the milliseconds of any date will only return the number of milliseconds +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1449) * of the current second (resulting in a number between 0 and 999). This +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1450) * method will retrieve the number of milliseconds for any fragment. +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1451) * For example, if you want to calculate the number of seconds past today, +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1452) * your fragment is Calendar.DATE or Calendar.DAY_OF_YEAR. The result will +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1453) * be all seconds of the past hour(s), minutes(s) and second(s).

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1454) * +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1455) *

Valid fragments are: Calendar.YEAR, Calendar.MONTH, both +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1456) * Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1457) * Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1458) * A fragment less than or equal to a MILLISECOND field will return 0.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1459) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1460) *
    +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1461) *
  • January 1, 2008 7:15:10.538 with Calendar.SECOND as fragment will return 538 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1462) * (equivalent to calendar.get(Calendar.MILLISECOND))
  • +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1463) *
  • January 6, 2008 7:15:10.538 with Calendar.SECOND as fragment will return 538 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1464) * (equivalent to calendar.get(Calendar.MILLISECOND))
  • +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1465) *
  • January 6, 2008 7:15:10.538 with Calendar.MINUTE as fragment will return 10538 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1466) * (10*1000 + 538)
  • +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1467) *
  • January 16, 2008 7:15:10.538 with Calendar.MILLISECOND as fragment will return 0 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1468) * (a millisecond cannot be split in milliseconds)
  • +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1469) *
+16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1470) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1471) * @param calendar the calendar to work with, not null +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1472) * @param fragment the {@code Calendar} field part of calendar to calculate +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1473) * @return number of milliseconds within the fragment of date +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1474) * @throws IllegalArgumentException if the date is null or +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1475) * fragment is not supported +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1476) * @since 2.4 +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1477) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 1478) public static long getFragmentInMilliseconds(final Calendar calendar, final int fragment) { +3ee44535b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-03-14 13:23:25 +0000 1479) return getFragment(calendar, fragment, TimeUnit.MILLISECONDS); +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1480) } +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1481) /** +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1482) *

Returns the number of seconds within the +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1483) * fragment. All datefields greater than the fragment will be ignored.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1484) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1485) *

Asking the seconds of any date will only return the number of seconds +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1486) * of the current minute (resulting in a number between 0 and 59). This +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1487) * method will retrieve the number of seconds for any fragment. +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1488) * For example, if you want to calculate the number of seconds past today, +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1489) * your fragment is Calendar.DATE or Calendar.DAY_OF_YEAR. The result will +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1490) * be all seconds of the past hour(s) and minutes(s).

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1491) * +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1492) *

Valid fragments are: Calendar.YEAR, Calendar.MONTH, both +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1493) * Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1494) * Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1495) * A fragment less than or equal to a SECOND field will return 0.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1496) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1497) *
    +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1498) *
  • January 1, 2008 7:15:10.538 with Calendar.MINUTE as fragment will return 10 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1499) * (equivalent to calendar.get(Calendar.SECOND))
  • +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1500) *
  • January 6, 2008 7:15:10.538 with Calendar.MINUTE as fragment will return 10 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1501) * (equivalent to calendar.get(Calendar.SECOND))
  • +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1502) *
  • January 6, 2008 7:15:10.538 with Calendar.DAY_OF_YEAR as fragment will return 26110 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1503) * (7*3600 + 15*60 + 10)
  • +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1504) *
  • January 16, 2008 7:15:10.538 with Calendar.MILLISECOND as fragment will return 0 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1505) * (a millisecond cannot be split in seconds)
  • +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1506) *
+16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1507) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1508) * @param calendar the calendar to work with, not null +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1509) * @param fragment the {@code Calendar} field part of calendar to calculate +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1510) * @return number of seconds within the fragment of date +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1511) * @throws IllegalArgumentException if the date is null or +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1512) * fragment is not supported +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1513) * @since 2.4 +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1514) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 1515) public static long getFragmentInSeconds(final Calendar calendar, final int fragment) { +3ee44535b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-03-14 13:23:25 +0000 1516) return getFragment(calendar, fragment, TimeUnit.SECONDS); +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1517) } + 1518) +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1519) /** +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1520) *

Returns the number of minutes within the +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1521) * fragment. All datefields greater than the fragment will be ignored.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1522) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1523) *

Asking the minutes of any date will only return the number of minutes +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1524) * of the current hour (resulting in a number between 0 and 59). This +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1525) * method will retrieve the number of minutes for any fragment. +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1526) * For example, if you want to calculate the number of minutes past this month, +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1527) * your fragment is Calendar.MONTH. The result will be all minutes of the +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1528) * past day(s) and hour(s).

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1529) * +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1530) *

Valid fragments are: Calendar.YEAR, Calendar.MONTH, both +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1531) * Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1532) * Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1533) * A fragment less than or equal to a MINUTE field will return 0.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1534) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1535) *
    +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1536) *
  • January 1, 2008 7:15:10.538 with Calendar.HOUR_OF_DAY as fragment will return 15 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1537) * (equivalent to calendar.get(Calendar.MINUTES))
  • +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1538) *
  • January 6, 2008 7:15:10.538 with Calendar.HOUR_OF_DAY as fragment will return 15 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1539) * (equivalent to calendar.get(Calendar.MINUTES))
  • +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1540) *
  • January 1, 2008 7:15:10.538 with Calendar.MONTH as fragment will return 15
  • +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1541) *
  • January 6, 2008 7:15:10.538 with Calendar.MONTH as fragment will return 435 (7*60 + 15)
  • +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1542) *
  • January 16, 2008 7:15:10.538 with Calendar.MILLISECOND as fragment will return 0 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1543) * (a millisecond cannot be split in minutes)
  • +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1544) *
+16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1545) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1546) * @param calendar the calendar to work with, not null +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1547) * @param fragment the {@code Calendar} field part of calendar to calculate +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1548) * @return number of minutes within the fragment of date +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1549) * @throws IllegalArgumentException if the date is null or +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1550) * fragment is not supported +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1551) * @since 2.4 +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1552) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 1553) public static long getFragmentInMinutes(final Calendar calendar, final int fragment) { +3ee44535b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-03-14 13:23:25 +0000 1554) return getFragment(calendar, fragment, TimeUnit.MINUTES); +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1555) } + 1556) +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1557) /** +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1558) *

Returns the number of hours within the +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1559) * fragment. All datefields greater than the fragment will be ignored.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1560) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1561) *

Asking the hours of any date will only return the number of hours +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1562) * of the current day (resulting in a number between 0 and 23). This +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1563) * method will retrieve the number of hours for any fragment. +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1564) * For example, if you want to calculate the number of hours past this month, +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1565) * your fragment is Calendar.MONTH. The result will be all hours of the +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1566) * past day(s).

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1567) * +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1568) *

Valid fragments are: Calendar.YEAR, Calendar.MONTH, both +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1569) * Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1570) * Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1571) * A fragment less than or equal to a HOUR field will return 0.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1572) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1573) *
    +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1574) *
  • January 1, 2008 7:15:10.538 with Calendar.DAY_OF_YEAR as fragment will return 7 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1575) * (equivalent to calendar.get(Calendar.HOUR_OF_DAY))
  • +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1576) *
  • January 6, 2008 7:15:10.538 with Calendar.DAY_OF_YEAR as fragment will return 7 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1577) * (equivalent to calendar.get(Calendar.HOUR_OF_DAY))
  • +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1578) *
  • January 1, 2008 7:15:10.538 with Calendar.MONTH as fragment will return 7
  • +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1579) *
  • January 6, 2008 7:15:10.538 with Calendar.MONTH as fragment will return 127 (5*24 + 7)
  • +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1580) *
  • January 16, 2008 7:15:10.538 with Calendar.MILLISECOND as fragment will return 0 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1581) * (a millisecond cannot be split in hours)
  • +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1582) *
+16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1583) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1584) * @param calendar the calendar to work with, not null +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1585) * @param fragment the {@code Calendar} field part of calendar to calculate +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1586) * @return number of hours within the fragment of date +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1587) * @throws IllegalArgumentException if the date is null or +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1588) * fragment is not supported +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1589) * @since 2.4 +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1590) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 1591) public static long getFragmentInHours(final Calendar calendar, final int fragment) { +3ee44535b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-03-14 13:23:25 +0000 1592) return getFragment(calendar, fragment, TimeUnit.HOURS); +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1593) } + 1594) +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1595) /** +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1596) *

Returns the number of days within the +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1597) * fragment. All datefields greater than the fragment will be ignored.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1598) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1599) *

Asking the days of any date will only return the number of days +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1600) * of the current month (resulting in a number between 1 and 31). This +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1601) * method will retrieve the number of days for any fragment. +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1602) * For example, if you want to calculate the number of days past this year, +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1603) * your fragment is Calendar.YEAR. The result will be all days of the +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1604) * past month(s).

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1605) * +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1606) *

Valid fragments are: Calendar.YEAR, Calendar.MONTH, both +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1607) * Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1608) * Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1609) * A fragment less than or equal to a DAY field will return 0.

+1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1610) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1611) *
    +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1612) *
  • January 28, 2008 with Calendar.MONTH as fragment will return 28 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1613) * (equivalent to calendar.get(Calendar.DAY_OF_MONTH))
  • +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1614) *
  • February 28, 2008 with Calendar.MONTH as fragment will return 28 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1615) * (equivalent to calendar.get(Calendar.DAY_OF_MONTH))
  • +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1616) *
  • January 28, 2008 with Calendar.YEAR as fragment will return 28 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1617) * (equivalent to calendar.get(Calendar.DAY_OF_YEAR))
  • +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1618) *
  • February 28, 2008 with Calendar.YEAR as fragment will return 59 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1619) * (equivalent to calendar.get(Calendar.DAY_OF_YEAR))
  • +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1620) *
  • January 28, 2008 with Calendar.MILLISECOND as fragment will return 0 +8e83403ba src/java/org/apache/commons/lang/time/DateUtils.java (Niall Pemberton 2008-03-06 00:58:11 +0000 1621) * (a millisecond cannot be split in days)
  • +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1622) *
+16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1623) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1624) * @param calendar the calendar to work with, not null +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1625) * @param fragment the {@code Calendar} field part of calendar to calculate +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1626) * @return number of days within the fragment of date +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1627) * @throws IllegalArgumentException if the date is null or +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1628) * fragment is not supported +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1629) * @since 2.4 +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1630) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 1631) public static long getFragmentInDays(final Calendar calendar, final int fragment) { +3ee44535b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-03-14 13:23:25 +0000 1632) return getFragment(calendar, fragment, TimeUnit.DAYS); +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1633) } + 1634) +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1635) /** +d79d88439 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-24 12:48:59 +0000 1636) * Gets a Date fragment for any unit. +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1637) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1638) * @param date the date to work with, not null +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1639) * @param fragment the Calendar field part of date to calculate +3ee44535b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-03-14 13:23:25 +0000 1640) * @param unit the time unit +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1641) * @return number of units within the fragment of the date +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1642) * @throws IllegalArgumentException if the date is null or +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1643) * fragment is not supported +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1644) * @since 2.4 +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1645) */ +3ee44535b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-03-14 13:23:25 +0000 1646) private static long getFragment(final Date date, final int fragment, final TimeUnit unit) { +ff6b0ada7 src/main/java/org/apache/commons/lang3/time/DateUtils.java (pascalschumacher 2017-04-19 19:07:05 +0000 1647) validateDateNotNull(date); +5292526e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:09:45 +0000 1648) final Calendar calendar = Calendar.getInstance(); +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1649) calendar.setTime(date); +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1650) return getFragment(calendar, fragment, unit); +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1651) } + 1652) +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1653) /** +d79d88439 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-24 12:48:59 +0000 1654) * Gets a Calendar fragment for any unit. +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1655) * +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1656) * @param calendar the calendar to work with, not null +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1657) * @param fragment the Calendar field part of calendar to calculate +3ee44535b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-03-14 13:23:25 +0000 1658) * @param unit the time unit +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1659) * @return number of units within the fragment of the calendar +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1660) * @throws IllegalArgumentException if the date is null or +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1661) * fragment is not supported +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1662) * @since 2.4 +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1663) */ +3ee44535b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-03-14 13:23:25 +0000 1664) private static long getFragment(final Calendar calendar, final int fragment, final TimeUnit unit) { +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1665) if(calendar == null) { +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1666) throw new IllegalArgumentException("The date must not be null"); +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1667) } + 1668) +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1669) long result = 0; + 1670) +96c30e248 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2014-06-27 12:22:17 +0000 1671) final int offset = (unit == TimeUnit.DAYS) ? 0 : 1; + 1672) +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1673) // Fragments bigger than a day require a breakdown to days +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1674) switch (fragment) { +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1675) case Calendar.YEAR: +3ee44535b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-03-14 13:23:25 +0000 1676) result += unit.convert(calendar.get(Calendar.DAY_OF_YEAR) - offset, TimeUnit.DAYS); +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1677) break; +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1678) case Calendar.MONTH: +3ee44535b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-03-14 13:23:25 +0000 1679) result += unit.convert(calendar.get(Calendar.DAY_OF_MONTH) - offset, TimeUnit.DAYS); +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1680) break; +d6a556dad src/main/java/org/apache/commons/lang3/time/DateUtils.java (Sebastian Bazley 2014-02-06 13:51:45 +0000 1681) default: +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1682) break; +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1683) } + 1684) +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1685) switch (fragment) { +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1686) // Number of days already calculated for these cases +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1687) case Calendar.YEAR: +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1688) case Calendar.MONTH: + 1689) +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1690) // The rest of the valid cases +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1691) case Calendar.DAY_OF_YEAR: +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1692) case Calendar.DATE: +3ee44535b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-03-14 13:23:25 +0000 1693) result += unit.convert(calendar.get(Calendar.HOUR_OF_DAY), TimeUnit.HOURS); +835085d91 src/java/org/apache/commons/lang/time/DateUtils.java (Sebastian Bazley 2009-10-24 15:48:10 +0000 1694) //$FALL-THROUGH$ +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1695) case Calendar.HOUR_OF_DAY: +3ee44535b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-03-14 13:23:25 +0000 1696) result += unit.convert(calendar.get(Calendar.MINUTE), TimeUnit.MINUTES); +835085d91 src/java/org/apache/commons/lang/time/DateUtils.java (Sebastian Bazley 2009-10-24 15:48:10 +0000 1697) //$FALL-THROUGH$ +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1698) case Calendar.MINUTE: +3ee44535b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-03-14 13:23:25 +0000 1699) result += unit.convert(calendar.get(Calendar.SECOND), TimeUnit.SECONDS); +835085d91 src/java/org/apache/commons/lang/time/DateUtils.java (Sebastian Bazley 2009-10-24 15:48:10 +0000 1700) //$FALL-THROUGH$ +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1701) case Calendar.SECOND: +3ee44535b src/main/java/org/apache/commons/lang3/time/DateUtils.java (Duncan Jones 2014-03-14 13:23:25 +0000 1702) result += unit.convert(calendar.get(Calendar.MILLISECOND), TimeUnit.MILLISECONDS); +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1703) break; +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1704) case Calendar.MILLISECOND: break;//never useful +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1705) default: throw new IllegalArgumentException("The fragment " + fragment + " is not supported"); +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1706) } +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1707) return result; +16120b386 src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2008-02-01 07:12:56 +0000 1708) } + 1709) +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1710) /** +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1711) * Determines if two calendars are equal up to no more than the specified +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1712) * most significant field. +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1713) * +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1714) * @param cal1 the first calendar, not null +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1715) * @param cal2 the second calendar, not null +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 1716) * @param field the field from {@code Calendar} +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1717) * @return true if equal; otherwise false +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1718) * @throws IllegalArgumentException if any argument is null +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1719) * @see #truncate(Calendar, int) +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1720) * @see #truncatedEquals(Date, Date, int) +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1721) * @since 3.0 +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1722) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 1723) public static boolean truncatedEquals(final Calendar cal1, final Calendar cal2, final int field) { +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1724) return truncatedCompareTo(cal1, cal2, field) == 0; +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1725) } + 1726) +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1727) /** +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1728) * Determines if two dates are equal up to no more than the specified +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1729) * most significant field. +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1730) * +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1731) * @param date1 the first date, not null +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1732) * @param date2 the second date, not null +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 1733) * @param field the field from {@code Calendar} +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1734) * @return true if equal; otherwise false +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1735) * @throws IllegalArgumentException if any argument is null +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1736) * @see #truncate(Date, int) +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1737) * @see #truncatedEquals(Calendar, Calendar, int) +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1738) * @since 3.0 +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1739) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 1740) public static boolean truncatedEquals(final Date date1, final Date date2, final int field) { +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1741) return truncatedCompareTo(date1, date2, field) == 0; +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1742) } + 1743) +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1744) /** +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1745) * Determines how two calendars compare up to no more than the specified +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1746) * most significant field. +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1747) * +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1748) * @param cal1 the first calendar, not null +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1749) * @param cal2 the second calendar, not null +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 1750) * @param field the field from {@code Calendar} +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1751) * @return a negative integer, zero, or a positive integer as the first +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1752) * calendar is less than, equal to, or greater than the second. +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1753) * @throws IllegalArgumentException if any argument is null +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1754) * @see #truncate(Calendar, int) +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1755) * @see #truncatedCompareTo(Date, Date, int) +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1756) * @since 3.0 +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1757) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 1758) public static int truncatedCompareTo(final Calendar cal1, final Calendar cal2, final int field) { +5292526e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:09:45 +0000 1759) final Calendar truncatedCal1 = truncate(cal1, field); +5292526e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:09:45 +0000 1760) final Calendar truncatedCal2 = truncate(cal2, field); +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1761) return truncatedCal1.compareTo(truncatedCal2); +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1762) } + 1763) +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1764) /** +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1765) * Determines how two dates compare up to no more than the specified +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1766) * most significant field. +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1767) * +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1768) * @param date1 the first date, not null +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1769) * @param date2 the second date, not null +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1770) * @param field the field from Calendar +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1771) * @return a negative integer, zero, or a positive integer as the first +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1772) * date is less than, equal to, or greater than the second. +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1773) * @throws IllegalArgumentException if any argument is null +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1774) * @see #truncate(Calendar, int) +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1775) * @see #truncatedCompareTo(Date, Date, int) +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1776) * @since 3.0 +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1777) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 1778) public static int truncatedCompareTo(final Date date1, final Date date2, final int field) { +5292526e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:09:45 +0000 1779) final Date truncatedDate1 = truncate(date1, field); +5292526e4 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:09:45 +0000 1780) final Date truncatedDate2 = truncate(date2, field); +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1781) return truncatedDate1.compareTo(truncatedDate2); +7721acd0e src/main/java/org/apache/commons/lang3/time/DateUtils.java (Paul C. Benedict Jr 2010-02-27 03:29:43 +0000 1782) } + 1783) +ff6b0ada7 src/main/java/org/apache/commons/lang3/time/DateUtils.java (pascalschumacher 2017-04-19 19:07:05 +0000 1784) private static void validateDateNotNull(final Date date) { +ff6b0ada7 src/main/java/org/apache/commons/lang3/time/DateUtils.java (pascalschumacher 2017-04-19 19:07:05 +0000 1785) Validate.isTrue(date != null, "The date must not be null"); +ff6b0ada7 src/main/java/org/apache/commons/lang3/time/DateUtils.java (pascalschumacher 2017-04-19 19:07:05 +0000 1786) } + 1787) +f446d290d src/main/java/org/apache/commons/lang3/time/DateUtils.java (Stephen Colebourne 2011-03-04 13:29:01 +0000 1788) //----------------------------------------------------------------------- +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1789) /** +eadb45d1a src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2003-07-14 22:25:06 +0000 1790) *

Date iterator.

+402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1791) */ +950def5b6 src/java/org/apache/commons/lang/time/DateUtils.java (Sebastian Bazley 2009-03-16 02:11:38 +0000 1792) static class DateIterator implements Iterator { +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1793) private final Calendar endFinal; +008d16ed5 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-28 17:49:53 +0000 1794) private final Calendar spot; + 1795) +228957a0e src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2005-02-04 03:16:58 +0000 1796) /** +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1797) * Constructs a DateIterator that ranges from one date to another. +228957a0e src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2005-02-04 03:16:58 +0000 1798) * +228957a0e src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2005-02-04 03:16:58 +0000 1799) * @param startFinal start date (inclusive) +24b91fba8 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Henri Yandell 2011-12-28 07:05:24 +0000 1800) * @param endFinal end date (inclusive) +228957a0e src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2005-02-04 03:16:58 +0000 1801) */ +5bd622dab src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2013-01-22 07:07:42 +0000 1802) DateIterator(final Calendar startFinal, final Calendar endFinal) { +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1803) super(); +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1804) this.endFinal = endFinal; +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1805) spot = startFinal; +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1806) spot.add(Calendar.DATE, -1); +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1807) } + 1808) +228957a0e src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2005-02-04 03:16:58 +0000 1809) /** +228957a0e src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2005-02-04 03:16:58 +0000 1810) * Has the iterator not reached the end date yet? +228957a0e src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2005-02-04 03:16:58 +0000 1811) * +228957a0e src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2005-02-04 03:16:58 +0000 1812) * @return true if the iterator has yet to reach the end date +228957a0e src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2005-02-04 03:16:58 +0000 1813) */ +b81934348 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-05 17:53:39 +0000 1814) @Override +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1815) public boolean hasNext() { +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1816) return spot.before(endFinal); +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1817) } + 1818) +228957a0e src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2005-02-04 03:16:58 +0000 1819) /** +228957a0e src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2005-02-04 03:16:58 +0000 1820) * Return the next calendar in the iteration +228957a0e src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2005-02-04 03:16:58 +0000 1821) * +228957a0e src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2005-02-04 03:16:58 +0000 1822) * @return Object calendar for the next date +228957a0e src/java/org/apache/commons/lang/time/DateUtils.java (Henri Yandell 2005-02-04 03:16:58 +0000 1823) */ +b81934348 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-05 17:53:39 +0000 1824) @Override +950def5b6 src/java/org/apache/commons/lang/time/DateUtils.java (Sebastian Bazley 2009-03-16 02:11:38 +0000 1825) public Calendar next() { +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1826) if (spot.equals(endFinal)) { +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1827) throw new NoSuchElementException(); +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1828) } +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1829) spot.add(Calendar.DATE, 1); +950def5b6 src/java/org/apache/commons/lang/time/DateUtils.java (Sebastian Bazley 2009-03-16 02:11:38 +0000 1830) return (Calendar) spot.clone(); +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1831) } + 1832) +bbdd2932d src/java/org/apache/commons/lang/time/DateUtils.java (Gary D. Gregory 2004-10-21 01:18:34 +0000 1833) /** +bbdd2932d src/java/org/apache/commons/lang/time/DateUtils.java (Gary D. Gregory 2004-10-21 01:18:34 +0000 1834) * Always throws UnsupportedOperationException. +1da8ccdbf src/main/java/org/apache/commons/lang3/time/DateUtils.java (Benedikt Ritter 2017-06-06 13:12:06 +0000 1835) * +bbdd2932d src/java/org/apache/commons/lang/time/DateUtils.java (Gary D. Gregory 2004-10-21 01:18:34 +0000 1836) * @throws UnsupportedOperationException +bbdd2932d src/java/org/apache/commons/lang/time/DateUtils.java (Gary D. Gregory 2004-10-21 01:18:34 +0000 1837) * @see java.util.Iterator#remove() +bbdd2932d src/java/org/apache/commons/lang/time/DateUtils.java (Gary D. Gregory 2004-10-21 01:18:34 +0000 1838) */ +b81934348 src/main/java/org/apache/commons/lang3/time/DateUtils.java (Gary D. Gregory 2012-04-05 17:53:39 +0000 1839) @Override +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1840) public void remove() { +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1841) throw new UnsupportedOperationException(); +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1842) } +402c8e084 src/java/org/apache/commons/lang/time/DateUtils.java (Stephen Colebourne 2003-06-23 23:41:10 +0000 1843) } + 1844) +26c6a7c28 src/java/org/apache/commons/lang/DateUtils.java (Henri Yandell 2002-12-16 21:48:41 +0000 1845) }