相关工具方法

/**
	 * 获取WEB-INF目录路径
	 * 
	 * @return String - WEB-INF目录路径
	 */
	public static String getWebInfPath()
	{
		String classPath = Utility.class.getResource("/").getPath();
		classPath = classPath.replaceAll("%20", " ");

		return new File(classPath).getParent();
	}
	
	
	  /**
	 * 获取磁盘的剩余空间(以GB为单位, 保留两位小数)
	 * 
	 * @param folderPath - 文件夹路径
	 * @return float - 磁盘剩余空间
	 */
	public static float getDiskFreeSpace(String folderPath)
	{
		File folder = new File(folderPath);
		long space = folder.getFreeSpace();
		float spaceGB = (float) (space / (1024.0 * 1024 * 1024));
		
		return Utility.round(spaceGB, 2, BigDecimal.ROUND_HALF_UP);  // 四舍五入, 保留两位小数
	}
	
	
	/**
	 * 获取现在时间下一个小时的整点时间
	 * 
	 * @return String - 整点时间(例如: 12:00:00)
	 */
	public static String getTime()
	{
		String time = "";
		Calendar calendar = Calendar.getInstance();
		int hour = calendar.get(Calendar.HOUR);
		if (hour == 23)
		{
			hour = 0;
		}
		else
		{
			hour = hour + 1;
		}
		
		// 如果小时数为一位数, 则需要在前面加个"0"
		if (hour < 10)
		{
			time = "0" + hour + ":00:00";
		}
		else
		{
			time = hour + ":00:00";
		}
		
		return time;
	}
	
	
	/**
	 * 对float数据进行取精度
	 * 
	 * @param value - float数据
	 * @param scale - 精度位数(保留的小数位数)
	 * @param roundMode - 精度取值方式(如四舍五入: BigDecimal.ROUND_HALF_UP, 详见BigDecimal)
	 * @return float - float数据进行取精度后的数据
	 */
	public static float roundFloat(float value, int scale, int roundMode)
	{
		float f = new BigDecimal(value).setScale(scale, roundMode).floatValue();
		
		String temp = String.valueOf(f);
		if (temp.length() < 6)
		{
			temp = temp + "00000";
		}
		
		return Float.parseFloat(String.valueOf(temp).substring(0, 6));
	}
	
	
	/**
	 * 对DOUBLE数据进行取精度
	 * 
	 * @param value - float数据
	 * @param scale - 精度位数(保留的小数位数)
	 * @param roundMode - 精度取值方式(如四舍五入: BigDecimal.ROUND_HALF_UP, 详见BigDecimal)
	 * @return float - float数据进行取精度后的数据
	 */
	public static double roundDouble(double value, int scale, int roundMode)
	{
		double d = new BigDecimal(value).setScale(scale, roundMode).doubleValue();
		
		if (String.valueOf(d).length() > 4)
		{
			return Double.parseDouble(String.valueOf(d).substring(0, 6));
		}
		return d;
	}

你可能感兴趣的:(方法)