HDUtil.java

Go to the documentation of this file.
00001 package cedar.hepdata.util;
00002 
00003 import java.text.DecimalFormat;
00004 import java.util.Date;
00005 import java.io.File;
00006 import java.io.FileWriter;
00007 import java.io.IOException;
00008 
00009 import org.apache.commons.lang.time.DateUtils;
00010 import java.text.ParseException;
00011 
00019 public class HDUtil {
00020 
00021 
00029     public static String diff(Date end, Date start) {
00030         return getTime(end.getTime() - start.getTime());
00031     }
00032 
00034     private static String getTime(long time) {
00035         long mins = Math.round(Math.floor(time / 60000));
00036         double secs = (time - (mins * 60000)) / 1000.0;
00037         DecimalFormat decFormat = new DecimalFormat("##0.###");
00038         String rtn = mins + " minutes and " + decFormat.format(secs) + " seconds";
00039 
00040         return rtn;
00041     }
00042 
00044     public static Date dateFromString(String timestamp) throws HDException {
00045         try {
00046             String[] formats = {
00047                 "yyyy-MM-dd HH:mm:ss",
00048                 "yyyy.MM.dd HH:mm:ss",
00049                 "yyyy/MM/dd HH:mm:ss",
00050                 "dd/MM/yyyy HH:mm:ss",
00051                 "dd MMM yyyy HH:mm:ss",
00052                 "yyyy-MM-dd",
00053                 "yyyy.MM.dd",
00054                 "yyyy/MM/dd",
00055                 "dd/MM/yyyy",
00056                 "dd MMM yyyy"
00057             };
00058             return DateUtils.parseDate(timestamp, formats);
00059         } catch (ParseException e) {
00060             throw new HDException(e);
00061         }
00062     }
00063 
00065     public static String getIntForm(Double dbl) {
00066         String dblString = dbl.toString();
00067 
00068         //  System.out.println("Input String:"+dblString);
00069         String mString = "";
00070         int exp = 0;
00071 
00072         if (dblString.indexOf("E") > -1) {
00073             //9.0E-4
00074             //1.378E05
00075             exp = Integer.parseInt(dblString.substring(dblString.indexOf("E") + 1));
00076             dblString = dblString.substring(0, dblString.indexOf("E"));
00077         }
00078 
00079         int len = dblString.length();
00080 
00081         if (dblString.indexOf(".") > -1) {
00082             String s = dblString;
00083 
00084             while (s.endsWith("0")) {
00085                 s = s.substring(0, s.length() - 1);
00086             }
00087 
00088             exp = -s.length() + s.indexOf(".") + 1 + exp;
00089             mString = s.substring(0, s.indexOf(".")) + s.substring(s.indexOf(".") + 1);
00090 
00091             //System.out.println("Is decimal, mString,exp "+mString+","+exp);
00092         } else {
00093             String s = dblString;
00094             int c = 0;
00095 
00096             while (s.endsWith("0") && !s.equals("")) {
00097                 c++;
00098                 s = s.substring(0, s.length() - 1);
00099             }
00100 
00101             mString = s;
00102             exp = c + exp;
00103 
00104             //System.out.println("Is not decimal, mString,exp "+mString+","+exp);
00105         }
00106 
00107         //System.out.println("Return String "+mString+"E"+exp);
00108         return mString + "E" + exp;
00109     }
00110 
00111 
00116     public static String stripBrackets(String in) {
00117         if (in == null) {
00118             return null;
00119         }
00120 
00121         char[] charArray = (in.trim()).toCharArray();
00122         StringBuffer out = new StringBuffer();
00123 
00124         for (int i = 0; i < charArray.length; i++) {
00125             char test = charArray[i];
00126 
00127             if ((test != '(') && (test != ')') && (test != '*') && (test != '\"') &&
00128                     (test != '\'')) {
00129                 out.append(test);
00130             }
00131         }
00132 
00133         return out.toString();
00134 
00140     }
00141 
00142 
00149     public static void toFile(String mystr, String filedir, String filename) {
00150         try {
00151             File f = new File(filedir, filename);
00152             FileWriter fw = new FileWriter(f);
00153             fw.write(mystr);
00154             fw.close();
00155         } catch (IOException e) {
00156             System.err.println(e);
00157         }
00158     }
00159 
00160 }

Generated on Tue Apr 21 15:54:54 2009 for HepData common classes by  doxygen 1.5.5