1 19 package org.openharmonise.swing.datefield; 20 21 import java.util.ArrayList ; 22 import java.util.Calendar ; 23 import java.util.HashMap ; 24 import java.util.List ; 25 26 43 public class DateFormatParser { 44 45 48 private String m_sFormat = null; 49 50 53 private ArrayList m_formatBlocks = new ArrayList (); 54 55 58 private static final HashMap m_mPatternInfo = new HashMap (); 59 60 static { 61 m_mPatternInfo.put("G", new Integer (Calendar.ERA)); 62 m_mPatternInfo.put("y", new Integer (Calendar.YEAR)); 63 m_mPatternInfo.put("M", new Integer (Calendar.MONTH)); 64 m_mPatternInfo.put("w", new Integer (Calendar.WEEK_OF_YEAR)); 65 m_mPatternInfo.put("W", new Integer (Calendar.WEEK_OF_MONTH)); 66 m_mPatternInfo.put("D", new Integer (Calendar.DAY_OF_YEAR)); 67 m_mPatternInfo.put("d", new Integer (Calendar.DAY_OF_MONTH)); 68 m_mPatternInfo.put("F", new Integer (Calendar.DAY_OF_WEEK_IN_MONTH)); 69 m_mPatternInfo.put("E", new Integer (Calendar.DAY_OF_WEEK)); 70 m_mPatternInfo.put("a", new Integer (Calendar.AM_PM)); 71 m_mPatternInfo.put("H", new Integer (Calendar.HOUR_OF_DAY)); 72 m_mPatternInfo.put("k", new Integer (Calendar.HOUR_OF_DAY)); 73 m_mPatternInfo.put("K", new Integer (Calendar.HOUR)); 74 m_mPatternInfo.put("h", new Integer (Calendar.HOUR)); 75 m_mPatternInfo.put("m", new Integer (Calendar.MINUTE)); 76 m_mPatternInfo.put("s", new Integer (Calendar.SECOND)); 77 m_mPatternInfo.put("S", new Integer (Calendar.MILLISECOND)); 78 m_mPatternInfo.put("z", new Integer (Calendar.ZONE_OFFSET)); 79 m_mPatternInfo.put("Z", new Integer (Calendar.ZONE_OFFSET)); 80 } 81 82 87 public DateFormatParser(String sFormat) { 88 super(); 89 this.m_sFormat = sFormat; 90 this.setup(); 91 } 92 93 97 private void setup() { 98 int nPos = 0; 99 int nInputPosition = 0; 100 101 while(nPos<=this.m_sFormat.length()-1) { 102 DateFormatBlock block = this.getNextBlock(nPos, nInputPosition); 103 this.m_formatBlocks.add(block); 104 nPos = nPos + block.getFormat().length(); 105 nInputPosition = nInputPosition + block.getInputLength(); 106 } 107 } 108 109 114 public List getFormatBlocks() { 115 return this.m_formatBlocks; 116 } 117 118 125 private DateFormatBlock getNextBlock(int nPosition, int nInputPosition) { 126 DateFormatBlock block = null; 127 boolean bFetchingActiveBlock = false; 128 String sFormatBlock = new String (); 129 130 int nCurrentPosition = nPosition; 131 132 String sFirst = this.m_sFormat.substring(nPosition, nPosition+1); 133 bFetchingActiveBlock = DateFormatParser.m_mPatternInfo.keySet().contains(sFirst); 134 135 int nCalendarField = -1; 136 if(bFetchingActiveBlock) { 137 nCalendarField = ((Integer )DateFormatParser.m_mPatternInfo.get(sFirst)).intValue(); 138 } 139 140 String sTemp = sFirst; 141 boolean bEnd = false; 142 143 while((bFetchingActiveBlock && DateFormatParser.m_mPatternInfo.keySet().contains(sTemp)) 144 || ((!bFetchingActiveBlock && !DateFormatParser.m_mPatternInfo.keySet().contains(sTemp)))) { 145 sFormatBlock = sFormatBlock + sTemp; 146 nCurrentPosition++; 147 if(nCurrentPosition<=this.m_sFormat.length()-1) { 148 sTemp = this.m_sFormat.substring(nCurrentPosition, nCurrentPosition+1); 149 } else { 150 break; 151 } 152 } 153 154 int nInputLength = -1; 155 156 if(sFormatBlock.equalsIgnoreCase("G")) { 157 nInputLength = 2; 158 } else if(!bFetchingActiveBlock) { 159 String sFormatTemp = sFormatBlock.replaceAll("'", ""); 160 nInputLength = sFormatTemp.length(); 161 } else { 162 nInputLength = sFormatBlock.length(); 163 } 164 165 String sClearedValue = null; 166 String sEntryValue = null; 167 168 if(bFetchingActiveBlock) { 169 if(sFormatBlock.equalsIgnoreCase("G")) { 170 sClearedValue = "AD"; 171 sEntryValue = "G"; 172 } else if(sFormatBlock.equalsIgnoreCase("yyyy")) { 173 nInputLength = 8; 174 sClearedValue = " ccyy"; 175 sEntryValue = " ccyy"; 176 } else { 177 StringBuffer sBuff = new StringBuffer (sFormatBlock); 178 if(sBuff.length()<nInputLength) { 179 for(int i=sBuff.length(); i<nInputLength; i++) { 180 sBuff.append(" "); 181 } 182 } 183 sClearedValue = sBuff.toString(); 184 sEntryValue = sBuff.toString(); 185 } 186 } else { 187 sClearedValue = sFormatBlock.replaceAll("'", ""); 188 sEntryValue = sFormatBlock.replaceAll("'", ""); 189 } 190 191 block = new DateFormatBlock(sFormatBlock, bFetchingActiveBlock, nInputPosition, nInputLength, sClearedValue, nCalendarField, sEntryValue); 192 193 return block; 194 } 195 196 public static void main(String [] args) { 197 DateFormatParser dfp = new DateFormatParser("yyyy-MM-dd G"); 198 DateFormatParser dfp2 = new DateFormatParser("yyyy-MM-dd'T'HH:mm:ss G"); 199 } 200 201 202 } 203 | Popular Tags |