JAVA中各种单位之间的转换

本次讲述的是JAVA中各种单位之间的换算,其中包括货币单位的换算,时间单位的换算,以及小数点的保留和小数点与百分号之间的换算,都是从项目中抽取出来的,可能不太全面,把现有的先记录在这里,后面再继续补充。

(一)首先,这里是货币之间单位的换算:

/**
	 * 货币转换 1000000 return 1百万 10000 return 1万 1000 return 1千
	 * 
	 * @param amount
	 * @return
	 */
	public static HashMap<String, String> amountConversionMap(long amount) {
		HashMap<String, String> map = new HashMap<String, String>();

		if (amount >= 1000000) {
			map.put("amount", amount / 1000000 + "");
			map.put("amoun_unit", "百万");
			return map;
		}

		if (amount >= 10000) {
			map.put("amount", amount / 10000 + "");
			map.put("amoun_unit", "万元");
			return map;
		}

		if (amount >= 1000) {
			map.put("amount", amount / 1000 + "");
			map.put("amoun_unit", "千元");
			return map;
		}

		map.put("amount", amount + "");
		map.put("amoun_unit", "元");
		return map;
	}

	/**
	 * 1000000 return 1百万 10000 return 1万 1000 return 1千
	 * 
	 * @param amount
	 * @return
	 */
	public static String amountConversion(long amount) {
		if (amount >= 1000000) {
			return amount / 1000000 + "百万";
		}

		if (amount >= 10000) {
			return amount / 10000 + "万元";
		}

		if (amount >= 1000) {
			return amount / 1000 + "千元";
		}

		return amount + "元";
	}

	/**
	 * 10000 return 10,000
	 * 
	 * @param amount
	 * @return
	 */
	public static String amountConversionFormat(long amount) {
		return NumberFormat.getInstance().format(amount);
	}

	/**
	 * 10000 return 10,000
	 * 
	 * @param amount
	 * @return
	 */
	public static String amountConversionFormat(double amount) {
		return NumberFormat.getInstance().format(amount);
	}
	
都是一些常见的单位转换,还有国际标准的金额形式。

(二)然后是时间单位上的换算:

/**
	 * 1 Year 1年 1 Month 1个月 1 Day 1天
	 * 
	 * @param period
	 * @param periodUnit
	 * @return
	 */
	public static String periodConversion(int period, String periodUnit) {
		if ("Year".equals(periodUnit)) {
			return period + "年";
		}

		if ("Month".equals(periodUnit)) {
			return period + "个月";
		}

		if ("Day".equals(periodUnit)) {
			return period + "天";
		}

		return period + "";
	}

	/**
	 * 1 Year 1年 1 Month 1个月 1 Day 1天
	 * 
	 * @param period
	 * @param periodUnit
	 * @return
	 */
	public static String periodConversion(String periodUnit) {
		if ("Year".equals(periodUnit)) {
			return "年";
		}

		if ("Month".equals(periodUnit)) {
			return "个月";
		}

		if ("Day".equals(periodUnit)) {
			return "天";
		}

		return "";
	}
	
	
	/**
	 * ep: xx月xx日 xx:xx
	 * 
	 * get time ,only have month,date,hour and minutes
	 * @param time
	 * @return
	 */
	public static String getDateStringMonthDayTime(long time) {
		
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(time);
		StringBuilder builder = new StringBuilder();
		int month = cal.get(Calendar.MONTH)+1;
		builder.append(month);
		builder.append("月");
		builder.append(cal.get(Calendar.DATE));
		builder.append("日");
		builder.append("  ");
		builder.append(cal.get(Calendar.HOUR_OF_DAY));
		builder.append(":");
		builder.append(cal.get(Calendar.MINUTE));
		return builder.toString();
	}

	/**
	 * 123123123 2015-05-18 10:54:27
	 * 
	 * @param time
	 * @return
	 */
	public static String getDateString(long time) {
		SimpleDateFormat myFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		return myFmt.format(new Date(time));
	}

	public static String getDateStringMinute(long time) {
		SimpleDateFormat myFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		return myFmt.format(new Date(time));
	}

	/**
	 * ep: xx月xx日
	 * @param time longtime
	 * @return
	 */
	public static String getDateStringMonthAndDay(long time) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(time);
		StringBuilder sb = new StringBuilder();
		sb.append(cal.get(Calendar.MONTH)+1);
		sb.append("月");
		sb.append(cal.get(Calendar.DAY_OF_MONTH));
		sb.append("日");
		return sb.toString();
	}
	
	/**
	 * 123123123 2015-05-18
	 * 
	 * @param time
	 * @return
	 */
	public static String getDateStringDay(long time) {
		SimpleDateFormat myFmt = new SimpleDateFormat("yyyy-MM-dd");

		return myFmt.format(new Date(time));
	}

	public static boolean isDouble(String value) {
		try {
			Double.parseDouble(value);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}
年月日,时分秒,基本也都齐了。

(三)最后一个是小数点的各种保留方式以及小数与百分数之间的转换

/*	*//**
	 * 9.658 return 9.65
	 */
	/*
	 * public static BigDecimal scale(double scale){ return new
	 * BigDecimal(scale).setScale(2, BigDecimal.ROUND_DOWN); }
	 *//**
	 * 9.658 return 9.65
	 */
	public static BigDecimal scale(BigDecimal scale) {
		return scale.setScale(2, BigDecimal.ROUND_DOWN);
	}

	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	public static int px2dip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}
	
	/**
	 * string型保留两位小数
	 * @param value
	 * @return
	 */
	public static String CovertTwoDecimal(Object value) {
		DecimalFormat df = new DecimalFormat("######0.00");
		return df.format(Double.parseDouble((String) value));
	}
	
	/**
	 * double型保留两位小数
	 */
	public static String dobCoverTwoDecimal (double value) {
		DecimalFormat df = new DecimalFormat("######0.00");
		return df.format(value);
	}
	
	
	/**
	 * 小数转百分数
	 * 
	 */
	public static String changePointToPercent(Double point) {
        return String.valueOf(point*100);
    }
    
    	/**
	 * 0.00001 return 0.01%
	 * 
	 * @param percent
	 * @return
	 */
	public static String percentConversionFormat(double percent){
		NumberFormat percentNF = NumberFormat.getPercentInstance();
		percentNF.setMaximumFractionDigits(2);
		return percentNF.format(percent);
	}


注释都很清晰了,可能还不够完整,后面有新的还会再补充,要是发现有错误的地方欢迎指出,有新的建议欢迎留言!

猜你喜欢

转载自blog.csdn.net/liuwaiter/article/details/54134754