Package Hk.buttonwood.ops.common;
Import Java.math.BigDecimal;
Import Java.text.DecimalFormat;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import Org.apache.commons.lang.StringUtils;
public class Fmtutils {
Private static final String pattern = "Yyyy-mm-dd HH:mm:ss";
private static final String pattern1 = "YYYYWW";
private static final String pattern2 = "YYYY-MM-DD";
private static final String pattern3 = "MM/DD";
private static final String pattern4 = "Mm/dd hh:mm";
private static final String pattern5 = "YYYYMMDD";
private static final String pattern6 = "Hhmmssss";
private static final String pattern7 = "HHmm";
private static final String Pattern8 = "YYMMDD";
Static final String m = "M";
Static final String k = "k";
public static String FormatDate (Date d) {
if (d = = null) {
Return "";
}
SimpleDateFormat SDF = new SimpleDateFormat (pattern);
Return Sdf.format (d);
}
public static String Formatdateweekinyear (Date d) {
if (d = = null) {
Return "";
}
SimpleDateFormat sdf1 = new SimpleDateFormat (PATTERN1);
Return Sdf1.format (d);
}
public static String FormatPercent (BigDecimal b) {
if (b = = null) {
Return "";
} else {
String pattern = "0.00";
DecimalFormat df = new DecimalFormat (pattern);
Return Df.format (B.doublevalue () * 100) + "%";
}
}
public static String Formatdatewithouttime (Date d) {
if (d = = null) {
Return "";
}
SimpleDateFormat sdf2 = new SimpleDateFormat (PATTERN2);
Return Sdf2.format (d);
}
public static String Formatdateshort (Date d) {
if (d = = null) {
Return "";
}
SimpleDateFormat sdf3 = new SimpleDateFormat (PATTERN3);
Return Sdf3.format (d);
}
public static String Formatdatemiddle (Date d) {
if (d = = null) {
Return "";
}
SimpleDateFormat sdf4 = new SimpleDateFormat (PATTERN4);
Return Sdf4.format (d);
}
public static String FORMATDATEYYYYMMDD (Date d) {
if (d = = null) {
Return "";
}
SimpleDateFormat sdf5 = new SimpleDateFormat (PATTERN5);
Return Sdf5.format (d);
}
public static String FORMATDATEYYMMDD (Date d) {
if (d = = null) {
Return "";
}
SimpleDateFormat sdf8 = new SimpleDateFormat (PATTERN8);
Return Sdf8.format (d);
}
public static Date int2date (Integer i) {
SimpleDateFormat sdf5 = new SimpleDateFormat (PATTERN5);
try {
Return Sdf5.parse (string.valueof (i));
} catch (ParseException e) {
return null;
}
}
public static Integer Date2int (Date d) {
if (d = = null) {
return null;
}
SimpleDateFormat sdf5 = new SimpleDateFormat (PATTERN5);
String s = sdf5.format (d);
return integer.valueof (s);
}
public static String Formatdatehhmmssss (Date d) {
if (d = = null) {
Return "";
}
SimpleDateFormat Sdf6 = new SimpleDateFormat (PATTERN6);
String s = sdf6.format (d);
Return Stringutils.left (S, 8);
}
public static String formatdatehhmm (Date d) {
if (d = = null) {
Return "";
}
SimpleDateFormat sdf7 = new SimpleDateFormat (PATTERN7);
Return Sdf7.format (d);
}
Public static String blank (Object o) {
if (o = = null) {
Return ' * blank * ';
}
if (Stringutils.isempty (string.valueof (o))) {
Return "* blank *";
}
Return Stringutils.isempty (o.tostring ())? "* Blank *": o.tostring ();
}
/*
* Specify precision formatting bigdemcial
* @param: Corresponding value
* @param: Precision, #.00 two bits after decimal
* @return: Return formatted data
*/
public static string Formatdecimalwithscale (string value,string scale) {
string v=null;
try{
BigDecimal bigdecimal=new BigDecimal (value);
V=new Java.text.DecimalFormat (scale). Format (BigDecimal);
} catch (Exception e) {
v= "0.0000000000";
E.printstacktrace ();
}
return v;
}
public static String empty (Object o) {
if (o = = null) {
return "";
}
if (Stringutils.isempty (string.valueof (o))) {
return "";
}
Return Stringutils.isempty (o.tostring ())? "": o.tostring ();
}
public static String Hold3decimal (BigDecimal D) {//reserved 3-bit decimal digits, by rounding
try {
String pattern = "0.000";
DecimalFormat df = new DecimalFormat (pattern);
String s = df.format (d);
if (S.equals ("0.000")) {
s = "0.000";
}
return s;
} catch (Exception ex) {
Return "";
}
}
public static String Hold2decimal (BigDecimal D) {//reserved 2-bit decimal digits, by rounding
try {
String pattern = "0.00";
DecimalFormat df = new DecimalFormat (pattern);
String s = df.format (d);
if (S.equals ("0.00")) {
s = "0.00";
}
return s;
} catch (Exception ex) {
Return "";
}
}
public static String Hold1decimal (BigDecimal D) {//reserved 1-bit decimal digits, by rounding
try {
String pattern = "0.0";
DecimalFormat df = new DecimalFormat (pattern);
String s = df.format (d);
if (S.equals ("0.00")) {
s = "0.00";
}
return s;
} catch (Exception ex) {
Return "";
}
}
public static String Formatdecimal (BigDecimal D) {//formatted digital display
2342565.555->2,342,565.56
try {
String pattern = "##,# #0.00";
DecimalFormat Declimalformat = new DecimalFormat (pattern);
String s = Declimalformat.format (D.doublevalue ());
if (S.equals ("0.00")) {
s = "0.00";
}
return s;
} catch (Exception ex) {
Return "";
}
}
public static String Hold0decimal (BigDecimal D) {//formatted digital display
try {
String pattern = "# # # #0. # #";
DecimalFormat Declimalformat = new DecimalFormat (pattern);
String s = Declimalformat.format (D.doublevalue ());
if (S.equals ("0.00")) {
s = "0.00";
}
return s;
} catch (Exception ex) {
Return "";
}
}
public static String Nodecimal (BigDecimal D) {//reserved integers only
try {
String pattern = "# # #0";
DecimalFormat Declimalformat = new DecimalFormat (pattern);
String s = Declimalformat.format (D.doublevalue ());
if (S.equals ("0")) {
s = "0";
}
return s;
} catch (Exception ex) {
Return "";
}
}
public static String Formatint (BigDecimal D) {//formatted digital display
2342565.555->2,342,565.56
try {
String pattern = "##,# #0";
DecimalFormat Declimalformat = new DecimalFormat (pattern);
String s = Declimalformat.format (D.doublevalue ());
if (S.equals ("0")) {
s = "0";
}
return s;
} catch (Exception ex) {
Return "";
}
}
public static String Formatint (Integer D) {//formatted digital display
2342565.555->2,342,565.56
try {
String pattern = "##,# #0";
DecimalFormat Declimalformat = new DecimalFormat (pattern);
String s = declimalformat.format (d);
if (S.equals ("0")) {
s = "0";
}
return s;
} catch (Exception ex) {
Return "";
}
}
public static String Formatlong (Long D) {//formatted digital display
2342565.555->2,342,565.56
try {
String pattern = "##,# #0";
DecimalFormat Declimalformat = new DecimalFormat (pattern);
String s = declimalformat.format (d);
if (S.equals ("0")) {
s = "0";
}
return s;
} catch (Exception ex) {
Return "";
}
}
public static String Shortqty (Integer qty) {//converts quantity to format, 1000->1k,1000000->1m
try {
String retVal = "";
Double d = 0.0;
if (Qty.doublevalue () >= 1000000) {//greater than 1000000
D = qty.doublevalue ()/1000000;
if ((d *)/((int) d * 10)! = 1)
RetVal = (new Double (d). toString () + M);
Retval=formatdecimal (New BigDecimal (d)) +m;
Else
RetVal = new Integer ((int) d). ToString () + M;
return retVal;
}
if (Qty.doublevalue () >= 1000) {//greater than 1000
D = Qty.doublevalue ()/1000;
if ((d *)/((int) d * 10)! = 1)
RetVal = (new Double (d). toString () + K);
Else
RetVal = new Integer ((int) d). ToString () + K;
return retVal;
}
if (Qty.doublevalue () <=-1000000) {//less than-1000000
D = qty.doublevalue ()/1000000;
if ((d *)/((int) d * 10)! = 1)
RetVal = (new Double (d). toString () + M);
Else
RetVal = new Integer ((int) d). ToString () + M;
return retVal;
}
if (Qty.doublevalue () <=-1000) {//less than 1000
D = Qty.doublevalue ()/1000;
if ((d *)/((int) d * 10)! = 1)
RetVal = (new Double (d). toString () + K);
Else
RetVal = new Integer ((int) d). ToString () + K;
return retVal;
}
if (Qty.doublevalue ()-Math.floor (Qty.doublevalue ()) = = 0)//integer Returns an integer with decimal return decimal
RetVal = (New Long (Qty.longvalue ()). ToString ());
Else
RetVal = (New Double (Qty.doublevalue ()). ToString ());
return retVal;
} catch (Exception ex) {
Return "";
}
}
public static void Main (string[] args) {
Date d = new Date ();
System.out.println (d);
System.out.println (Formatdatehhmmssss (d));
}
}
Data Formatting helper Classes