litesoft
@ 851
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 |
package org.litesoft.core.typeutils.gregorian; import java.util.*; import org.litesoft.core.typeutils.*; public enum Month { January( 31 ), Febuary() { @Override public int daysIn( int pYear ) { return Year.isLeap( pYear ) ? 29 : 28; } }, March( 31 ), April( 30 ), May( 31 ), June( 30 ), July( 31 ), August( 31 ), September( 30 ), October( 31 ), November( 30 ), December( 31 ); private final int mDaysInMonth; private final String mShortName; private final String mNameUpperCase; private Month( int pDaysInMonth ) { mDaysInMonth = pDaysInMonth; mShortName = name().substring( 0, 3 ); mNameUpperCase = name().toUpperCase(); } private Month() { this( -1 ); } public String shortName() { return mShortName; } public String nameUpperCase() { return mNameUpperCase; } /** * Days in 'this' Month for the specified Year. * <p/> * Dates prior to the year 1800 may get incorrect results! */ public int daysIn( int pYear ) { return mDaysInMonth; } public int toMonthNumber() { return ordinal() + 1; } public static Month fromMonthNumber( int pMonthNumber ) { return ((1 <= pMonthNumber) && (pMonthNumber <= 12)) ? values()[pMonthNumber - 1] : null; } public static Month valueOf( int pOrdinal ) { while ( pOrdinal < 0 ) { pOrdinal += 12; } while ( 12 <= pOrdinal ) { pOrdinal -= 12; } return values()[pOrdinal]; } public Month prev() { return valueOf( ordinal() - 1 ); } public Month next() { return valueOf( ordinal() + 1 ); } public static String shortNameFromMonthNumber( int pMonthNumber ) { Month zMonth = Month.fromMonthNumber( pMonthNumber ); return (zMonth == null) ? "---" : zMonth.shortName(); } public static String nameFromMonthNumber( int pMonthNumber ) { Month zMonth = Month.fromMonthNumber( pMonthNumber ); return (zMonth == null) ? "---" : zMonth.name(); } /** * Days in specified Year & Month (or 0 if the month is invalid). * <p/> * Dates prior to the year 1800 may get incorrect results! */ public static int daysIn( int pYear, int pMonth ) { Month zMonth = fromMonthNumber( pMonth ); return (zMonth == null) ? 0 : zMonth.daysIn( pYear ); } public static int parseMonth( String pToParse, String pFrom ) throws IllegalArgumentException { pToParse = pToParse.trim().toUpperCase(); if ( pToParse.length() == 0 ) { throw new IllegalArgumentException( "No Month in '" + pFrom + "'" ); } Month zFound = null; List<Month> zMultipleFounds = null; for ( Month zMonth : values() ) { if ( zMonth.nameUpperCase().startsWith( pToParse ) ) { if ( zFound == null ) { zFound = zMonth; } else { if ( zMultipleFounds == null ) { zMultipleFounds = Lists.newArrayList(); zMultipleFounds.add( zFound ); } zMultipleFounds.add( zMonth ); } } } if ( zMultipleFounds != null ) { String zMultiple = zMultipleFounds.toString(); throw new IllegalArgumentException( "Invalid Month '" + pToParse + "' in '" + pFrom + "': Matched-" + zMultiple.substring( 1, zMultiple.length() - 1 ) ); } if ( zFound == null ) { throw new IllegalArgumentException( "Month '" + pToParse + "' NOT recognized in '" + pFrom + "'" ); } return zFound.toMonthNumber(); } } |
Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/core/typeutils/gregorian/Month.java
Revision | Author | Commited | Message |
---|---|---|---|
851 | GeorgeS | Mon 08 Oct, 2012 00:05:32 +0000 | Breaking the code as Temporal changes are implemented... |