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
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";
}
}
}