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

📄 commonutil.java

📁 用Java开发的、实现类似Visio功能的应用程序源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    * 
    *   @return  the day part
    *
    */
   public static int getDay(Date d){
   	Calendar cal = Calendar.getInstance();
   	cal.setTime(d);
   	return cal.get(Calendar.DATE);
   }

   /**
    *   Get the hour part from a date
    * 
    *   @param d  A date value to be extracted
    * 
    *   @return  the hour part
    *
    */
   public static int getHour(Date d){
   	Calendar cal = Calendar.getInstance();
   	cal.setTime(d);
   	return cal.get(Calendar.HOUR);
   }

   /**
    *   Get the minute part from a date
    * 
    *   @param d  A date value to be extracted
    * 
    *   @return  the minute part
    *
    */
   public static int getMinute(Date d){
   	Calendar cal = Calendar.getInstance();
   	cal.setTime(d);
   	return cal.get(Calendar.MINUTE);
   }

   /**
    *   Get the second part from a date
    * 
    *   @param d  A date value to be extracted
    * 
    *   @return  the second part
    *
    */
   public static int getSecond(Date d){
   	Calendar cal = Calendar.getInstance();
   	cal.setTime(d);
   	return cal.get(Calendar.SECOND);
   }

   /**
    *   Get the millisecond part from a date
    * 
    *   @param d  A date value to be extracted
    * 
    *   @return  the millisecond part
    *
    */
   public static int getMillisecond(Date d){
   	Calendar cal = Calendar.getInstance();
   	cal.setTime(d);
   	return cal.get(Calendar.MILLISECOND);
   }


   /**
    *   Convert a date to String
    * 
    *   @param d  A date value to be converted
    * 
    *   @return  a string formatted as 'YYYY-MM-DD', used to represent a date
    *
    */
   public static String  d2s(Date d) {
   	  try {	
     			SimpleDateFormat dateFormatter   =new SimpleDateFormat();
   			dateFormatter.applyPattern("yyyy-MM-dd");
   			return dateFormatter.format(d);   		
   	  } catch(Exception ex) {
   	  	 	return "";
   	  }
   }

   /**
    *   Convert a time to String
    * 
    *   @param d  A time value to be converted
    * 
    *   @return  a string formatted as 'hh:mm:ss', used to represent a time
    *
    */
   public static String  t2s(Date d) {
   	  try {	
     			SimpleDateFormat dateFormatter   =new SimpleDateFormat();
   			dateFormatter.applyPattern("hh:mm:ss");
   			return dateFormatter.format(d);   		
   	  } catch(Exception ex) {
   	  	 	return "";
   	  }
   }

   /**
    *   Convert a datetime to String
    * 
    *   @param d  A datetime value to be converted
    * 
    *   @return  a string formatted as 'YYYY-MM-DD hh:mm:ss', used to represent a datetime
    *
    */
   public static String  dt2s(Date d){
   	  try {	
     			SimpleDateFormat dateFormatter   =new SimpleDateFormat();
   			dateFormatter.applyPattern("yyyy-MM-dd hh:mm:ss");
   			return dateFormatter.format(d);   		
   	  }catch(Exception ex){
   	  	 	return "";
   	  }
   }


   /**
    *   Convert a string to a date value
    * 
    *   @param s  A string formatted as 'YYYY-MM-DD', represents a date value
    * 
    *   @return  a Date value, null if invalid input
    *
    */
   public static Date  s2d(String s){
   		try{
     			SimpleDateFormat dateFormatter   =new SimpleDateFormat();
   			dateFormatter.applyPattern("yyyy-MM-dd");
 			ParsePosition pos = new ParsePosition(0);

   			return dateFormatter.parse(s,pos);   		
   		}catch(Exception ex){
   	  	 	return  null;
   	  	}
   		
   }
   
   /**
    *   Convert a string to a time value
    * 
    *   @param s  A string formatted as 'hh:mm:ss', represents a time value
    * 
    *   @return  a time value, null if invalid input
    *
    */
   public static Date  s2t(String s){
   		try{
     			SimpleDateFormat dateFormatter   =new SimpleDateFormat();
   			dateFormatter.applyPattern("hh:mm:ss");
 			ParsePosition pos = new ParsePosition(0);

   			return dateFormatter.parse(s,pos);   		
   		}catch(Exception ex){
   	  	 	return  null;
   	  	}
   		
   }

   /**
    *   Convert a string to a datetime value
    * 
    *   @param s  A string formatted as 'YYYY-MM-DD hh:mm:ss', represents a datetime value
    * 
    *   @return  a datetime value, null if invalid input
    *
    */
   public static Date  s2dt(String s){
   		try{
     			SimpleDateFormat dateFormatter   =new SimpleDateFormat();
   			dateFormatter.applyPattern("yyyy-MM-dd hh:mm:ss");
 			ParsePosition pos = new ParsePosition(0);

   			return dateFormatter.parse(s,pos);   		
   		}catch(Exception ex){
   	  	 	return  null;
   	  	}
   		
   }
 

   /**
    *   Convert a boolean value to an int one
    * 
    *   @param b  a boolean value to be converted
    * 
    *   @return  a int value represents a boolean one,  1 if true, 0 if false
    *
    */
   public static int b2i(boolean b){
   	if (b)
   		return 1;
   	else
   		return 0;
   }

   /**
    *   Convert an int value to a boolean one
    * 
    *   @param i  an int value to be converted
    * 
    *   @return  a boolean value represents a int one,  true if 1, false if 0
    *
    */
   public static boolean i2b(int i){
   	if (i==0)
   		return false;
   	else
   		return true;
   }
   
   /**
    *   Convert a String to an int value
    * 
    *   @param s  a string to be converted
    * 
    *   @return  an int value converted, 0 if invalid
    *
    */
   public static int s2i(String s)
   {
        try{
		return Integer.parseInt(s);		
  	}catch(Exception e){
               	return 0;
        }
   
   }
    
   /**
    *   Convert an int value to String
    * 
    *   @param i  an int value to be converted
    * 
    *   @return   a string value converted
    *
    */
   public static String  i2s(int i)   {
	return String.valueOf(i);
   }    
   

   /**
    *   Convert an Integer object value to String
    * 
    *   @param i  an Integer object value to be converted
    * 
    *   @return   a string value converted,null if invalid
    *
    */
   public static String  i2s(Integer i)   {
   	if (i==null){
   	   	return null;
   	}
   	else{
		return String.valueOf(i.intValue());
	}
   }    

   /**
    *   Convert a String to a double value
    * 
    *   @param  s  a string to be converted
    * 
    *   @return   a double value converted, 0 if invalid
    *
    */
   public static double s2f(String s)   {
           	try {
			return Double.parseDouble(s);		
  		}catch(Exception e){
               		return 0;
         	}
   
    }
    
   /**
    *   Convert a double value to a string
    * 
    *   @param  d  a double value to be converted
    * 
    *   @return   a string converted
    *
    */
   public static String  f2s(double d)
   {
	return String.valueOf(d);
   }    


   // Object conversion-----------
   

   /**
    *   Convert an object to a string
    * 
    *   @param  o  an object to be converted
    * 
    *   @return   a string converted, null if invalid or unconvertible
    *
    */
   public static String o2s(Object o){
   	try{
   		return (String)o;
	}catch(Exception e){
		return null;
	}
   }

   /**
    *   Format a double value to string
    * 
    *   @param  d   a double value to be formatted
    * 
    *   @param  precision  a precision for length of all digits
    * 
    *   @param  scale   digits count of the fraction partition
    * 
    *   @return   a string formatted
    *
    */
  public static String format(double d, int precision, int scale)
  	throws Exception
  {
    BigDecimal decData = new BigDecimal(d);
    decData = decData.setScale(scale, BigDecimal.ROUND_HALF_EVEN);
    String strData = decData.toString();

    // prepare the final string
    int finalLen = precision + 1;
    String finalStr;
    if (finalLen <= strData.length())    {
      finalStr = strData.substring(0, finalLen);
    }
    else    {
      finalStr = "";
      for (int i = 0; i < finalLen - strData.length(); i++)
      {
        finalStr = finalStr + " ";
      }
      finalStr = finalStr + strData;
    }

    return (finalStr);
  } 
  

   /**
    *   Format a double value to string 
    * 
    *   @param  d   a double value to be formatted
    * 
    *   @return   a string formatted as xxx.xx, use default precistion as 15 and default scale as 2
    *
    */
   public static String  f2fs(double d)   {
   	try{
   	  	return format(d,15,2).trim();
   	}catch(Exception e){
		return f2s(d);
	}
   }    

   /**
    *   Format a double value to string 
    * 
    *   @param  d   a double value to be formatted
    * 
    *   @param  scale   digits count of the fraction partition
    * 
    *   @return   a string formatted as xxx.xx, default precistion=15
    *
    */
   public static String  f2fs(double d, int scale)   {
   	try{
   	  	return format(d,15,scale).trim();
   	}catch(Exception e){
		return f2s(d);
	}
   }    


   //-------Check data validation ------------------------------
   

   /**
    *   Check if an Integer object value has valid or invalid value
    * 
    *   @param  i   an Integer object to be checked
    * 
    *   @return  a boolean result, false if null or zero value, true if not zero
    *
    */
   public static boolean chkValidInt(Integer i){
   	if (i==null || i.intValue()==0){
   		return false;
   	}else{
   		return true;
   	}	
   }
   
   /**
    *   Check if an String value has valid or invalid value
    * 
    *   @param  s  a String to be checked
    * 
    *   @return  a boolean result, false if null or empty string, true if not empty
    *
    */
   public static boolean chkValidStr(String s){
   	if (s==null || s.length()==0){
   		return false;
   	}else{
   		return true;
   	}	
   }
   

   /**
    *   Get a system property by a key.
    *  @param inKey A system property key.
    * 
    */
   public static String getSystemProperty(String inKey){
      java.util.Properties prop = System.getProperties();
      java.util.Enumeration enum = prop.propertyNames();
      for (; enum.hasMoreElements() ;)
      {
         String key = (String)enum.nextElement();
         if (key!=null && key.length()!=0 && key.equals(inKey))
         	return System.getProperty(key);
      }
      
      return "";
   }	
 


        
}

⌨️ 快捷键说明

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