java常用方法

---------------------------------------------保留2位小数------------------------------------------------

//---
new BigDecimal(3.333).setScale(2, RoundingMode.HALF_UP).floatValue();
//--
new DecimalFormat("##0.00").format(3.335);
//--
(float)Math.round(3.335*100)/100;

 

---------------------------------------------日期------------------------------------------------

		Date date = new Date();
		// 格式化日期
		SimpleDateFormat sf = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
		String format = sf.format(date);
		// 日期运算
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		// 加天
		cal.add(Calendar.DATE, 100);
		Date time = cal.getTime();

 

---------------------------------------------随机数------------------------------------------------

	// method1
		Random ran = new Random();
		int nextInt = ran.nextInt(10);
		// method12
		int i = (int) (Math.random() * 10);

 

你可能感兴趣的:(java)