当前位置 : 主页 > 编程语言 > java >

将一个基本数据类型转换为另一个基本数据类型。

来源:互联网 收集:自由互联 发布时间:2021-06-28
gistfile1.txt import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.List;import com.google.common.base.Preconditions;import com.meallink.core.exception.api.ArgumentsIncorrectException;impor
gistfile1.txt
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.google.common.base.Preconditions;
import com.meallink.core.exception.api.ArgumentsIncorrectException;
import com.meallink.core.exception.api.InvalidOprationException;

/**
 * 将一个基本数据类型转换为另一个基本数据类型。
 * 
 */
public class ConvertUtil {
	/**
	 * 将指定的字符串转换为等效的指定类型的对象。
	 * 
	 * @param value
	 *            字符串。
	 * @return 等效于 value 的指定类型的对象。
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static Object changeType(String value, Class
  type) {
		if ((null != value) && (value.getClass() == type)) {
			return value;
		}
		if ((type == boolean.class) || (type == Boolean.class)) {
			if ("1".equals(value)) {
				return true;
			}
			return Boolean.parseBoolean(value);
		}
		if ((type == char.class) || (type == Character.class)) {
			return value.charAt(0);
		}
		if ((type == byte.class) || (type == Byte.class)) {
			return Byte.parseByte(value);
		}
		if ((type == short.class) || (type == Short.class)) {
			return Short.parseShort(value);
		}
		if ((type == int.class) || (type == Integer.class)) {
			return Integer.parseInt(value);
		}
		if ((type == long.class) || (type == Long.class)) {
			return Long.parseLong(value);
		}
		if ((type == float.class) || (type == Float.class)) {
			return Float.parseFloat(value);
		}
		if ((type == double.class) || (type == Double.class)) {
			return Double.parseDouble(value);
		}
		if (type == String.class) {
			return value;
		}
		if (type == Class.class) {
			try {
				return Class.forName(value);
			} catch (ClassNotFoundException e) {
				throw new InvalidOprationException(e.getMessage());
			}
		} else if (type.isEnum()) {
			return Enum.valueOf((Class
 
  ) type, (String) value);
		} else if (Date.class.isAssignableFrom(type)) {
			try {
				return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(value);
			} catch (ParseException e) {
				 try {
					return new SimpleDateFormat("yyyy-MM-dd").parse(value);
				} catch (ParseException e1) {
					throw new InvalidOprationException(e.getMessage());
				}
			}
		}
		throw new InvalidOprationException(type.getName());
	}

	/**
	 * 将指定的 32 位整数转换为字节数组表示形式。
	 * 
	 * @param value
	 *            32 位整数。
	 * @return 长度为 4 的字节数组。
	 */
	public static byte[] toBytes(int value) {
		byte[] bytes = new byte[4];
		bytes[0] = (byte) (value & 0xff);
		bytes[1] = (byte) ((value >>> 8) & 0xff);
		bytes[2] = (byte) ((value >>> 16) & 0xff);
		bytes[3] = (byte) ((value >>> 24) & 0xff);
		return bytes;
	}

	/**
	 * 返回由字节数组中指定位置的 4 个字节转换来的 32 位整数。
	 * 
	 * @param bytes
	 *            字节数组。
	 * @param index
	 *            bytes 内的起始索引。
	 * @return 由 4 个字节构成、从 index 开始的 32 位整数。
	 */
	public static int toInteger(byte[] bytes, int index) {
		Preconditions.checkArgument(null != bytes);
		if ((index < 0) || (index >= bytes.length)) {
			throw new ArgumentsIncorrectException("index");
		}
		if (index + 4 > bytes.length) {
			throw new ArgumentsIncorrectException("bytes");
		}
		return ((bytes[index] & 0xff) | ((bytes[index + 1] & 0xff) << 8)
				| ((bytes[index + 2] & 0xff) << 16) | ((bytes[index + 3] & 0xff) << 24));
	}

	public static String toString(List
  
    list, char c) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < list.size(); i++) { if (builder.length() > 0) { builder.append(','); } builder.append(list.get(i)); } return builder.toString(); } public static Long[] toLongArray(String s) { String[] stringArray = s.split(","); Long[] longArray = new Long[stringArray.length]; for (int i = 0; i < stringArray.length; i++) { longArray[i] = Long.parseLong(stringArray[i]); } return longArray; } }
  
 
网友评论