java 金額字串轉換 分轉元 元轉分
阿新 • • 發佈:2019-02-08
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import java.security.MessageDigest; import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; public class Utils { public static String DateToTimestamp(Date time){ Timestamp ts = new Timestamp(time.getTime()); return ((ts.getTime())/1000)+""; } //MD5加密 public static String MD5(String sign) { byte[] bytes = null; try { MessageDigest md5 = MessageDigest.getInstance("MD5"); bytes = md5.digest(sign.toString().getBytes("UTF-8")); } catch (Exception e) { e.printStackTrace(); return null; } // 將MD5輸出的二進位制結果轉換為小寫的十六進位制 StringBuilder sign_s = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { String hex = Integer.toHexString(bytes[i] & 0xFF); if (hex.length() == 1) { sign_s.append("0"); } sign_s.append(hex); } return sign_s.toString(); } public static String getTime(){ SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmssSSS"); return df.format(new Date()); } public String turnTime(String abc){ Long timestamp = Long.parseLong(abc); String date = new java.text.SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date(timestamp)); return date; } // GET 請求 // public String userInter(String url){ // HttpURLConnection connection; // URL realUrl; // String result=""; // try { // realUrl = new URL(url); // connection = (HttpURLConnection) realUrl.openConnection(); // connection.connect(); // BufferedReader reader = new BufferedReader( // new InputStreamReader(connection.getInputStream(),"UTF-8")); // String line=""; // while ((line = reader.readLine()) != null) { // result+=line; // } // connection.disconnect(); // } catch (Exception e) { // e.printStackTrace(); // } // return result; // } //POST 請求 public static String sendPost(String url, String param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); // 開啟和URL之間的連線 URLConnection conn = realUrl.openConnection(); // 設定通用的請求屬性 // conn.setRequestProperty("accept", "*/*"); // conn.setRequestProperty("connection", "Keep-Alive"); // conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 傳送POST請求必須設定如下兩行 conn.setDoOutput(true); conn.setDoInput(true); // 獲取URLConnection物件對應的輸出流 out = new PrintWriter(conn.getOutputStream()); // 傳送請求引數 out.print(param); // flush輸出流的緩衝 out.flush(); // 定義BufferedReader輸入流來讀取URL的響應 in = new BufferedReader( new InputStreamReader(conn.getInputStream(),"UTF-8")); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("傳送 POST請求出現異常!"+e); e.printStackTrace(); } //使用finally塊來關閉輸出流、輸入流 finally{ try{ if(out!=null){ out.close(); } if(in!=null){ in.close(); } } catch(IOException ex){ ex.printStackTrace(); } } return result; } public String decoParam(String url_param,String name,String key){ String param=""; try { String abc=Encrypt.decodeDes(key, url_param); String a[]=abc.split("&"); for (int i = 0; i < a.length; i++) { String b[]=a[i].split("="); if(b[0].equals(name)){ param=b[1]; } } return param; } catch (Exception e) { System.out.println("解密失敗"); } return null; } /** * * 功能描述:去除字串首部為"0"字元 * @param str 傳入需要轉換的字串 * @return 轉換後的字串 */ public static String removeZero(String str){ char ch; String result = ""; if(str != null && str.trim().length()>0 && !str.trim().equalsIgnoreCase("null")){ try{ for(int i=0;i<str.length();i++){ ch = str.charAt(i); if(ch != '0'){ result = str.substring(i); break; } } }catch(Exception e){ result = ""; } }else{ result = ""; } return result; } /** * * 功能描述:金額字串轉換:單位分轉成單元 * @param str 傳入需要轉換的金額字串 * @return 轉換後的金額字串 */ public static String fenToYuan(Object o) { if(o == null) return "0.00"; String s = o.toString(); int len = -1; StringBuilder sb = new StringBuilder(); if (s != null && s.trim().length()>0 && !s.equalsIgnoreCase("null")){ s = removeZero(s); if (s != null && s.trim().length()>0 && !s.equalsIgnoreCase("null")){ len = s.length(); int tmp = s.indexOf("-"); if(tmp>=0){ if(len==2){ sb.append("-0.0").append(s.substring(1)); }else if(len==3){ sb.append("-0.").append(s.substring(1)); }else{ sb.append(s.substring(0, len-2)).append(".").append(s.substring(len-2)); } }else{ if(len==1){ sb.append("0.0").append(s); }else if(len==2){ sb.append("0.").append(s); }else{ sb.append(s.substring(0, len-2)).append(".").append(s.substring(len-2)); } } }else{ sb.append("0.00"); } }else{ sb.append("0.00"); } return sb.toString(); } /** * * 功能描述:金額字串轉換:單位元轉成單分 * @param str 傳入需要轉換的金額字串 * @return 轉換後的金額字串 */ public static String yuanToFen(Object o) { if(o == null) return "0"; String s = o.toString(); int posIndex = -1; String str = ""; StringBuilder sb = new StringBuilder(); if (s != null && s.trim().length()>0 && !s.equalsIgnoreCase("null")){ posIndex = s.indexOf("."); if(posIndex>0){ int len = s.length(); if(len == posIndex+1){ str = s.substring(0,posIndex); if(str == "0"){ str = ""; } sb.append(str).append("00"); }else if(len == posIndex+2){ str = s.substring(0,posIndex); if(str == "0"){ str = ""; } sb.append(str).append(s.substring(posIndex+1,posIndex+2)).append("0"); }else if(len == posIndex+3){ str = s.substring(0,posIndex); if(str == "0"){ str = ""; } sb.append(str).append(s.substring(posIndex+1,posIndex+3)); }else{ str = s.substring(0,posIndex); if(str == "0"){ str = ""; } sb.append(str).append(s.substring(posIndex+1,posIndex+3)); } }else{ sb.append(s).append("00"); } }else{ sb.append("0"); } str = removeZero(sb.toString()); if(str != null && str.trim().length()>0 && !str.trim().equalsIgnoreCase("null")){ return str; }else{ return "0"; } } }