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

📄 data.java

📁 java swing源码 欢迎下载 有问题请联系 我一定负责到底
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * Created on 2004-2-15convertDateToString
 * last modified by lucas on 2004-2-19
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package com.sinosoft.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.*;
import java.io.*;
 



/**
 * @author lisui
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class Data {
	
		  /**
		   * 2003-2-25 add SQLOutValue(Date,valueType) function to apt to rs.getDate("fieldName")
		   * modified by lucas 2004-2-19
		   * change value for sql
		   * @param orignalValue value;
		   * @param the type of value ,such as int,string,datetime,type name is no case sensitive,default type is string;
		   * @param allowNull false means null is not permit,default value is true;
		   */
		  public static  String formatValue(String orignalValue,String valueType,boolean allowNull) throws RuntimeException{
			  String tempValue=null;
			  if(orignalValue==null || orignalValue.equals("")){
				  if(allowNull){
					  tempValue="NULL";
				  }else throw new RuntimeException("星号字段不允许为空");
			  }else
					if(isStringType(valueType)){
						if(valueType.equalsIgnoreCase("char")){
							if(orignalValue.length()>1) throw new RuntimeException(orignalValue+"不是char型数据");
						}
						//对字符串进行过滤操作!	
						orignalValue=orignalValue.trim();
						tempValue="'"+orignalValue.replaceAll("'","''")+"'";
					}else
						if(isNumberType(valueType)){
							if(valueType.equalsIgnoreCase("int")){
								try{
									tempValue=Integer.parseInt(orignalValue)+"";
								}catch(Exception ex){
									throw new RuntimeException(orignalValue+"不是整形数字");	
								}
							}else 
								if(valueType.equalsIgnoreCase("money")){
									if(!isMoneyType((orignalValue))){
										throw new RuntimeException(orignalValue+"不是有效的金额");
									}
									tempValue=orignalValue;
								}else
									tempValue=orignalValue;		
						}else 
							if(isDateType(valueType)){
								if(valueType.equalsIgnoreCase("datetime")){
									tempValue="'"+convertDateToString(convertStringToDate(orignalValue),"datetime")+"'";
								}else
									tempValue="'" +convertDateToString(convertStringToDate(orignalValue),"smalldatetime")+"'";	
							}
				return tempValue;
		  }
		  /**
		   * 判断一个字符串是否为“char”、“string”、“text”、“varchar”型数据,不考虑大小写
		   * @param valueType  输入字符串
		   */
		  private static boolean isStringType(String valueType){
				if(valueType.equalsIgnoreCase("char") 
				|| (valueType.equalsIgnoreCase("string")) 
				|| valueType.equalsIgnoreCase("text")
				|| valueType.equalsIgnoreCase("varchar"))
					return true;
				else
					return false;
		  }
		  /**
		   * 判断一个字符串是否为“int”、“float”、“money”、“smallint”型数据,不考虑大小写
		   * @param valueType  输入字符串
		   */
		  private  static boolean isNumberType(String valueType){
				if(valueType.equalsIgnoreCase("int")
				||valueType.equalsIgnoreCase("float")
				||valueType.equalsIgnoreCase("money")
				||valueType.equalsIgnoreCase("smallint")){
					return true;
				}else
					return false;
		  }
		  /**
		   * 判断一个字符串是否为“datetime”、“smalldatetime”、“date”型数据,不考虑大小写
		   * @param valueType  输入字符串
		   */
		  private  static boolean isDateType(String valueType){
				if(valueType.equalsIgnoreCase("datetime")
					||valueType.equalsIgnoreCase("smalldatetime")
					||valueType.equalsIgnoreCase("date"))
					return true;
				else
					return false;
		  }
		  /**
		   * 判断一个字符串是否为“time”型数据,不考虑大小写
		   * @param valueType  输入字符串
		   */
		  private static boolean isTimeType(String valueType){
		  		if(valueType.equalsIgnoreCase("time"))
		  			return true;
		  		else
		  			return false;
		  }
		  /**
		   * 判断一个字符串是否为Money型数据,不考虑大小写
		   * @param orignalValue  输入字符串
		   */
		  private static boolean isMoneyType(String orignalValue){
		  		String firstAllowChars="-+0123456789$";
		  		String allowChars="0123456789,.";
		  		
		  		char a=orignalValue.charAt(0);
		  		if(firstAllowChars.indexOf(a)<0)
		  			return false;
		  		
		  		for(int i=1;i<orignalValue.length();++i){
		  			a=orignalValue.charAt(i);
		  			if(allowChars.indexOf(a)<0){
						return false;
		  			}
		  		}
		  		return true;
		  }
		  /**
		   * 删除money型数据的小数点的最后两位,保留两位小数
		   * @param orignalValue  输入字符串
		   */
		  private static String cutMoney(String orignalValue){
				double value = Double.parseDouble(orignalValue);
				java.text.DecimalFormat df=new java.text.DecimalFormat("##0.00");
				return formatNum(df.format(value));

		  }
		  /**
		   * 删除money型数据的小数点的最后两位,保留三位小数
		   * @param orignalValue  输入字符串
		   */
		  private static String cutTon(String orignalValue){
				double value = Double.parseDouble(orignalValue);
				java.text.DecimalFormat df=new java.text.DecimalFormat("##0.000");
				return formatNum(df.format(value));

		  }	
		  /**
		   * 把字符串换成sql server2000数据库能够识别的数据格式 String
		   * @param orignalValue  输入字符串
		   * @param 允许为空
		   */
		  public static  String formatValue(String orignalValue) throws RuntimeException{
				  return formatValue(orignalValue,"String",true);
		  }
		  /**
		   * 把字符串换成sql server2000数据库能够识别的数据格式 String
		   * @param orignalValue  输入字符串
		   * @param allowNull  是否允许为空
		   */
		  public static  String formatValue(String orignalValue,boolean allowNull) throws RuntimeException{
			  return formatValue(orignalValue,"String",allowNull);
		  }
		  /**
		   * 把字符串换成sql server2000数据库能够识别的数据格式
		   * @param orignalValue  输入字符串
		   * @param valueType  输入格式
		   * @param 允许为空
		   */
		  public static  String formatValue(String orignalValue,String valueType) throws RuntimeException{
			  return formatValue(orignalValue,valueType,true); 
		  }
		  /**
		   * 把int型换成sql server2000数据库能够识别的数据格式 int
		   * @param orignalValue  输入数字
		   * @param 允许为空
		   */
		  public static  String formatValue(int orignalValue) throws RuntimeException{
			return formatValue(orignalValue+"","int",true);
		  }
		/**
		 * 将布尔型数据转换成“1”、“0”
		 * @param orignalValue  输入布尔型数据
		 */  
		public static String formatValue(boolean orignalValue) throws RuntimeException{
			  if(orignalValue){
				  return "1";
			  }else
				  return "0";
		}
		/**
		   * 把int型换成sql server2000数据库能够识别的数据格式 
		   * @param orignalValue  输入数字
		   * @param valueType  输入格式
		   * @param 允许为空
		   */
		  public static  String formatValue(int orignalValue,String valueType) throws RuntimeException{
			return formatValue(orignalValue+"",valueType,true);
		  }
		  /**
		   * 把int型换成sql server2000数据库能够识别的数据格式 
		   * @param orignalValue  输入数字
		   * @param valueType  输入格式
		   * @param allowNull  是否允许为空
		   */
		  public static  String formatValue(int orignalValue,String valueType,boolean allowNull) throws RuntimeException{
			return formatValue(orignalValue+"",valueType,allowNull);
		  }
		  /**
		   * 把Date型换成sql server2000数据库能够识别的数据格式
		   * @param orignalDate  输入日期
		   * @param valueType  输入类型
		   */
	  	  public static String formatValue(Date orignalDate,String dateType) throws RuntimeException{
	  	  	if(orignalDate==null){
	  	  		return "NULL";
	  	  	}else
	  	  		return "'"+convertDateToString(orignalDate,dateType)+"'";
	  	  }
	  	  /**
		   * 把Date型换成sql server2000数据库能够识别的数据格式(精确到分钟)
		   * @param orignalDate  输入日期
		  */
		  public static String formatValue(Date orignalDate)throws RuntimeException{
		  	return formatValue(orignalDate,"smalldatetime");
		  }
		 /**
		   * 把日期转换成sql server2000数据库能够识别的数据格式 yyyy-mm-dd
		   * @param orignalDate
		   * @param dateType
		   * @return
		   * @throws Exception
		   */
		  public static  String convertDateToString(Date orignalDate,String dateType) throws RuntimeException{
				String convertFormat="";
				try{
					if(dateType.equalsIgnoreCase("datetime")){
					convertFormat = new String("yyyy-MM-dd HH:mm:ss");
				}else if(dateType.equalsIgnoreCase("smalldatetime") || dateType.equalsIgnoreCase("date")){
					convertFormat = new String("yyyy-MM-dd");
				}
	  		
				SimpleDateFormat dateFormatter=new SimpleDateFormat(convertFormat);
				return dateFormatter.format(orignalDate);
				}catch(Exception ex){
					throw new RuntimeException(ex.getMessage());
				}
		  }
		  
		  /**
		   * 把日期型转换成用户看到的日期格式 xx年xx月xx日
		   * @param orignalDate
		   * @param dateType
		   * @return
		   * @throws Exception
		   */
		  public static String convertDateToCNString(Date orignalDate,String dateType)throws RuntimeException{
				String convertFormat="";
				try{
					if(dateType.equalsIgnoreCase("datetime")){
						//convertFormat = new String("yyyy年MM月dd日 HH:mm:ss");
						convertFormat = new String("yyyy-MM-dd HH:mm:ss");
					}else if(dateType.equalsIgnoreCase("smalldatetime") || dateType.equalsIgnoreCase("date")){
						//convertFormat = new String("yyyy年MM月dd日");
						convertFormat = new String("yyyy-MM-dd");  //modify by wq
					}
	  				
	  				SimpleDateFormat dateFormatter=new SimpleDateFormat(convertFormat);
					return dateFormatter.format(orignalDate);
				}catch(Exception ex){
					throw new RuntimeException(ex.getMessage());
				}
		  }
		 	
		 	/**
		 	 * 把用户输入的日期数值或者从数据库提取的数值转换成日期类型。
		 	 * @param orignalValue
		 	 * @return
		 	 * @throws Exception
		 	 */
			public static  Date convertStringToDate(String orignalValue) throws RuntimeException{
				String tempValue=orignalValue;
				try{
//					tempValue=tempValue.replaceFirst("年","-");
//					tempValue=tempValue.replaceFirst("月","-");
//					tempValue=tempValue.replaceFirst("日","");
					tempValue=tempValue.replaceFirst("/","-");
					tempValue=tempValue.replaceFirst("/","-");
					
					if(tempValue.trim().length()<=10){
						tempValue=tempValue.trim() +" 0:00:00";
					}
					SimpleDateFormat dateFormatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					return dateFormatter.parse(tempValue);
				}catch(Exception ex){
					throw new RuntimeException(orignalValue +"不是日期型数据");
				}
			}
			/**
		 	 * 把日期转换成sql server2000数据库能够识别的数据格式 yyyy-mm-dd
		 	 * @param orignalValue
		 	 * @return
		 	 */ 
		 public static  String converDateToString(Date orignalDate) throws RuntimeException{
			return convertDateToString(orignalDate,"datetime");
		 }
		 /**
		 	 * 将日期(格式:某年某月某日)替换成YYYY-MM-DD
		 	 * @param tmpdate
		 	 */ 
		public  String transferdate(String tmpdate){
				String date=tmpdate;
				date=date.replaceFirst("年","-");
				date=date.replaceFirst("月","-");
				date=date.replaceFirst("日","");
				date=date.trim();
		return date;
			 }
		/**
	 	 * 获得当前时间(格式:YYYY-MM-DD)
	 	 */
		public static  String getdate(){
			Calendar cal = Calendar.getInstance(); 
			int year = cal.get(Calendar.YEAR); 
			int month = cal.get(Calendar.MONTH)+1; 
			int day = cal.get(Calendar.DATE); 

			String currDay=year +"-"+ month +"-"+ day;
		
			return currDay;
				 }
			public static  String getRoom(String zkwz){
				zkwz=zkwz.trim();
				String room="";
				int num1=zkwz.indexOf("房");
				if (num1>=0){
				room=zkwz.substring(0,num1);
				}
				return room;
			}	
			public static  String getShelf(String zkwz){
					zkwz=zkwz.trim();
					String room="";
					String shelf="";
				
					int num1=zkwz.indexOf("房");
					int num2=zkwz.indexOf("架");
					if (num1>=0){
					room=zkwz.substring(0,num1);
					}
					if (num2>=0){
					shelf=zkwz.substring(num1+1,num2);
					}
					return shelf;
				}			 
		/**
		 * 根据输入的类型对输入值进行转换
		 * @param orignalValue  输入值
		 * @param valueType  输入类型
		*/		 
		public static String SQLOutValue(String orignalValue,String valueType) throws RuntimeException{
			String tempValue="";
			if(orignalValue==null
			 || orignalValue.equals("")
			 || orignalValue.equalsIgnoreCase("null")){
				return "";
			}
			
			if(isStringType(valueType)){
				if(orignalValue.equalsIgnoreCase("null")){
					tempValue="";		
				}else
					tempValue=orignalValue;
			}else
				if(isDateType(valueType)){
					Date tempDate=convertStringToDate(orignalValue);
					tempValue=convertDateToCNString(tempDate,valueType);
					
//					if(valueType.equals("smalldatetime") || valueType.equals("date")){
//						tempValue=orignalValue.substring(0,10);
//					}else if(valueType.equalsIgnoreCase("datetime")){
//						int i_dot=orignalValue.indexOf(".");
//						if(i_dot>0){
//							tempValue=orignalValue.substring(0,i_dot);	
//						}
//						
//						
//						
//					}else
//						tempValue=orignalValue;
				}else{
					if(valueType.equalsIgnoreCase("ton")){
						 tempValue=cutTon(orignalValue);
					}else if(valueType.equalsIgnoreCase("money")){
						 tempValue=cutMoney(orignalValue);
					}else 
						tempValue=orignalValue;
				}
			return tempValue;
		}
	/**
	* 根据string类型对输入值进行转换
	* @param orignalValue  输入值
	*/		
	 public static String SQLOutValue(String orignalValue) throws RuntimeException{
	 	return SQLOutValue(orignalValue,"string");
	 }
	/**
	* 获得当前时间(格式:某年某月某日)

⌨️ 快捷键说明

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