当前位置: 代码迷 >> Web前端 >> 分享 做java web项目中常用的工具类中的方法
  详细解决方案

分享 做java web项目中常用的工具类中的方法

热度:170   发布时间:2013-01-25 15:55:29.0
分享 做java web项目中常用的工具类中的方法!

 

分享 做java web项目中常用的工具类中的方法!之后会不断更新的!大家有什么好的共用方法也可送上,共同学习,学习!

 

 一: 把时间Date类型转换成String类型

 

/**
	 * 把Date类型转换成String类型
	 * @param date
	 * @return
	 */
	public static String dateToString(Date date){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(date);
	}


 

  二:把String类型转换成Date类型

 

/**
	 * 把String类型转换成Date类型
	 * @param date
	 * @return
	 */
	public static Date stringToDate(String date){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d = null;
		try {
			d = format.parse(date);
		} catch (java.text.ParseException e) {
			e.printStackTrace();
		}
		return d;
	}

 

  三:同时判断‘’‘’和null 两种情况

 

/**
	 * 判断变量是否为空
	 * @param s
	 * @return
	 */
	public static boolean isEmpty(String s){
		if(null == s || "".equals(s) || "".equals(s.trim()) || "null".equalsIgnoreCase(s)){
			return true;
		}else{
			return false;
		}
	}


 

  四:去除字符串前后的空格

/**
	 * 去除字符串前后的空格
	 * @param s
	 * @return
	 */
	public static String trim(String s){
		if(null == s){
			return "";
		}else{
			return s.trim();
		}
	}


 

  五:用来去掉List中空值和相同项的。

 

/**
	 * 用来去掉List中空值和相同项的。
	 * @param list
	 * @return
	 */
	public List<String> removeSameItem(List<String> list) {
		List<String> difList = new ArrayList<String>();
		for (String t : list) {
			if (t != null && !difList.contains(t)) {
				difList.add(t);
			}
		}
		return difList;
	}

 

   六:当前天数加一天

 

/**
	 * 天数加一
	 * @param dateTime
	 * @return
	 * @throws Exception
	 */
	public static String dateFamte(String dateTime) throws Exception{
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = format.parse(dateTime);
		Calendar calendar = Calendar.getInstance();//日历对象
		calendar.setTime(date);//设置当前日期
		calendar.add(Calendar.DAY_OF_MONTH, 1);//天数加一
		return format.format(calendar.getTime());
	}



 

  相关解决方案