Tuesday, September 7, 2010
Most commonly used Java method or Function
****************************************************************************************************************************************
public static String arrToString(String[] arr) {
String str = "";
if (arr != null) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] != null) {
if (str.trim().equals(""))
str += "" + arr[i];
else
str += "~~!!~~" + arr[i];
}
}
return str;
} else {
return null;
}
}
****************************************************************************************************************************************
public static boolean compareTimestamp(java.sql.Timestamp t1)
throws java.lang.Exception {
// returns false if t1 TS is less than current TS t2
if (getCurrentTS().after(t1))
return false;
else
return true;
}
****************************************************************************************************************************************
/**
* @param beforeRound
* @param decimalPlace
* @return
* Ravi Shekhar
*/
public static double toRoundValue(double beforeRound, int decimalPlace) {
if ((beforeRound + "").length() > 5) {
java.math.BigDecimal bd = new java.math.BigDecimal(beforeRound);
bd = bd.setScale(decimalPlace, java.math.BigDecimal.ROUND_UP);
return bd.doubleValue();
} else {
return beforeRound;
}
}
****************************************************************************************************************************************
public static String currentDateTime() throws Exception {
//java.util.Date date = new java.util.Date();
Calendar cal = Calendar.getInstance();
String dds = "" + cal.get(Calendar.DATE);
int mm = cal.get(Calendar.MONTH) + 1;
String mms = "" + mm;
int yy = cal.get(Calendar.YEAR);
String hhs = "" + cal.get(Calendar.HOUR_OF_DAY);
String mins = "" + cal.get(Calendar.MINUTE);
String secs = "" + cal.get(Calendar.SECOND);
String msec = "" + cal.get(Calendar.MILLISECOND);
if (dds.length() < 2)
dds = "0" + dds;
if (mms.length() < 2)
mms = "0" + mms;
if (cal.get(Calendar.HOUR_OF_DAY) < 10)
hhs = "0" + hhs;
if (cal.get(Calendar.MINUTE) < 10)
mins = "0" + mins;
if (cal.get(Calendar.SECOND) < 10)
secs = "0" + secs;
return yy + "-" + mms + "-" + dds + " " + hhs + ":" + mins + ":" + secs;
}
****************************************************************************************************************************************
public static String currentDateTime1() throws Exception {
//java.util.Date date = new java.util.Date();
Calendar cal = Calendar.getInstance();
String dds = "" + cal.get(Calendar.DATE);
int mm = cal.get(Calendar.MONTH) + 1;
String mms = "" + mm;
int yy = cal.get(Calendar.YEAR);
String hhs = "" + cal.get(Calendar.HOUR_OF_DAY);
String mins = "" + cal.get(Calendar.MINUTE);
String secs = "" + cal.get(Calendar.SECOND);
String msec = "" + cal.get(Calendar.MILLISECOND);
if (dds.length() < 2)
dds = "0" + dds;
if (mms.length() < 2)
mms = "0" + mms;
if (cal.get(Calendar.HOUR_OF_DAY) < 10)
hhs = "0" + hhs;
if (cal.get(Calendar.MINUTE) < 10)
mins = "0" + mins;
if (cal.get(Calendar.SECOND) < 10)
secs = "0" + secs;
if (cal.get(Calendar.MILLISECOND) < 10)
msec = "0" + msec;
return yy
+ "-"
+ mms
+ "-"
+ dds
+ " "
+ hhs
+ ":"
+ mins
+ ":"
+ secs
+ ":"
+ msec;
}
****************************************************************************************************************************************
/**
* Insert the method's description here.
* Creation date: (7/16/02 11:35:53 AM)
*/
public static String currentDate(String date_format) throws Exception {
Calendar cal = Calendar.getInstance();
String dds = "" + cal.get(Calendar.DATE);
int mm = cal.get(Calendar.MONTH) + 1;
String mms = "" + mm;
int yy = cal.get(Calendar.YEAR);
String yys = "" + yy;
if (dds.length() < 2)
dds = "0" + dds;
if (mms.length() < 2)
mms = "0" + mms;
return dds + "/" + mms + "/" + yys;
}
****************************************************************************************************************************************
public static java.sql.Timestamp getCurrentTS() throws Exception {
return java.sql.Timestamp.valueOf(currentDateTime());
}
****************************************************************************************************************************************
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment