package us.or.state.database; // need to use to replace single quotes instead of .replace(), because // of string sent to the javascript "handleResponse". // The jsp will not compile for example, rs.getString(4).replace("'","\""), // or other combinations. // Replacing double quotes works: rs2.getString(4).replace('"','\"') /* Replace all instances of a String in a String. @param s String to alter. @param f String to look for. @param r String to replace it with, or null to just remove it. call it like this: String parsedReqParam = replace(reqParam,"\'","\'\'"); */ public String replace( String s, String f, String r ) { if (s == null) return s; if (f == null) return s; if (r == null) r = ""; int index01 = s.indexOf( f ); while (index01 != -1) { s = s.substring(0,index01) + r + s.substring(index01+f.length()); index01 += r.length(); index01 = s.indexOf( f, index01 ); } return s.trim(); } public String replaceCRLF(String s) { if (s == null) return s="--"; String r = ""; for (int i=0; i < s.length(); i++) { if (s.charAt(i) != '\n' && s.charAt(i) != '\r' && s.charAt(i) != '\t') { r += s.charAt(i); } } return r.trim(); } public String formatmoney(String s) { if (s == null) return null; double d = Double.valueOf(s.trim()).doubleValue(); DecimalFormat f = new DecimalFormat("#,##0.00"); String g = "$ " + f.format(d); return g; } public String formatlength(String s, int size) { String spacer=" "; String newspacer=""; String news=""; if (s == null) { for(int i=0;i<=size;i++) { newspacer += spacer; } s = newspacer; return s; } s = s.trim(); int cursize=(int)s.length(); if(cursize < size) { int diff=(size-cursize); for(int i=0;i size) { news = s.substring(0, size); s = news; } return s; } // strip last comma strSQL = strSQL.substring(0, strSQL.length() - 1); strSQL = strSQL.replaceAll("|","'"); ps.setDate(index, new java.sql.Date(javaDate.getTime())).toString() */ /*************************** Date: ***************************/ import java.util.Date; import java.text.SimpleDateFormat; import java.text.ParseException; public String formatdate(Date s) { if (s == null) return null; SimpleDateFormat d = new SimpleDateFormat("MM/dd/yyyy"); String e = d.format(s); return e; } { String datePattern = "dd-MM-yyyy"; SimpleDateFormat dateFormat = new SimpleDateFormat( datePattern ); String sDate_one = "22-09-1972"; String sDate_two = "23-10-2002"; Date date_one = null; Date date_two = null; try { date_one = dateFormat.parse( sDate_one ); } catch( ParseException e ) { System.out.println( "Date ["+sDate_one+"] must have format ["+datePattern+"] ! Error: "+e.getMessage() ); } try { date_two = dateFormat.parse( sDate_two ); } catch( ParseException e ) { System.out.println( "Date ["+sDate_two+"] must have format ["+datePattern+"] ! Error: "+e.getMessage() ); } if (date_one!=null && date_two!=null) { long diff_ms = Math.abs( date_two.getTime() - date_one.getTime() ); long diff_days = diff_ms / (24*60*60*1000); System.out.println( "Diff for "+sDate_one+" and "+sDate_two+" is "+diff_ms+" ms, or "+diff_days+" days." ); } } /****************************** Convert other type to String *******************************/ ASCII code to String int i = 64; String aChar = new Character((char)i).toString(); integer to String : int i = 42; String str = Integer.toString(i); or String str = "" + i double to String : String str = Double.toString(i); long to String : String str = Long.toString(l); float to String : String str = Float.toString(f); /****************************** Convert String to number *******************************/ String to integer : str = "25"; int i = Integer.valueOf(str).intValue(); or int i = Integer.parseInt(str); String to double : double d = Double.valueOf(str).doubleValue(); String to long : long l = Long.valueOf(str).longValue(); or long l = Long.parseLong(str); String to float : float f = Float.valueOf(str).floatValue(); decimal to binary : int i = 42; String binstr = Integer.toBinaryString(i); hexadecimal (String) to integer : int i = Integer.valueOf("B8DA3", 16).intValue(); or int i = Integer.parseInt("B8DA3", 16); /**********************/ decimal to hexadecimal : int i = 42; String hexstr = Integer.toString(i, 16); or String hexstr = Integer.toHexString(i); or (with leading zeroes and uppercase) public class Hex { public static void main(String args[]){ int i = 42; System.out.print (Integer.toHexString( 0x10000 | i).substring(1).toUpperCase()); } } integer to ASCII code (byte) char c = 'A'; int i = (int) c; // i will have the value 65 decimal To extract Ascii codes from a String String test = "ABCD"; for ( int i = 0; i < test.length(); ++i ) { char c = test.charAt( i ); int i = (int) c; System.out.println(i); } integer to boolean b = (i != 0); boolean to integer i = (b)?1:0; note :To catch illegal number conversion, try using the try/catch mechanism. try{ i = Integer.parseInt(aString); } catch(NumberFormatException e) { } } /**** Array ************************** Must follow three steps 1. Declaration. 2. Construction. 3. Initialization (java has default initial value). You can combine all three steps in one line. ***************************/ int[] ints; //primitive array. //OK format int ints[]; Dimentions[] dims; //object arrry float[][] twoDee; //two demintion array ints = new int[25]; //int ints[25] is not correct. int[] ints=new int[25]; float[] diameters = {1.1f, 1.3f, 1.3f}; //three steps together /***** Split a String *****/ String s3 = "Real-How-To"; String [] temp = null; temp = s3.split("-"); for (int i = 0 ; i < temp.length ; i++) System.out.println(temp[i]); /***** array and list convert *****/ // array to list Object[] array = new Object[]{"12","23","34"}; java.util.List list = Arrays.asList(array); //List to array Cast List to Array, you need indocate the object type 1) Object[] toArray() ; //return Object 2) Object[] toArray(Object[] a) //return a object in Type defined in the (Object[] a) , if size is not big enough, same type ofobject will be generated. List list = new ArrayList(); list.add("This"); list.add("is"); list.add("an"); list.add("example."); //String[] arr1 = (String[]) list.toArray(); // ClassCastException -- Object[] to String[] String[] myArr = new String[list.size()]; list.toArray(myArr); //convert and save data in myArr, and return myArr too String[] arr2 = (String[]) list.toArray(new String[0]); // Works fine //// more sample ///// meta is a list of Object SurveyMetaData, convert this List to array SurveyMetaData[] tempMetadata= new SurveyMetaData[meta.size()]; tempMetadata = (SurveyMetaData[])meta.toArray(tempMetadata)); //without = , tempMetadata hold the same value too /*****************************/ convert unil.Date to SQL Date Date curDate = sr.getCompletionDate(); java.sql.Date today = new java.sql.Date(curDate.getTime());