ENDRPrint_12CRelease/.svn/pristine/39/39e9446c49343f7c022cfea72784b315b49c907d.svn-base

874 lines
27 KiB
Plaintext
Raw Permalink Normal View History

2024-08-14 10:58:03 +07:00
package th.co.muangthai.endrprint.util;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Vector;
public class Util {
public static String genID(int len) {
String result = "";
while (result.length() < len) {
result += (int) (Math.random() * 9);
}
if (result.length() > len) {
result = result.substring(0, len);
}
return result;
}
/**
* Insert the method's description here. Creation date: (10/9/2003 15:31:18)
*/
public static String MS874ToUnicode(String value) {
StringBuffer strTemp = new StringBuffer(value);
for (int i = 0; i < value.length(); i++) {
int code = strTemp.charAt(i);
if (161 <= code && code <= 251)
strTemp.setCharAt(i, (char) (code + 3424));
}
return strTemp.toString();
}
public static String Unicode2ASCII(String unicode) { // แปลง Unicode เป็น ASCII
StringBuffer ascii = new StringBuffer(unicode); // กำหนดพื้นที่ใช้งานชั่วคราว
int code;
for (int i = 0; i < unicode.length(); i++) { // ลูปเท่าจำนวนตัวอักษร
code = (int) unicode.charAt(i); // อ่านค่ารหัสที่ละตัวอักษร
if ((0xE01 <= code) && (code <= 0xE5B)) // ตรวจสอบว่าอยู่ในช่วงภาษาไทยของ Unicode หรือไม่
ascii.setCharAt(i, (char) (code - 0xD60)); // หากใช้แปลงเป็นภาษาไทยในช่วงของ ASCII
}
return ascii.toString(); // แปลงข้อมูลกลับไปเป็นแบบ String เพื่อใช้งานต่อไป
}
public static String ASCII2Unicode(String ascii) {
StringBuffer unicode = new StringBuffer(ascii);
int code;
for (int i = 0; i < ascii.length(); i++) {
code = (int) ascii.charAt(i);
if ((0xA1 <= code) && (code <= 0xFB)) // ตรวจสอบว่าอยู่ในช่วงภาษาไทยของ ASCII หรือไม่
unicode.setCharAt(i, (char) (code + 0xD60)); // หากใช้แปลงเป็นภาษาไทยในช่วงของ Unicode
}
return unicode.toString(); // แปลงข้อมูลกลับไปเป็นแบบ String เพื่อใช้งานต่อไป
}
/**
* Insert the method's description here. Creation date: (09/09/03 16:09:30)
*/
public static String replace(String str, String pattern, String replace) {
int slen = str.length();
int plen = pattern.length();
int s = 0, e = 0;
StringBuffer result = new StringBuffer(slen * 2);
char[] chars = new char[slen];
while ((e = str.indexOf(pattern, s)) >= 0) {
str.getChars(s, e, chars, 0);
result.append(chars, 0, e - s).append(replace);
s = e + plen;
}
str.getChars(s, slen, chars, 0);
result.append(chars, 0, slen - s);
return result.toString();
}
/**
* แทนที่ช่องว่าที่อยู่ระหว่างข้อความ
*
* @param str : ข้อความหลัก
* @param replace : ข้อความที่จะแทนค่า
* @return String
*/
public static String replaceEmptyInnerString(String str, String replace) {
if (trim(str).equals("")) return str;
try {
String result = trim(str);
String[] strArr = result.split(" ");
if (strArr.length > 1) {
result = "";
for (String s : strArr) {
if (!Util.trim(s).equals("")) result += Util.trim(s) + replace;
}
}
return result;
} catch (Exception e) {
e.printStackTrace();
return str;
}
}
/**
* select
*
* @param array
* @param min
* @param max
* @return
*/
private static int select(int array[], int min, int max) {
int index = min;
for (int i = min + 1; i <= max; ++i)
if (array[i] < array[index])
index = i;
return index;
}
/**
* sort
*
* @param array
* @param min
* @param max
*/
private static void sort(int array[], int min, int max) {
if (min == max) return;
// Find the smallest.
int index = select(array, min, max);
// Swap the smallest with the first.
int temp = array[min];
array[min] = array[index];
array[index] = temp;
// Sort the rest.
sort(array, min + 1, max);
}
/**
* sortArray
*
* @param array
*/
public static void sortArray(int array[]) {
sort(array, 0, array.length - 1);
}
/**
* UnicodeToMS874
*
* @param value
* @return
*/
public static String UnicodeToMS874(String value) {
StringBuffer strTemp = new StringBuffer(value);
for (int i = 0; i < value.length(); i++) {
int code = strTemp.charAt(i);
if (3585 <= code && code <= 3675)
strTemp.setCharAt(i, (char) (code - 3424));
}
return strTemp.toString();
}
/**
* unionArray
*
* @param p1
* @param p2
* @return
*/
@SuppressWarnings("unchecked")
public static int[] unionArray(int[] p1, int[] p2) {
Vector vc = new Vector();
int i = 0, j = 0, tmp = 0;
char chk = 'n';
vc.add("0");
for (i = 0; i < p1.length; i++) {
chk = 'n';
for (j = 1; j < vc.size(); j++) {
tmp = Integer.parseInt((String) vc.elementAt(j));
if (p1[i] == tmp) {
chk = 'y';
break;
}
}
if (chk == 'n') vc.add("" + p1[i]);
}
for (i = 0; i < p2.length; i++) {
chk = 'n';
for (j = 1; j < vc.size(); j++) {
tmp = Integer.parseInt((String) vc.elementAt(j));
if (p2[i] == tmp) {
chk = 'y';
break;
}
}
if (chk == 'n') vc.add("" + p2[i]);
}
int[] result = new int[vc.size() - 1];
for (i = 1; i < vc.size(); i++) result[i - 1] = Integer.parseInt((String) vc.get(i));
vc.clear();
return result;
}
/**
* unionArray
*
* @param p1
* @param p2
* @return
*/
@SuppressWarnings("unchecked")
public static String[] unionArray(String[] p1, String[] p2) {
Vector vc = new Vector();
int i = 0, j = 0;
char chk = 'n';
String tmp = "";
vc.add(" ");
for (i = 0; i < p1.length; i++) {
chk = 'n';
for (j = 1; j < vc.size(); j++) {
if (p1[i] != null) {
tmp = (String) vc.elementAt(j);
if (p1[i].equals(tmp)) {
chk = 'y';
break;
}
} else {
chk = 'y';
}
}
if (chk == 'n') vc.add(p1[i]);
}
for (i = 0; i < p2.length; i++) {
chk = 'n';
for (j = 1; j < vc.size(); j++) {
if (p2[i] != null) {
tmp = (String) vc.elementAt(j);
if (p2[i].equals(tmp)) {
chk = 'y';
break;
}
} else {
chk = 'y';
}
}
if (chk == 'n') vc.add(p2[i]);
}
String[] result = new String[vc.size() - 1];
for (i = 1; i < vc.size(); i++) result[i - 1] = (String) vc.get(i);
vc.clear();
return result;
}
/**
* @param tmp
* @return
*/
public static String HTML(String tmp) {
tmp = replace(tmp, "<", "&lt");
tmp = replace(tmp, ">", "&gt");
tmp = UnicodeToMS874(tmp);
return tmp;
}
/**
* @param source
* @param chr
* @return
*/
@SuppressWarnings("unchecked")
public static Vector split(String source, String chr) {
Vector tmp = new Vector();
int i = 0;
int index;
while (source.length() > 0) {
index = source.indexOf(chr);
switch (index) {
case -1:
tmp.add(source);
source = "";
break;
case 0:
tmp.add("");
source = source.substring(index + 1);
break;
default:
tmp.add(source.substring(0, index));
source = source.substring(index + 1);
break;
}
i++;
}
return tmp;
}
/**
* @param str
* @return
*/
public static String getDateFormatThai(String str) {
String result = "";
switch (str.length()) {
case 6:
result = thaiMonth((Integer.parseInt(str.substring(4, 6)) - 1))
+ " " + (Integer.parseInt(str.substring(0, 4)) + 543);
break;
case 8:
result = str.substring(6, str.length()) + " "
+ thaiMonth((Integer.parseInt(str.substring(4, 6)) - 1))
+ " " + (Integer.parseInt(str.substring(0, 4)) + 543);
break;
}
return (result);
}
/**
* @param i
* @return
*/
public static String thaiMonth(int i) {
String result = "";
switch (i) {
case 0:
result = "มกราคม";
break;
case 1:
result = "กุมภาพันธ์";
break;
case 2:
result = "มีนาคม";
break;
case 3:
result = "เมษายน";
break;
case 4:
result = "พฤษภาคม";
break;
case 5:
result = "มิถุนายน";
break;
case 6:
result = "กรกฎาคม";
break;
case 7:
result = "สิงหาคม";
break;
case 8:
result = "กันยายน";
break;
case 9:
result = "ตุลาคม";
break;
case 10:
result = "พฤศจิกายน";
break;
case 11:
result = "ธันวาคม";
break;
}
return (result);
}
/**
* (สำหรับข้อความ) Convert null to Empty
*
* @param str
* @return
*/
public static String trim(String str) {
return str == null ? "" : str.trim();
}
/**
* (สำหรับข้อความ) Convert null to Empty
*
* @param str
* @param defaultValue
* @return
*/
public static String trim(String str, String defaultValue) {
return str == null ? defaultValue : str.trim();
}
public static String[] MONTH = {"มกราคม", "กุมภาพันธ์", "มีนาคม",
"เมษายน", "พฤษภาคม", "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน",
"ตุลาคม", "พฤศจิกายน", "ธันวาคม"};
public static String DateTH(Date date) {
SimpleDateFormat dd = new SimpleDateFormat("dd", Locale.ENGLISH);
SimpleDateFormat mm = new SimpleDateFormat("MM", Locale.ENGLISH);
SimpleDateFormat yyyy = new SimpleDateFormat("yyyy", Locale.ENGLISH);
String strDate = (Integer.parseInt(dd.format(date)) + " "
+ MONTH[Integer.parseInt(mm.format(date)) - 1] + " " + yyyy
.format(date));
return strDate;
}
/**
* ใช้จัดรูปแบบวันที่ เวลา
*
* @param format
* @param date
* @return
*/
public static String strDateTime(String format, Date date) {
return strDateTime(format, date, Locale.ENGLISH);
}
public static String strDateTime(String format, Date date, Locale locale) {
SimpleDateFormat simpledateformat = new SimpleDateFormat(format, locale);
return simpledateformat.format(date);
}
public static String emptyForNull(String str) {
return emptyForNull(str, "");
}
public static String emptyForNull(String str, String emptyStr) {
return (str != null) ? str.trim() : emptyStr;
}
/**
* Member point and volume
*
* @param number
* @return
*/
public static String toPointNumber(BigDecimal number) {
if (number != null)
return Number("#,##0", number.longValue());
return "";
}
/**
* ใช้จัดรูปแบบตัวเลข
*
* @param format
* @param number
* @return
*/
public static String Number(String format, double number) {
return (new DecimalFormat(format).format(number));
}
public static String Number(double number) {
return (new DecimalFormat("#,##0.00").format(number));
}
/**
* ใช้จัดรูปแบบตัวเลข
*
* @param format
* @param number
* @return
*/
public static String Number(String format, long number) {
return (new DecimalFormat(format).format(number));
}
public static String Number(long number) {
return (new DecimalFormat("#,##0.00").format(number));
}
public static String Number(Long number) {
return (new DecimalFormat("#,##0.00").format(number != null ? number.longValue() : 0L));
}
public static String Number(String format, Long number) {
return (new DecimalFormat(format).format(number != null ? number.longValue() : 0L));
}
// ---> ใช้จัดรูปแบบตัวเลข
public static String Number(String format, int number) {
return (new DecimalFormat(format).format(number));
}
public static String Number(int number) {
return (new DecimalFormat("#,##0").format(number));
}
// ---> ใช้จัดรูปแบบตัวเลข
public static String Number(String format, float number) {
return (new DecimalFormat(format).format(number));
}
public static String Number(float number) {
return (new DecimalFormat("#,##0.00").format(number));
}
// ---> ใช้จัดรูปแบบตัวเลข
public static String Number(String format, BigDecimal number) {
return (new DecimalFormat(format).format(number != null ? number.doubleValue() : 0));
}
public static String Number(BigDecimal number) {
return (new DecimalFormat("#,##0.00").format(number != null ? number.doubleValue() : 0L));
}
// ---> ใช้จัดรูปแบบตัวเลข
public static int iNumber(String num) {
try {
//return (Integer.parseInt(num.trim().replaceAll(",", "")));
return (int) dNumber(num); // By. CHAi 2010-10-07
} catch (Exception e) {
return (0);
}
}
// ---> ใช้จัดรูปแบบตัวเลข
public static double dNumber(String num) {
try {
return (Double.parseDouble(num.trim().replaceAll(",", "")));
} catch (Exception e) {
return (0.00);
}
}
// ---> ใช้จัดรูปแบบตัวเลข
public static long lNumber(String num) {
try {
return (Long.parseLong(num.trim().replaceAll(",", "")));
} catch (Exception e) {
return (0);
}
}
// ---> ใช้จัดรูปแบบตัวเลข
public static Long LNumber(String num) {
try {
return (new Long(num.trim().replaceAll(",", "")));
} catch (Exception e) {
return (null);
}
}
// ---> ใช้ดึงข้อมูลวันที่ปัจจุบัน
public static Date getDate() {
return (new Date());
}
// ---> String Of int Only
private static void sort(String[] array, int min, int max) {
if (min == max) {
return;
}
// Find the smallest.
int index = select(array, min, max);
// Swap the smallest with the first.
int temp = iNumber(array[min]);
array[min] = array[index];
array[index] = String.valueOf(temp);
// Sort the rest.
sort(array, min + 1, max);
}
private static int select(String[] array, int min, int max) {
int index = min;
for (int i = min + 1; i <= max; ++i) {
if (iNumber(array[i]) < iNumber(array[index])) {
index = i;
}
}
return index;
}
public static void sortArray(String[] array) {
sort(array, 0, array.length - 1);
}
/**
* ใช้ จัดรูปแบบ String ที่มี tage HTML ให้ถูกต้อง ใช้สำหรับแสดงข้อความบนหน้าจอ
*
* @param str
* @return
*/
public static String toHtml(String str) {
try {
if (str == null) return "";
StringBuffer sf = new StringBuffer();
char c;
for (int i = 0; i < str.length(); i++) {
c = str.charAt(i);
if (c == '<') {
sf.append("&lt;");
} else if (c == '>') {
sf.append("&gt;");
} else if (c == '"') {
sf.append("&quot;");
} else if (c == '&') {
sf.append("&amp;");
} else {
sf.append(c);
}
}
return sf.toString();
} catch (Exception e) {
return "";
}
}
/**
* สำหรับเรียกข้อมูลวันที่+เวลา
*
* @param date : Date
* @param hour 0-23
* @param minute : 0-59
* @param sec : 0-59
* @param msec : 0-999
* @return
*/
public static Date getDate(Date date, int hour, int minute, int sec, int msec) {
Calendar cal = Calendar.getInstance(Locale.US);
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, sec);
cal.set(Calendar.MILLISECOND, msec);
return cal.getTime();
}
public static Date getDate(Date date, int hour, int minute, int sec) {
return getDate(date, hour, minute, 0);
}
public static Date getDate(int hour, int minute, int sec, int msec) {
return getDate(new Date(), hour, minute, sec, msec);
}
public static Date getDate(int hour, int minute, int sec) {
return getDate(new Date(), hour, minute, sec, 0);
}
//-----------------------------------------------------------------------------
// ADD By.CHAi 2011-02-21
//-----------------------------------------------------------------------------
public static Date ObjectToDate(Object obj) {
return ObjectToDate(obj, null);
}
public static Date ObjectToDate(Object obj, Date defaultValue) {
try {
if (obj == null) return defaultValue;
return (Date) obj;
} catch (Exception e) {
}
return null;
}
public static String ObjectToString(Object obj) {
return ObjectToString(obj, "");
}
public static String ObjectToString(Object obj, String defaultValue) {
String value = defaultValue;
try {
if (obj == null) return defaultValue;
value = obj.toString().trim();
} catch (Exception e) {
}
return value;
}
public static int ObjectToInt(Object obj) {
return ObjectToInt(obj, 0);
}
public static int ObjectToInt(Object obj, int defaultValue) {
int value = defaultValue;
try {
if (obj == null) return defaultValue;
value = Integer.parseInt(obj.toString().trim());
} catch (Exception e) {
}
return value;
}
public static Integer ObjectToInteger(Object obj) {
return ObjectToInteger(obj, null);
}
public static Integer ObjectToInteger(Object obj, Integer defaultValue) {
Integer value = defaultValue;
try {
if (obj == null) return defaultValue;
value = Integer.valueOf(obj.toString().trim());
} catch (Exception e) {
}
return value;
}
public static long ObjectToLong(Object obj) {
return ObjectToLong(obj, 0);
}
public static long ObjectToLong(Object obj, long defaultValue) {
long value = defaultValue;
try {
if (obj == null) return defaultValue;
value = Long.parseLong(obj.toString().trim());
} catch (Exception e) {
}
return value;
}
public static Long ObjectToObjectLong(Object obj) {
return ObjectToObjectLong(obj, null);
}
public static Long ObjectToObjectLong(Object obj, Long defaultValue) {
Long value = defaultValue;
try {
if (obj == null) return defaultValue;
value = Long.valueOf(obj.toString().trim());
} catch (Exception e) {
}
return value;
}
public static double ObjectToDouble(Object obj) {
return ObjectToDouble(obj, 0);
}
public static double ObjectToDouble(Object obj, double defaultValue) {
double value = defaultValue;
try {
if (obj == null) return defaultValue;
value = Double.valueOf(obj.toString().trim());
} catch (Exception e) {
}
return value;
}
public static BigDecimal ObjectToBigDecimal(Object obj) {
return ObjectToBigDecimal(obj, BigDecimal.ZERO);
}
public static BigDecimal ObjectToBigDecimal(Object obj, BigDecimal defaultValue) {
BigDecimal value = defaultValue;
try {
if (obj == null) return defaultValue;
if (obj instanceof BigDecimal) {
value = (BigDecimal) obj;
} else {
value = convertToBigDecimal(ObjectToDouble(obj), ObjectToDouble(obj));
}
} catch (Exception e) {
}
return value;
}
//-----------------------------------------------------------------------------
public static final NumberFormat DEFAULT_DECIMAL_FORMAT = new DecimalFormat("#.0#################");
public static final BigDecimal ZERO = new BigDecimal("0");
public static BigDecimal convertToBigDecimal(double a, double b) {
String s = DEFAULT_DECIMAL_FORMAT.format(a);
BigDecimal bd = new BigDecimal(s);
return convertToBigDecimal(bd, b);
}
public static BigDecimal convertToBigDecimal(BigDecimal a, double b) {
String s = DEFAULT_DECIMAL_FORMAT.format(b);
BigDecimal bd = new BigDecimal(s);
return convertToBigDecimal(a, bd);
}
public static BigDecimal convertToBigDecimal(BigDecimal a, BigDecimal b) {
if (a == null) return (b == null) ? ZERO : b;
return a.add(b);
}
//-----------------------------------------------------------------------------
public static final Locale TH_LOCALE = new Locale("th", "TH");
/**
* แปลง String เป็น Date
*
* @param locale
* @param format : รูปแบบของ strDate เช่น dd/MM/yyyy
* @param strDate : รูปแบบของ วันที่ เช่น 01/12/2555
* @param defaultValue
* @return
*/
public static Date strToDate(Locale locale, String format, String strDate, Date defaultValue) {
DateFormat df = new SimpleDateFormat(format, locale);
try {
return df.parse(strDate);
} catch (Exception e) {
return defaultValue;
}
}
/**
* แปลง String เป็น Date
*
* @param format : รูปแบบของ strDate เช่น dd/MM/yyyy
* @param strDate : รูปแบบของ วันที่ เช่น 01/12/2011
* @param defaultValue
* @return
*/
public static Date strToDateEng(String format, String strDate, Date defaultValue) {
return strToDate(Locale.US, format, strDate, defaultValue);
}
/**
* แปลง String เป็น Date
*
* @param format : รูปแบบของ strDate เช่น dd/MM/yyyy
* @param strDate : รูปแบบของ วันที่ เช่น 01/12/2011
* @return
*/
public static Date strToDateEng(String format, String strDate) {
return strToDate(Locale.US, format, strDate, null);
}
/**
* แปลง String เป็น Date
*
* @param format : รูปแบบของ strDate เช่น dd/MM/yyyy
* @param strDate : รูปแบบของ วันที่ เช่น 01/12/2555
* @param defaultValue
* @return
*/
public static Date strToDateThai(String format, String strDate, Date defaultValue) {
return strToDate(TH_LOCALE, format, strDate, defaultValue);
}
/**
* แปลง String เป็น Date
*
* @param format : รูปแบบของ strDate เช่น dd/MM/yyyy
* @param strDate : รูปแบบของ วันที่ เช่น 01/12/2555
* @return
*/
public static Date strToDateThai(String format, String strDate) {
return strToDate(TH_LOCALE, format, strDate, null);
}
//-----------------------------------------------------------------------------
public static void main(String[] args)
{
String value = "\"1,000.00\"";
value = value.replaceAll("[^.\\d]", "");
System.out.println(value);
Date startTime = new Date();
// try {
// Thread.sleep(2500);
// } catch (InterruptedException e) {
// e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
// }
Date endTime = new Date();
long diff = endTime.getTime() - startTime.getTime();
float diffSeconds = diff / 1000f ;
}
}