All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.app.common.utils.Utils Maven / Gradle / Ivy

The newest version!
package com.app.common.utils;

import org.apache.http.util.TextUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {

	public static String calcLeftTenor(String date, boolean withD) {
		try {
			if (!TextUtils.isBlank(date)) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
				Date dtCur = sdf.parse(sdf.format(new Date()));
				Date dtDest = sdf.parse(date);
				long dateTime = dtDest.getTime();
				long todayTime = dtCur.getTime();
				long days = (dateTime - todayTime) / (1000 * 60 * 60 * 24);
				if (days < 0) {
					return "0Y";
				}
				if (withD && days < 365) {
					return days + "D";
				}
				int restYear = dtDest.getYear() - dtCur.getYear();
				int restYear2 = dtDest.getYear() - dtCur.getYear();
				String curMMdd = sdf.format(dtCur).substring(4, 4);
				String matruMMdd = date.substring(4, 4);
				Calendar cal = Calendar.getInstance();
				cal.clear();
				cal.setTime(dtCur);
				if (matruMMdd.compareTo(curMMdd) >= 0) {
					cal.add(Calendar.YEAR, restYear);
				} else {
					cal.add(Calendar.YEAR, restYear - 1);
					restYear2 = restYear2 - 1;
				}
				days = (dateTime - cal.getTime().getTime()) / (1000 * 60 * 60 * 24);

				BigDecimal bd = new BigDecimal(days).divide(new BigDecimal(isLeapYear(dtDest.getYear()) ? 366 : 365), 2,
						RoundingMode.HALF_UP);
				bd = bd.add(new BigDecimal(restYear2));

				return bd.toPlainString() + "Y";
			}
		} catch (Exception e) {

		}
		return "";

	}

	public static boolean isLeapYear(int year) {
		boolean b1 = year % 4 == 0;
		boolean b2 = year % 100 != 0;
		boolean b3 = year % 400 == 0;
		return b1 && b2 || b3;
	}

	public static String IpToHex(String ip) {
		String str = "";
		String[] strs = ip.split("\\.");
		if (strs.length == 4) {

			String temp = intStrToHex(strs[2]);
			str += temp;
			temp = intStrToHex(strs[3]);
			str += temp;

		}
		return str;
	}

	public static String ListStrToString(List lt) {
		String str = "";
		for (String s : lt) {
			str = str + s + ",";
		}
		if (str.length() > 0) {
			str = str.substring(0, str.length() - 1);
		}
		return str;
	}

	public static List StringToListStr(String str) {
		List lt = new ArrayList();
		if (str != null) {
			String[] strs = str.split(",");
			for (String s : strs) {
				if(!TextUtils.isBlank(str)) {
				lt.add(s);
				}
			}
		}

		return lt;
	}

	public static String intStrToHex(String str) {
		String temp = Integer.toHexString(Integer.valueOf(str));
		if (temp.length() < 2) {
			temp = "0" + temp;
		}
		return temp;
	}

	public static String getEmpty(String str) {
		return TextUtils.isBlank(str) ? "" : str;
	}

	public static String getEmpty(String str, int length) {
		String tmp = TextUtils.isBlank(str) ? "" : str;
		if (tmp.length() > length) {
			tmp = tmp.substring(0, length);
		}
		return tmp;
	}

	public static BigDecimal getBigDecimal(BigDecimal b) {
		return b == null ? BigDecimal.ZERO : b;
	}

	public static BigDecimal getBigDecimal(String b) {
		return b == null ? BigDecimal.ZERO : new BigDecimal(b);
	}

	public static String getFormat(Object str, int length) {
		return String.format("%-" + length + "s", str);
	}

	private static String macAddressStr = null;
	private static String computerName = System.getenv().get("COMPUTERNAME");

	private static final String[] windowsCommand = { "ipconfig", "/all" };
	private static final String[] linuxCommand = { "/sbin/ifconfig", "-a" };
	private static final Pattern macPattern = Pattern.compile(".*((:?[0-9a-f]{2}[-:]){5}[0-9a-f]{2}).*",
			Pattern.CASE_INSENSITIVE);

	private final static List getMacAddressList() throws IOException {
		final ArrayList macAddressList = new ArrayList();
		final String os = System.getProperty("os.name");
		final String command[];

		if (os.startsWith("Windows")) {
			command = windowsCommand;
		} else if (os.startsWith("Linux")) {
			command = linuxCommand;
		} else {
			return macAddressList;
		}
		// 执行命令
		final Process process = Runtime.getRuntime().exec(command);

		BufferedReader bufReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
		for (String line = null; (line = bufReader.readLine()) != null;) {
			Matcher matcher = macPattern.matcher(line);
			if (matcher.matches()) {
				macAddressList.add(matcher.group(1));

			}
		}

		process.destroy();
		bufReader.close();
		return macAddressList;
	}

	public static String getMacAddress() {
		if (macAddressStr == null || macAddressStr.equals("")) {
			StringBuffer sb = new StringBuffer(); // 存放多个网卡地址用,目前只取一个非0000000000E0隧道的值
			try {
				List macList = getMacAddressList();
				for (Iterator iter = macList.iterator(); iter.hasNext();) {
					String amac = iter.next();
					if (!amac.equals("0000000000E0")) {
						sb.append(amac);
						break;
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

			macAddressStr = sb.toString();

		}

		return macAddressStr;
	}

	public static String getLocalMac() throws Exception {

		InetAddress ia = InetAddress.getLocalHost();
		byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();

		StringBuffer sb = new StringBuffer("");
		for (int i = 0; i < mac.length; i++) {
			if (i != 0) {
				sb.append("-");
			}
			int temp = mac[i] & 0xff;
			String str = Integer.toHexString(temp);
			if (str.length() == 1) {
				sb.append("0" + str);
			} else {
				sb.append(str);
			}
		}
		return sb.toString();

	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy