Tuesday, September 7, 2010
Most commonly used Java method or Function
****************************************************************************************************************************************
public static String getDisplayDate(String dt_str) {
if (dt_str != null) {
int mm, yy;
String yys = "", mms = "", dds = "";
Calendar cal = Calendar.getInstance();
cal.setTime(java.sql.Date.valueOf(dt_str));
dds = "" + cal.get(Calendar.DATE);
mm = cal.get(Calendar.MONTH) + 1;
mms = "" + mm;
yy = cal.get(Calendar.YEAR);
yys = "" + yy;
if (dds.length() < 2)
dds = "0" + dds;
if (mms.length() < 2)
mms = "0" + mms;
return dds + "/" + mms + "/" + yys;
} else
return null;
}
****************************************************************************************************************************************
public static String getDisplayDate(Date dt_str) {
return getDisplayDate(dt_str + "");
}
****************************************************************************************************************************************
public static String getInstancePath(javax.servlet.http.HttpServletRequest request) {
String servername = request.getRequestURI();
servername = servername.substring(0, servername.lastIndexOf('/'));
return "http://"
+ request.getServerName()
+ ":"
+ request.getServerPort()
+ servername;
}
****************************************************************************************************************************************
public static String getServerPath() {
ResourceBundle rbundle = ResourceBundle.getBundle("Application");
String serverName = rbundle.getString("servername");
String serveruri = rbundle.getString("serveruri");
return "http://" + serverName + "/" + serveruri;
}
****************************************************************************************************************************************
public static String getPortalURL() {
ResourceBundle rbundle = ResourceBundle.getBundle("Application");
return rbundle.getString("portaldoclink");
}
****************************************************************************************************************************************
public static String intArrToString(int[] arr, String sep) {
StringBuffer str = new StringBuffer();
if (arr != null) {
for (int i = 0; i < arr.length; i++) {
str.append(arr[i]);
str.append(sep);
}
str.setLength(str.length() - sep.length());
return str.toString();
} else {
return null;
}
}
****************************************************************************************************************************************
public static String remNewLineChar(String checkStr) {
String finalstr = "";
if (checkStr != null)
for (int i = 0; i < checkStr.length(); i++) {
if ((checkStr.charAt(i) == 13) && (checkStr.charAt(i + 1) == 10)) {
i++;
finalstr += "\\n";
} else
finalstr += checkStr.charAt(i);
}
return finalstr;
}
****************************************************************************************************************************************
public static String[] splitstr(String inpstr, String sep) {
if (inpstr != null) {
StringTokenizer str = new StringTokenizer(inpstr, sep);
int count = str.countTokens();
String arr_str[] = new String[count];
for (int i = 0; i < count; i++) {
arr_str[i] = str.nextToken(sep);
}
return arr_str;
} else
return null;
}
****************************************************************************************************************************************
public static int[] toIntArr(String[] strarr) throws Exception {
int a[] = null;
if (strarr != null) {
a = new int[strarr.length];
for (int i = 0; i < strarr.length; i++) {
try {
if (strarr[i] != null && !(strarr[i].trim().equals("")))
a[i] = Integer.parseInt(strarr[i].trim());
else
a[i] = 0;
} catch (Exception ex) {
throw new Exception("Error while trying to parseInt " + strarr[i]);
}
}
}
return a;
}
****************************************************************************************************************************************
public static String toString(String str) {
if ((str != null) && !(str.trim().equals(""))) {
int first = 0;
int next = 0;
int indexap = 0;
str = str.trim();
while ((!(next < 0)) && (next < str.length())) {
indexap = str.indexOf("'", first);
if (!(indexap < 0)) {
str = str.substring(0, indexap) + "'" + str.substring(indexap);
}
next = indexap;
first = next + 2;
}
first = 0;
next = 0;
indexap = 0;
str = str.trim();
while ((!(next < 0)) && (next < str.length())) {
indexap = str.indexOf("\"", first);
if (!(indexap < 0)) {
str = str.substring(0, indexap) + "\"" + str.substring(indexap);
}
next = indexap;
first = next + 2;
}
return "'" + str + "'";
} else {
return null;
}
}
****************************************************************************************************************************************
public String toTrim(String str) {
int next = 0;
int indexap = 0;
str = str.trim();
while ((!(next < 0)) && (next < str.length())) {
indexap = str.indexOf(" ");
if (!(indexap < 0)) {
if (str.substring(indexap) != null
&& !(str.substring(indexap).trim().equals(""))) {
str = str.substring(0, indexap) + " " + str.substring(indexap).trim();
}
}
next = indexap;
}
while ((!(next < 0)) && (next < str.length())) {
indexap = str.indexOf("\n");
if (!(indexap < 0)) {
if (str.substring(indexap) != null
&& !(str.substring(indexap).trim().equals(""))) {
str = str.substring(0, indexap) + " " + str.substring(indexap).trim();
}
}
next = indexap;
}
while ((!(next < 0)) && (next < str.length())) {
indexap = str.indexOf("\r");
if (!(indexap < 0)) {
if (str.substring(indexap) != null
&& !(str.substring(indexap).trim().equals(""))) {
str = str.substring(0, indexap) + " " + str.substring(indexap).trim();
}
}
next = indexap;
}
return str;
}
****************************************************************************************************************************************
public int getNoOfdays(Timestamp t1, Timestamp t2) {
try {
if (t1 == null)
t1 = getCurrentTS();
if (t2 == null)
t2 = getCurrentTS();
} catch (Exception e) {
}
Long l1 = new Long(t1.getTime());
Long l2 = new Long(t2.getTime());
Double d2 = new Double((l1.doubleValue() - l2.doubleValue()) / 86400000);
return d2.intValue();
}
****************************************************************************************************************************************
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment