package com.topsun.posclient.common; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Properties; import org.eclipse.core.runtime.Platform; /** * @author LiLei * */ public class ProjectUtil { static SimpleDateFormat defaultFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); static SimpleDateFormat defaultTimeFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public static String getRuntimeClassPath() { String path = Platform.getInstanceLocation().getURL().getFile(); return path; } public static Date getDateByFormat(String dateStr, String custFormat) { SimpleDateFormat dataformat = new SimpleDateFormat(custFormat); Date date = null; try { date = dataformat.parse(dateStr); } catch (ParseException e) { e.printStackTrace(); } return date; } /** * @param startHour * @param endHour * @return */ public static boolean compareCurrentHour(int startHour, int endHour){ Calendar calendar = Calendar.getInstance(); int hour = calendar.get(Calendar.HOUR_OF_DAY); if(hour >= startHour && hour <= endHour){ return true; }else{ return false; } } public static String getCurrentDateByFormat(String format){ if(null == format){ format = "yyyy-MM-dd HH:mm:ss"; } SimpleDateFormat dataformat = new SimpleDateFormat(format); return dataformat.format(new Date()); } /** * @param startDate * @param endDate * @return */ public static boolean compareCurrentDate(Date startDate, Date endDate){ Calendar calendar = Calendar.getInstance(); Date currentDate = ProjectUtil.getDateByFormat("yyyyMMdd"); Date sDate = ProjectUtil.getDateByFormat(ProjectUtil.getDateString(startDate), "yyyyMMdd"); Date eDate = ProjectUtil.getDateByFormat(ProjectUtil.getDateString(endDate), "yyyyMMdd"); if(currentDate.compareTo(sDate) == 1 || currentDate.compareTo(eDate) == 1){ return true; }else if(calendar.after(sDate) && calendar.before(eDate)){ return true; }else{ return false; } } public static Date getDateByFormat(String custFormat) { SimpleDateFormat dataformat = new SimpleDateFormat(custFormat); Date date = null; try { date = dataformat.parse(dataformat.format(new Date())); } catch (ParseException e) { e.printStackTrace(); } return date; } public static Date getDate(String dateStr) { Date date = null; try { date = defaultFmt.parse(dateStr); } catch (ParseException e) { e.printStackTrace(); } return date; } public static String getDateString(Date date) { String dateStr = null; if(null == date){ dateStr = defaultFmt.format(new Date()); }else{ dateStr = defaultFmt.format(date); } return dateStr; } public static String getDateString(Date date, String custFormat) { SimpleDateFormat dataformat = new SimpleDateFormat(custFormat); String dateStr = null; if(null == date){ dateStr = dataformat.format(new Date()); }else{ dateStr = dataformat.format(date); } return dateStr; } public static Date getDateTime(String dateStr) { Date date = null; try { date = defaultTimeFmt.parse(dateStr); } catch (ParseException e) { e.printStackTrace(); } return date; } public static String getDateTime(Date date) { String dateStr = null; if(null == date){ dateStr = defaultTimeFmt.format(new Date()); }else{ dateStr = defaultTimeFmt.format(date); } return dateStr; } private static Properties getProperties(String fileName) { InputStream in = ProjectUtil.class.getClassLoader() .getResourceAsStream(fileName); Properties p = new Properties(); try { p.load(in); } catch (IOException e) { e.printStackTrace(); } return p; } public static Object getPropertiesValue(String keyName, String fileName) { Properties p = getProperties(fileName); return p.get(keyName); } public static String readValue(String filePath, String key) throws Exception { Properties props = new Properties(); FileInputStream in = new FileInputStream(filePath); props.load(in); String value = props.getProperty(key); return value; } /** * String * @param value * @return */ public static BigDecimal getBigDecimal(String value) { if (null != value) { value = value.trim(); } else { value = "0"; } BigDecimal decimal = new BigDecimal(value); return decimal; } public static Calendar getCalendar(Date date) { if (null == date) { date = new Date(); } Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal; } public static Date getDate(Calendar calendar) { if (null == calendar) { return new Date(); } try{ return calendar.getTime(); }catch(Exception e){ return ProjectUtil.getDateByFormat("yyyy-MM-dd HH:mm:ss"); } } public static String getDateString(Calendar calendar) { if (null == calendar) { return ProjectUtil.getCurrentDateByFormat("yyyy-MM-dd"); } try{ return ProjectUtil.getDateString(calendar.getTime()); }catch(Exception e){ return ProjectUtil.getCurrentDateByFormat("yyyy-MM-dd HH:mm:ss"); } } /** * 生成单据编号 * @param type 单据编号类型 1、零售单据编号 2、旧金编号 * @return 当日编号累加 */ public static String generateDocNum(int type){ String currentDate = ProjectUtil.getCurrentDateByFormat("yyMMdd"); String initDocNum = ""; switch(type){ case AppConstants.DOC_TYPE_SALES: initDocNum = POSClientApp.get().getSysConfig().getDocNum(); break; case AppConstants.DOC_TYPE_OLDGOLD: initDocNum = POSClientApp.get().getSysConfig().getOgDocNum(); break; case AppConstants.DOC_TYPE_BOOKINGGOLD: initDocNum = POSClientApp.get().getSysConfig().getbDocNum(); break; case AppConstants.DOC_TYPE_GOLDBUYBACK: initDocNum = POSClientApp.get().getSysConfig().getgDocNum(); break; case AppConstants.DOC_TYPE_RETURNED: initDocNum = POSClientApp.get().getSysConfig().getrDocNum(); break; } StringBuffer ret = new StringBuffer(""); //本地保存的单据号年月日日期等于系统当前时间年月日;则单据顺序号加1,否则从1开始 if(initDocNum.substring(0, 6).equals(currentDate)){ String indexDocNum = initDocNum.substring(6, 10); Integer num = Integer.valueOf(indexDocNum); num = num+1; String numStr = String.valueOf(num); for(int i=0; i<4-numStr.length(); i++){ ret.append("0"); } ret.append(numStr); return currentDate+ret; }else{ return currentDate+"0001"; } } public static String formatAmount(String format, String value){ BigDecimal fmtValue = new BigDecimal(0); if(null == format || "".equals(format)){ format = ",##0.00"; } if(null != value){ fmtValue = new BigDecimal(value); } DecimalFormat myFormatter = new DecimalFormat(format); String formatAmount = myFormatter.format(fmtValue); return formatAmount; } public static String formatAmount(String format, BigDecimal value){ if(null == format || "".equals(format)){ format = ",##0.00"; } if(null == value){ value = new BigDecimal(0); } DecimalFormat myFormatter = new DecimalFormat(format); String formatAmount = myFormatter.format(value); return formatAmount; } public static String formatNetnessAmount(String format, BigDecimal value){ if(null == format || "".equals(format)){ format = ",##0.000"; } if(null == value){ value = new BigDecimal(0); } DecimalFormat myFormatter = new DecimalFormat(format); String formatAmount = myFormatter.format(value); return formatAmount; } public static BigDecimal formatString(String value){ if(null == value || "".equals(value)){ return new BigDecimal(0); } value = value.replace(",", ""); return new BigDecimal(value); } /** 大写数字 */ private static final String[] NUMBERS = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; /** 整数部分的单位 */ private static final String[] IUNIT = { "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟" }; /** 小数部分的单位 */ private static final String[] DUNIT = { "角", "分" }; /** * 得到大写金额。 */ public synchronized static String toChinese(String str) { boolean isNegative = false; if (str.startsWith("-")) { isNegative = true; str = str.substring(1); } BigDecimal f = new BigDecimal(str); if (f.compareTo(new BigDecimal(0)) == 0) { return "零元整"; } str = str.replaceAll(",", "");// 去掉"," String integerStr;// 整数部分数字 String decimalStr;// 小数部分数字 // 初始化:分离整数部分和小数部分 if (str.indexOf(".") > 0) { integerStr = str.substring(0, str.indexOf(".")); decimalStr = str.substring(str.indexOf(".") + 1); } else if (str.indexOf(".") == 0) { integerStr = ""; decimalStr = str.substring(1); } else { integerStr = str; decimalStr = ""; } // integerStr去掉首0,不必去掉decimalStr的尾0(超出部分舍去) if (!integerStr.equals("")) { integerStr = Long.toString(Long.parseLong(integerStr)); if (integerStr.equals("0")) { integerStr = ""; } } // overflow超出处理能力,直接返回 if (integerStr.length() > IUNIT.length) { System.out.println(str + ":超出处理能力"); return str; } int[] integers = toArray(integerStr);// 整数部分数字 boolean isMust5 = isMust5(integerStr);// 设置万单位 int[] decimals = toArray(decimalStr);// 小数部分数字 if (isNegative) { return "负" + getChineseInteger(integers, isMust5) + getChineseDecimal(decimals); } else { return getChineseInteger(integers, isMust5) + getChineseDecimal(decimals); } } /** * 得到大写金额 double */ public synchronized static String toChinese(double doubleStr) { return toChinese(String.valueOf(doubleStr)); } /** 得到大写金额 BigDecimal */ public synchronized static String toChinese(BigDecimal bd) { return toChinese(bd.toString()); } /** * 整数部分和小数部分转换为数组,从高位至低位 */ private static int[] toArray(String number) { int[] array = new int[number.length()]; for (int i = 0; i < number.length(); i++) { array[i] = Integer.parseInt(number.substring(i, i + 1)); } return array; } /** * 得到中文金额的整数部分。 */ private static String getChineseInteger(int[] integers, boolean isMust5) { StringBuffer chineseInteger = new StringBuffer(""); int length = integers.length; for (int i = 0; i < length; i++) { // 0出现在关键位置:1234(万)5678(亿)9012(万)3456(元) // 特殊情况:10(拾元、壹拾元、壹拾万元、拾万元) String key = ""; if (integers[i] == 0) { if ((length - i) == 13)// 万(亿)(必填) key = IUNIT[4]; else if ((length - i) == 9)// 亿(必填) key = IUNIT[8]; else if ((length - i) == 5 && isMust5)// 万(不必填) key = IUNIT[4]; else if ((length - i) == 1)// 元(必填) key = IUNIT[0]; // 0遇非0时补零,不包含最后一位 if ((length - i) > 1 && integers[i + 1] != 0) key += NUMBERS[0]; } chineseInteger.append(integers[i] == 0 ? key : (NUMBERS[integers[i]] + IUNIT[length - i - 1])); } return chineseInteger.toString(); } /** * 得到中文金额的小数部分。TODO */ private static String getChineseDecimal(int[] decimals) { StringBuffer chineseDecimal = new StringBuffer(""); for (int i = 0; i < decimals.length; i++) { // 舍去3位小数之后的 if (i == 2) break; if (i == 0 && decimals[i] == 0) { chineseDecimal.append("零"); } chineseDecimal.append(decimals[i] == 0 ? "" : (NUMBERS[decimals[i]] + DUNIT[i])); } if (chineseDecimal.length() < 2) { return "整"; } else { return chineseDecimal.toString(); } } /** * 判断第5位数字的单位"万"是否应加。 */ private static boolean isMust5(String integerStr) { int length = integerStr.length(); if (length > 4) { String subInteger = ""; if (length > 8) { // 取得从低位数,第5到第8位的字串 subInteger = integerStr.substring(length - 8, length - 4); } else { subInteger = integerStr.substring(0, length - 4); } return Integer.parseInt(subInteger) > 0; } else { return false; } } public static void main(String[] args){ BigDecimal big = new BigDecimal(0.000); System.out.println(ProjectUtil.toChinese(big)); } }