litesoft
@ 898
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.core.simpletypes.temporal; import java.util.*; import org.litesoft.core.*; import org.litesoft.core.typeutils.gregorian.*; /** * A Calendar "date" (e.g. Year, Month, & Day) ("Wall" relative). * <p/> * Note: This class supports Gregorian dates only and has no concept of time or TimeZones. As such when working with * Java Util Date(s) (or it's descendants) the local (or "Wall") date is all that is considered! */ @SuppressWarnings("UnusedDeclaration") public final class CalendarYMD extends AbstractCalendarYMD<CalendarYMD, AbstractCalendarYMD.Mutable> implements SQLdateable { private static final long serialVersionUID = 1L; private int mYear, mMonth, mDay; @Override public int getYear() { return mYear; } @Override public int getMonth() { return mMonth; } @Override public int getDay() { return mDay; } public CalendarYMD( int pYear, int pMonth, int pDay ) { mMonth = validateMonth( mYear = pYear, pMonth ); mDay = validateDay( mYear, mMonth, pDay ); } @SuppressWarnings({"deprecation", "UnusedDeclaration"}) @Deprecated /** for Serialization */ protected CalendarYMD() { this( 0, 0, 0 ); } public CalendarYMD( CalendarAccessorYMD pAccessor ) { this( pAccessor.getYear(), pAccessor.getMonth(), pAccessor.getDay() ); } public CalendarYMD( Date pDate ) throws IllegalArgumentException { this( new UtilDateAdaptor( pDate ) ); } @Override public TemporalResolution getResolution() { return TemporalResolution.ToDay; } @Override public String toString() { return YearMonthDayFormatAccessor.get().format( this ); } public static String toString( CalendarYMD pCalendar ) { return (pCalendar != null) ? pCalendar.toString() : null; } public static String toYMD( CalendarYMD pCalendar ) { return (pCalendar != null) ? pCalendar.toYMD() : null; } public String toYMD() { return formatSortableDisplayFormYMD( this, new StringBuilder() ).toString(); } public static CalendarYMD fromYMD( String pToYMD ) throws IllegalArgumentException { return fromStringForm( pToYMD, "//" ); } public final String toSQLvalue() { return formatSQLvalueYMD( this, new StringBuilder() ).toString(); } public static CalendarYMD fromSQLvalue( String pToSQLvalue ) { return fromStringForm( pToSQLvalue, "--" ); } private static CalendarYMD fromStringForm( String pStringForm, String pSequentialSeperators ) { String[] parts = parseSimple( pStringForm, pSequentialSeperators ); return (parts == null) ? null : // new CalendarYMD( parseYear( pStringForm, parts ), // parseMonth( pStringForm, parts ), parseDay( pStringForm, parts ) ); } @Override public UtilDateAdaptor toUtilDateAdaptor() { return UtilDateAdaptor.wall( getYear(), getMonth(), getDay() ); } // vvvvvvvvvvvvvvvvvv Non-common Public methods vvvvvvvvvvvvvvvvv public static CalendarYMD today() { return new CalendarYMD( new Date() ); } /** * Return a CalendarYMD with the day set to the 1st day of the current month, this will always set the 'Day' field to 1. * * @throws IllegalArgumentException if the Resolution does not extend to Day. */ public CalendarYMD forceFirstOfMonth() throws IllegalArgumentException { return day( 1 ); } /** * Return a CalendarYMD with the day set to the last day of the current month (either 28, 29, 30, or 31). * * @throws IllegalArgumentException if the Resolution does not extend to Day. */ public CalendarYMD forceEndOfMonth() throws IllegalArgumentException { return day( Month.daysIn( getYear(), getMonth() ) ); } /** * Return a CalendarYMD who's <code>getWeekDay()</code> will be <code>pWeekday</code>, where the resulting date will <b>NOT</b> be in the Future relative to * <code>this</code>. * * @param pWeekDay !null */ public CalendarYMD notFuture( WeekDay pWeekDay ) { return minusDays( getWeekDay().deltaTillBackward( pWeekDay ) ); } public WeekDay getWeekDay() { return WeekDay.valueOf( UtilDateAdaptor.dayOfWeek( toUtilDateAdaptor().getWallDate() ) ); } /** * Number of days from this to pThem (could be negative). * * @param pThem !null * * @throws IllegalArgumentException if pThem is null or the resolution of either this' or pThem is not to the Day */ public int daysTill( CalendarAccessorYMD pThem ) throws IllegalArgumentException { undefinedResultIfNull( "daysTill", pThem ); int result = compareEm( compare( this.getYear(), pThem.getYear() ), compare( this.getMonth(), pThem.getMonth() ), compare( this.getDay(), pThem.getDay() ) ); if ( result == EQUAL ) { return 0; } return (result == LESSER) ? new DaysDiff( this ).till( pThem ) : -new DaysDiff( pThem ).till( this ); } // vvvvvvvvvvvvvvvvvv Support methods vvvvvvvvvvvvvvvvv @Override protected CalendarYMD createTypeFrom( Mutable pMutable ) { return new CalendarYMD( pMutable.getYear(), pMutable.getMonth(), pMutable.getDay() ); } @Override protected Mutable createMutable( int pYear, int pNewMonth ) { return new Mutable( pYear, pNewMonth, getDay(), true ); } @Override protected Mutable createMutable( int pYear, int pMonth, int pNewDay ) { return new Mutable( pYear, pMonth, pNewDay, false ); } private static class DaysDiff { private int mYear, mMonth, mDay, mDiff = 0; public DaysDiff( CalendarAccessorYMD pYMDaccessor ) { mYear = pYMDaccessor.getYear(); mMonth = pYMDaccessor.getMonth(); mDay = pYMDaccessor.getDay(); } public int till( CalendarAccessorYMD pThem ) // !null and is in our future { if ( mYear != pThem.getYear() ) { // advance to 1st of the year do { advanceTo1stOfNextMonth(); } while ( mMonth != 1 ); for (; mYear < pThem.getYear(); mYear++ ) { mDiff += Year.isLeap( mYear ) ? 366 : 365; } } while ( mMonth < pThem.getMonth() ) { advanceTo1stOfNextMonth(); } return mDiff + (pThem.getDay() - mDay); } private void advanceTo1stOfNextMonth() { int zDaysInMonth = Month.daysIn( mYear, mMonth ); mDiff += zDaysInMonth - (mDay - 1); mDay = 1; if ( ++mMonth > 12 ) { mMonth = 1; mYear++; } } } } |
Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/core/simpletypes/temporal/CalendarYMD.java
Revision | Author | Commited | Message |
---|---|---|---|
898 Diff | GeorgeS | Sun 17 Mar, 2013 21:46:33 +0000 | Temporal!!! |
897 Diff | GeorgeS | Mon 25 Feb, 2013 02:04:34 +0000 | |
893 Diff | GeorgeS | Mon 14 Jan, 2013 01:18:30 +0000 | |
864 Diff | GeorgeS | Mon 19 Nov, 2012 01:48:25 +0000 | |
862 Diff | GeorgeS | Thu 15 Nov, 2012 02:23:36 +0000 | On the Way... |
861 Diff | GeorgeS | Mon 05 Nov, 2012 13:55:32 +0000 | |
860 Diff | GeorgeS | Mon 05 Nov, 2012 01:39:02 +0000 | |
859 Diff | GeorgeS | Mon 05 Nov, 2012 01:26:38 +0000 | |
857 Diff | GeorgeS | Sun 04 Nov, 2012 16:01:37 +0000 | |
853 | GeorgeS | Sun 04 Nov, 2012 15:18:21 +0000 |