1 18 19 package sync4j.syncclient.demo; 20 21 import java.util.Date ; 22 import java.util.Hashtable ; 23 import java.util.StringTokenizer ; 24 import java.text.ParseException ; 25 import java.text.SimpleDateFormat ; 26 27 import sync4j.syncclient.spdm.DMException; 28 import sync4j.syncclient.spdm.SimpleDeviceManager; 29 import sync4j.syncclient.spdm.ManagementNode; 30 31 import sync4j.foundation.pdi.utils.TimeUtils; 32 33 34 41 42 public abstract class FieldsHelper 43 implements ConfigurationParameters { 44 45 47 public static final String DATE_FORMAT = "dd/MM/yyyy" ; 48 public static final String DATE_FORMAT_AND_TIME = "dd/MM/yyyy HH:mm:ss" ; 49 public static final String UTC_DATE_FORMAT = "yyyyMMdd'T'HHmmss'Z'" ; 50 public static final String NO_UTC_DATE_FORMAT = "yyyyMMdd'T'HHmmss" ; 51 public static final String BIRTHDAY_FORMAT = "yyyy-MM-dd" ; 52 53 55 private static Language ln = new Language() ; 56 57 59 61 protected static boolean checkDate (String date, String dateFormat) { 62 63 StringTokenizer st = null ; 64 SimpleDateFormat formatter = null ; 65 Date dTemp = new Date (); 66 int k = 0; 67 68 if (date != null && date.length() > 0) { 69 70 try { 71 if (dateFormat.equals(DATE_FORMAT)) { 72 formatter = new SimpleDateFormat (DATE_FORMAT); 73 } 74 else if (dateFormat.equals(DATE_FORMAT_AND_TIME)) { 75 formatter = new SimpleDateFormat (DATE_FORMAT_AND_TIME); 76 } 77 formatter.setLenient (false ); 78 dTemp = formatter.parse (date ); 79 80 if (dateFormat.equals(DATE_FORMAT)) { 81 st = new StringTokenizer (date, "/"); 82 while (st.hasMoreTokens()) { 83 84 k = Integer.parseInt(st.nextToken()); 85 86 if (k >9999) { 87 throw new NumberFormatException (ln. 88 getString ("error_number_format")); 89 } 90 91 } 92 } else if (dateFormat.equals(DATE_FORMAT_AND_TIME)) { 93 String sTemp = date.substring(0, 10); 94 st = new StringTokenizer (sTemp, "/"); 95 while (st.hasMoreTokens()) { 96 97 k = Integer.parseInt(st.nextToken()); 98 99 if (k >9999) { 100 throw new NumberFormatException (ln. 101 getString ("error_number_format")); 102 } 103 104 } 105 sTemp = date.substring(11); 106 107 st = new StringTokenizer (sTemp, ":"); 108 int i = 0; 109 while (st.hasMoreTokens()) { 110 String s = st.nextToken(); 111 112 if (s.length() > 2) { 113 throw new NumberFormatException (ln. 114 getString ("error_number_format")); 115 } 116 117 k = Integer.parseInt(s); 118 119 if (i == 0 && (k < 0 || k > 24)) { 121 throw new NumberFormatException (ln. 122 getString ("error_number_format")); 123 } 124 if (i == 1 && (k < 0 || k > 60)) { 126 throw new NumberFormatException (ln. 127 getString ("error_number_format")); 128 } 129 if (i == 2 && (k < 0 || k > 60)) { 131 throw new NumberFormatException (ln. 132 getString ("error_number_format")); 133 } 134 135 i++; 136 } 137 } 138 } catch (NullPointerException e ) { 139 return false; 140 } catch (ParseException e ) { 141 return false; 142 } catch (NumberFormatException e ) { 143 return false; 144 } catch (Exception e ) { 145 return false; 146 } 147 148 } 149 150 return true; 151 152 } 153 154 protected static String convertDateToUTC(String stringDate) { 155 156 ManagementNode rootNode = null ; 157 Hashtable parameters = null; 158 159 if (stringDate == null || stringDate.length() == 0) { 160 return ""; 161 } 162 163 rootNode = SimpleDeviceManager.getDeviceManager().getManagementTree(); 164 165 try { 166 parameters = rootNode.getNodeValues(MainWindow.DM_VALUE_PATH); 167 } catch (DMException e) { 168 e.printStackTrace(); 169 } 170 171 return TimeUtils.localTimeToUTC (stringDate) ; 172 173 } 174 175 protected static String convertDateFromUTC (String stringDate) { 176 177 ManagementNode rootNode = null ; 178 Hashtable parameters = null ; 179 180 String adjustDate = null ; 181 182 if (stringDate == null || stringDate.length() == 0) { 183 return ""; 184 } 185 186 return TimeUtils.UTCToLocalTime (stringDate ) ; 187 188 } 189 190 197 protected static String convertBirthdayFrom(String stringDate) { 198 if (stringDate == null || stringDate.length() == 0) { 199 return ""; 200 } 201 202 try { 203 SimpleDateFormat formatter = new SimpleDateFormat (BIRTHDAY_FORMAT); 204 formatter.setLenient(false); 205 Date date = formatter.parse(stringDate); 206 207 formatter.applyPattern(DATE_FORMAT); 208 return formatter.format(date); 209 210 } catch (Exception e) { 211 e.printStackTrace(); 212 } 213 return ""; 214 } 215 216 223 protected static String convertBirthdayTo(String stringDate) 224 throws Exception { 225 226 if (stringDate == null || stringDate.length() == 0) { 227 return ""; 228 } 229 230 try { 231 SimpleDateFormat formatter = new SimpleDateFormat (DATE_FORMAT); 232 formatter.setLenient(false); 233 Date date = formatter.parse(stringDate); 234 235 formatter.applyPattern(BIRTHDAY_FORMAT); 236 stringDate = formatter.format(date); 237 238 } catch (Exception e) { 239 e.printStackTrace(); 240 } 241 return TimeUtils.normalizeToISO8601(stringDate, null); 242 } 243 } 244 | Popular Tags |