📄 fieldhelper.java
字号:
package com.ebusiness.ebank.util;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2005</p> * <p>Company: eBusiness Inc., All right reserved</p> * @author unascribed * @version 1.0 */import java.sql.Timestamp;import java.util.Locale;import java.util.Date ;import java.util.Calendar ;import java.util.Set;import java.util.ArrayList;import java.util.Iterator;import java.text.SimpleDateFormat;import java.text.DateFormatSymbols;import java.text.ParseException ;import com.ebusiness.ebank.exception.BusinessException;import com.ebusiness.ebank.exception.ErrorMessages;public class FieldHelper { public static boolean isEmpty(String fieldValue){ if (fieldValue == null || fieldValue.trim().length() == 0) return true; else return false; } //Return a new value left padded with c public static String leftPad(String fieldValue, int length, char c) { String newValue = ""; int curLength; if (fieldValue == null || fieldValue.trim().length() == 0) curLength = 0; else { newValue = fieldValue.trim(); curLength = newValue.length(); } for (int i = 0; i < length - curLength; i++) newValue = c + newValue; return newValue; } /** * convert string to date as per format in userLocale */ public static Timestamp convertToTimestamp(String dateValue, Locale userLocale) { Timestamp retTimestamp = null; SimpleDateFormat formatter = null; if (isEmpty(dateValue)) return null; if (userLocale.equals(Locale.ENGLISH)) { formatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH); } else { DateFormatSymbols dateSymbols = new DateFormatSymbols(Locale.FRENCH) ; dateSymbols.setShortMonths(Constants.newFrenchShortMonths); formatter = new SimpleDateFormat("dd-MMM-yy", dateSymbols) ; } try { Date returnDate = formatter.parse(dateValue); retTimestamp = new Timestamp(returnDate.getTime()); } catch (ParseException e){ /** * do nothing, because before this method is called, "isValidDate" must be called to * ensure the "dateValue" is valid */ } return retTimestamp ; } /** * convert string to date as per format dd-MMM-yyyy in English */ public static Timestamp convertToTimestamp(String dateValue) { Timestamp retTimestamp = null; SimpleDateFormat formatter = null; formatter = new SimpleDateFormat("dd-MMM-yyyy"); if (isEmpty(dateValue)) return null; try { Date returnDate = formatter.parse(dateValue); retTimestamp = new Timestamp(returnDate.getTime()); } catch (ParseException e){} return retTimestamp ; }/** * Convert date to a string with format mask as dd-MMM-yyyy * */ public static String convertToString(Date dateValue, Locale userLocale) { String retValue = "" ; if (dateValue != null) { SimpleDateFormat formatter ; if (userLocale.equals(Locale.ENGLISH)) { formatter = new SimpleDateFormat("dd-MMM-yyyy" , Locale.ENGLISH); } else { DateFormatSymbols dateSymbols = new DateFormatSymbols(Locale.FRENCH) ; dateSymbols.setShortMonths(Constants.newFrenchShortMonths); formatter = new SimpleDateFormat("dd-MMM-yy", dateSymbols) ; } Date tempDate = new Date(dateValue.getTime()) ; retValue = formatter.format(tempDate); } return retValue; }/** * Convert date to a string with format mask as dd-MMM-yyyy in English */ public static String convertToString(Date dateValue, String formatMask) { String retValue = "" ; if (dateValue != null) { SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy"); if (formatMask != null) formatter = new SimpleDateFormat(formatMask) ; Date tempDate = new Date(dateValue.getTime()) ; retValue = formatter.format(tempDate); } return retValue; } // get the next day public static String getNextDate(String curValue) { if (isEmpty(curValue)) return null ; String retValue ; Date curDate = convertToTimestamp(curValue) ; Calendar calendar = Calendar.getInstance() ; calendar.setTime(curDate); calendar.add(Calendar.DATE, 1 ); Date nextDate = calendar.getTime() ; Timestamp retTimestamp = new Timestamp(nextDate.getTime()); retValue =convertToString(retTimestamp, Locale.ENGLISH) ; return retValue ; } // get the next day public static String getNextDate(String curValue, Locale userLocale) { String retValue ; if (FieldHelper.isEmpty(curValue)) return "" ; Date curDate = convertToTimestamp(curValue, userLocale ) ; Calendar calendar = Calendar.getInstance() ; calendar.setTime(curDate); calendar.add(Calendar.DATE, 1 ); Date nextDate = calendar.getTime() ; Timestamp retTimestamp = new Timestamp(nextDate.getTime()); retValue =convertToString(retTimestamp, userLocale) ; return retValue ; } public static String isChecked(String value) { String returnValue = " " ; if ("Y".equalsIgnoreCase(value.trim())) returnValue = "checked" ; return returnValue ; } public static boolean isValidPostalCode(String mpcode){ if( mpcode.length()==6 ) { if (!Character.isLetter(mpcode.charAt(0))) return false; if (!Character.isDigit(mpcode.charAt(1))) return false; if (!Character.isLetter(mpcode.charAt(2))) return false; if (!Character.isDigit(mpcode.charAt(3))) return false; if (!Character.isLetter(mpcode.charAt(4))) return false; if (!Character.isDigit(mpcode.charAt(5))) return false; return true; } return false; } public static boolean isValidZipCode(String mpcode){ boolean flag=true; try { if( mpcode.length()==5 ) flag = isNumeric(mpcode) ; else throw new Exception(); } catch(Exception e) { flag = false; } return flag; } public static boolean isNumeric(String value){ boolean flag=true; try { for (int i =0 ; i < value.length(); i++) if( !Character.isDigit(value.charAt(i))) throw new Exception(); } catch (Exception e) { flag = false; } return flag ; } public static boolean isAlphanumeric(String value){ boolean flag=true; try { for (int i =0 ; i < value.length(); i++) { if( !Character.isDigit(value.charAt(i)) && !Character.isLetter(value.charAt(i)) ) throw new Exception(); } } catch (Exception e) { flag = false; } return flag ; } public static boolean isEvenNumber(String value) { boolean flag = false ; try { long m = Long.parseLong(value) ; if ((m%2) == 0) flag = true ; } catch (Exception e) { flag = false; } return flag ; } public static boolean isInRange(long valueFrom, long valueTo, long value) { if ( value<=valueTo && value>=valueFrom) return true ; else return false ; } /** * check date * return 0 : valid value * return 1 : format is wrong * return 2 : format is OK but value is not valid, eg. 40-Jan-2001 */ public static int isValidDate(String inputDate) { int retValue = 0 ; SimpleDateFormat formatter=null; formatter = new SimpleDateFormat("dd-MMM-yyyy"); try { Date returnDate = formatter.parse(inputDate); if ( formatter.format(returnDate).equalsIgnoreCase(inputDate) == false) { retValue = 2 ; } } catch (ParseException e){ retValue = 1 ; } return retValue ; } public static String getCurrentDate() { Timestamp curDate = new Timestamp(System.currentTimeMillis()); return (FieldHelper.convertToString(curDate, Locale.ENGLISH)) ; } public static String getCurrentDate(Locale userLocale) { Timestamp curDate = new Timestamp(System.currentTimeMillis()); return (FieldHelper.convertToString(curDate, userLocale)) ; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -