commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Henri Yandell (JIRA)" <j...@apache.org>
Subject [jira] Commented: (LANG-379) Calculating A date fragment in any time-unit
Date Thu, 10 Jan 2008 04:34:33 GMT

    [ https://issues.apache.org/jira/browse/LANG-379?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12557540#action_12557540
] 

Henri Yandell commented on LANG-379:
------------------------------------

Valid point on Calendar/Date - the API already does lots of Calendar.getInstance().setTime.
So I'll retract that.

On ERA/YEAR etc, I get it now. I had unit and fragment confused; fragment is the user-passed
in one. 

=-=-=-=

Second round of thoughts (we've been burnt on time based code in Lang before and so have a
strong justify it attitude):

Code examples in the Javadoc are definitely going to be needed - looking at the javadoc it
isn't clear to me what the methods do exactly. I had to walk through the code before I properly
understood (and with the drop-through on the switch statement, it involves a little bit of
time to notice that's not a standard idiom). So - examples please :)

I'm also not sure "Why?" :) Why would I want the number of seconds of this year? Or the number
of minutes this month?



> 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.3
>            Reporter: Robert Scholte
>            Priority: Minor
>             Fix For: 2.4
>
>         Attachments: DateUtils-fragments.patch, 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.


Mime
View raw message