Return-Path: Delivered-To: apmail-commons-issues-archive@locus.apache.org Received: (qmail 41009 invoked from network); 29 Nov 2007 15:27:16 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 29 Nov 2007 15:27:16 -0000 Received: (qmail 31717 invoked by uid 500); 29 Nov 2007 15:26:55 -0000 Delivered-To: apmail-commons-issues-archive@commons.apache.org Received: (qmail 31633 invoked by uid 500); 29 Nov 2007 15:26:55 -0000 Mailing-List: contact issues-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: issues@commons.apache.org Delivered-To: mailing list issues@commons.apache.org Received: (qmail 31599 invoked by uid 99); 29 Nov 2007 15:26:54 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 29 Nov 2007 07:26:54 -0800 X-ASF-Spam-Status: No, hits=-100.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO brutus.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 29 Nov 2007 15:27:05 +0000 Received: from brutus (localhost [127.0.0.1]) by brutus.apache.org (Postfix) with ESMTP id 54F7A71422C for ; Thu, 29 Nov 2007 07:26:43 -0800 (PST) Message-ID: <17516748.1196350003345.JavaMail.jira@brutus> Date: Thu, 29 Nov 2007 07:26:43 -0800 (PST) From: "Paul Benedict (JIRA)" To: issues@commons.apache.org Subject: [jira] Commented: (LANG-379) Calculating A date fragment in any time-unit In-Reply-To: <9990053.1196348323017.JavaMail.jira@brutus> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Virus-Checked: Checked by ClamAV on apache.org [ https://issues.apache.org/jira/browse/LANG-379?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12546764 ] Paul Benedict commented on LANG-379: ------------------------------------ Should the switch statements should throw an exception on the default case? It is an assertion error to pass an unsupported fragment. > Calculating A date fragment in any time-unit > -------------------------------------------- > > Key: LANG-379 > URL: https://issues.apache.org/jira/browse/LANG-379 > Project: Commons Lang > Issue Type: New Feature > Affects Versions: 2.4 > Reporter: Robert Scholte > Priority: Minor > Attachments: DateUtilsFragmentTest.java > > > These DateUtils-features can make it possible to calculate a date-part in any time-unit. For example: the number of minutes of this year, the number of seconds of today, etc. > I've started with some coding, and if there's enough interest we can make it more solid. > public static long getFragmentInSeconds(Date date, int fragment) { > return getFragment(date, fragment, Calendar.SECOND); > } > > public static long getFragmentInMinutes(Date date, int fragment) { > return getFragment(date, fragment, Calendar.MINUTE); > } > > public static long getFragmentInHours(Date date, int fragment) { > return getFragment(date, fragment, Calendar.HOUR_OF_DAY); > } > > public static long getFragmentInDays(Date date, int fragment) { > return getFragment(date, fragment, Calendar.DAY_OF_YEAR); > } > public static long getFragmentInSeconds(Calendar calendar, int fragment) { > return getFragment(calendar, fragment, Calendar.SECOND); > } > > public static long getFragmentInMinutes(Calendar calendar, int fragment) { > return getFragment(calendar, fragment, Calendar.MINUTE); > } > > public static long getFragmentInHours(Calendar calendar, int fragment) { > return getFragment(calendar, fragment, Calendar.HOUR_OF_DAY); > } > > public static long getFragmentInDays(Calendar calendar, int fragment) { > return getFragment(calendar, fragment, Calendar.DAY_OF_YEAR); > } > > private static long getFragment(Date date, int fragment, int unit) { > Calendar calendar = Calendar.getInstance(); > calendar.setTime(date); > return getFragment(calendar, fragment, unit); > } > private static long getFragment(Calendar calendar, int fragment, int unit) { > long millisPerUnit = getMillisPerFragment(unit); > long result = 0; > switch (fragment) { > case Calendar.YEAR: > result += (calendar.get(Calendar.DAY_OF_YEAR) * MILLIS_PER_DAY) / millisPerUnit; > case Calendar.MONTH: > result += (calendar.get(Calendar.DAY_OF_MONTH) * MILLIS_PER_DAY) / millisPerUnit; > case Calendar.DAY_OF_YEAR: > case Calendar.DATE: > result += (calendar.get(Calendar.HOUR_OF_DAY) * MILLIS_PER_HOUR) / millisPerUnit; > case Calendar.HOUR_OF_DAY: > result += (calendar.get(Calendar.MINUTE) * MILLIS_PER_MINUTE) / millisPerUnit; > case Calendar.MINUTE: > result += (calendar.get(Calendar.SECOND) * MILLIS_PER_SECOND) / millisPerUnit; > case Calendar.SECOND: > result += (calendar.get(Calendar.MILLISECOND) * 1) / millisPerUnit; > } > return result; > } > > private static long getMillisPerFragment(int fragment) { > long result = Long.MAX_VALUE; > switch (fragment) { > case Calendar.DAY_OF_YEAR: > case Calendar.DATE: > result = MILLIS_PER_DAY; > break; > case Calendar.HOUR_OF_DAY: > result = MILLIS_PER_HOUR; > break; > case Calendar.MINUTE: > result = MILLIS_PER_MINUTE; > break; > case Calendar.SECOND: > result = MILLIS_PER_SECOND; > break; > case Calendar.MILLISECOND: > result = 1; > break; > } > return result; > } -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online.