⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commutil.java

📁 EasyJF信息发布全部源代码!
💻 JAVA
字号:
package com.easyjf.util;

import java.beans.PropertyDescriptor;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.BeanUtilsBean;

import com.easyjf.web.WebForm;
import com.easyjf.web.tools.IPageList;
/**
*
* <p>Title:通过工具类,处理一些数据转换及常用功能</p>
* <p>Description: 包括一些类型转换,数据格式化,生成一些常用的html文本等</p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: www.easyjf.com</p>
* @author 蔡世友
* @version 1.0
*/
public  class CommUtil {
	private static final java.text.SimpleDateFormat dateFormat=new java.text.SimpleDateFormat("yyyy-MM-dd");	
	private static final java.text.SimpleDateFormat timeFormat=new java.text.SimpleDateFormat("hh:mm:ss");
	private static final java.text.SimpleDateFormat longDateFormat=new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	private static final CommUtil util=new CommUtil();
	public static CommUtil getInstance()
	{
		return util;
	}
	
	public static java.text.SimpleDateFormat getDateFormat() {
		return dateFormat;
	}	

	public static String formatDate(String format,Object v)
	{
		if(v==null)return null;
		SimpleDateFormat df=new SimpleDateFormat(format);		
		return df.format(v);
	}
	public static String format(Object v)
	{
		if(v==null)return null;
		return dateFormat.format(v);
	}
	public static String sortTime(Object v)
	{
		if(v==null)return null;
		return timeFormat.format(v);
	}
	public static String longDate(Object v)
	{
		if(v==null)return null;
		return longDateFormat.format(v);
	}
	public static java.util.Date formatDate(String s)
	{
		java.util.Date d=null;
		try{
		d=dateFormat.parse(s);
		}
		catch(Exception e)
		{			
		}
		return d;
	}
	public static String null2String(Object s)
	{
		return s==null?"":s.toString();
	}
	public static String null2String(String s)
	{
		return s==null?"":s.toString();
	}
	public static int null2Int(Object s)
	{
		int v=0;
		if(s!=null)
		{
			try{
				v=Integer.parseInt(s.toString());
				}
			catch(Exception e)
			{				
			}			
			}
		return v;
	}
	 public static String round(double inNumber,int param){
	        String format="#.";
	        for(int i=0;i<param;i++){
	            format=format.concat("#");
	        }
	        //去掉多余小数点
	        if(param==0){
	            format=format.substring(0,format.toString().length()-1);
	        }
	        java.text.DecimalFormat df =new java.text.DecimalFormat(format);
	        return  df.format(inNumber);
	    }

	 public static String getRandString(int length)
	 {
		 StringBuffer s=new StringBuffer(""+(new java.util.Date().getTime()));
		 Random r=new Random(10);
		 s.append(Math.abs(r.nextInt()));
		 if(s.toString().length()>length)s.substring(0,length);
		 return s.toString();
	 }
	public static String  getOnlyID(){
		  String strRnd;
		    double dblTmp;
		    SimpleDateFormat df=new SimpleDateFormat("yyyyMMDDhhmmss");    
		     //---- 4random
		    dblTmp = java.lang.Math.random() * 100000;
		    while (dblTmp < 10000) {
		      dblTmp = java.lang.Math.random() * 100000;
		    }
		    strRnd = String.valueOf(dblTmp).substring(0,4);
		    String s = df.format(new Date())+strRnd;
		    return s;
	  }
	/**
	 * 显示页码
	 * @param currentPage
	 * @param pages
	 * @return 分布htmp字符串
	 */
	public static String showPageHtml(int currentPage,int pages)
	{
		String s="";	
		if(currentPage>1)
		{
		s+="<a href=# onclick='return gotoPage(1)'>首页</a> ";
		s+="<a href=# onclick='return gotoPage("+(currentPage-1)+")'>上一页</a> ";				
		}
		int beginPage=currentPage-3<1?1:currentPage-3;
		if(beginPage<pages){
			s+="第 ";
		for(int i=beginPage,j=0;i<=pages&&j<6;i++,j++)
		{
			if(i==currentPage)
				s+="<font color=red>"+i+"</font> ";
			else
			s+="<a href=# onclick='return gotoPage("+i+")'>"+i+"</a> ";
		}		
		s+="页 ";
		}
		if(currentPage<pages)
		{			
			s+="<a href=# onclick='return gotoPage("+(currentPage+1)+")'>下一页</a> ";
			s+="<a href=# onclick='return gotoPage("+pages+")'>末页</a> ";
		}
		//s+=" 转到<input type=text size=2>页";
		return s;
	}
	public static String getSelectOptions(String[][] items,String value)
	{
		String s="";
		for(int i=0;i<items.length;i++)
		{
			s+="<option value='"+items[i][0]+"' "+(items[i][0].equals(value)?"selected":"")+">"+items[i][1]+"</option>";			
		}
		return s;
	}
	public static String getSelectOptions(int min,int max,int value)
	{
		String s="";
		for(int i=min;i<=max;i++)
		{
			s+="<option value='"+i+"' "+(i==value?"selected":"")+">"+i+"</option>";			
		}
		return s;
	}
	public static String toChinese(String strvalue) 
	  { 
	        try{ 
	            if(strvalue==null) 
	               return null; 
	            else 
	            { 
	               strvalue = new String(strvalue.getBytes("ISO8859_1"), "GBK"); 
	               return strvalue; 
	        } 
	        }catch(Exception e){ 
	              return null; 
	        } 
	  } 
	
	public static String toChinese(String strvalue,String charset) 
	  { 
	        try{ 
	            if(charset==null || charset.equals("")) 
	               return toChinese(strvalue); 
	            else 
	            { 
	               strvalue = new String(strvalue.getBytes("ISO8859_1"),charset); 
	               return strvalue; 
	        } 
	        }catch(Exception e){ 
	              return null; 
	        } 
	  } 
	
	public static Iterator CopyIterator(Class classType,Iterator src)
	{	
		return CopyList(classType,src).iterator();
	}
	public static List CopyList(Class classType,Iterator src)
	{
		List tag=new ArrayList();
		while(src.hasNext())
		{					
			Object obj=null,ormObj=src.next();			
			try {
				obj=classType.newInstance();
				BeanUtils.copyProperties(obj,ormObj);
			} catch (Exception e) {
			}
			if (obj != null)
				tag.add(obj);
		}
		return tag;
	}
	public static void Map2Obj(Map map,Object obj)
	{
		 Iterator names =map.keySet().iterator();
         while (names.hasNext()) {
             String name = (String) names.next();
             if (
            	 BeanUtilsBean.getInstance().getPropertyUtils().isWriteable(obj, name)) {
                 Object value = map.get(name);
                 try{             	
                 BeanUtilsBean.getInstance().copyProperty(obj, name, value);
                 }
                 catch(Exception e)
             	{   
                	 e.printStackTrace();
                	 System.out.println(name+":"+e);
             	}
             	}
         		}		
	}
	public static void Obj2Map(Object obj,Map map)
	{
		if(map==null)map=new java.util.HashMap();			 
		PropertyDescriptor descriptors[] =
		BeanUtilsBean.getInstance().getPropertyUtils().getPropertyDescriptors(obj);
		//System.out.println("Obj2Map:"+descriptors.length);
		for (int i = 0; i < descriptors.length; i++) {
		String name = descriptors[i].getName();
		try{
		if (descriptors[i].getReadMethod() != null)
		{		
		 map.put(name, BeanUtilsBean.getInstance().getPropertyUtils().getProperty(obj,name));		
		}
		}
		catch(Exception e)
		{
			System.out.println(name+":"+e);            	
		}
		}
	}
	public static void saveIPageList2WebForm(IPageList pList,WebForm form)
	{
		if(pList!=null){
			form.addResult("list",pList.getResult());
			form.addResult("pages",new Integer(pList.getPages()));
			form.addResult("rows",new Integer(pList.getRowCount()));
			form.addResult("page",new Integer(pList.getCurrentPage()));		
			form.addResult("gotoPageHTML",CommUtil.showPageHtml(pList.getCurrentPage(),pList.getPages()));
			}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -