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

📄 commonutil.java

📁 用Java开发的、实现类似Visio功能的应用程序源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 *    $Id:CommonUtil.java $
 *
 *    Copyright 2004 ~ 2005 JingFei International Cooperation LTD. All rights reserved.
 *
 */
package com.jfimagine.utils.commonutil;

import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.text.ParsePosition;
import java.math.BigDecimal;


import com.jfimagine.utils.commonutil.ConstUtil;

/**
 * Common utilities for data converting, datetime processing and others.
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.00 $
 */ 
public class CommonUtil{

  /**
   *   Check if a specified file name is a URL file name
   *   @param fileName The file name to be checked.
   */
  public static boolean isURLFile(String fileName){
  	String http="http://";
       	if (fileName==null || fileName.equals("")){
       		return false;
       	}else{
       		fileName	=fileName.toLowerCase();
       		if (fileName.indexOf(http)==0){
       			//here we need to consider an intranet unc path, http://webserver/ is not an URL path.
       			fileName	=fileName.substring(http.length());
       			
       			int slashIndex	=fileName.indexOf("/");
       			if (slashIndex>=0){
       				//get the string section between 'http://' and the first slash '/'
				fileName	=fileName.substring(0,slashIndex);
				
				if (fileName.indexOf(".")>0)
					return true;
				else
					return false; // it's an intranet unc path.
				       				
       			}
       			
       			return true;
       		}
       	        return false;
       	}
  }  


   /**
    *   return a netLine() letter (not all platforms use '\n' for a newline)
    * 
    *   @return  A new Line letter 
    *
    */
   public static char newLine()   {
   	 return '\n';
   }

   /**
    *   Find the number of a char that occurs in the string.
    * 
    *   @param   inputStr An input string to be measured.
    *   @param   ch  A char to be found.
    *   @return  The number of occurrence.
    *
    */
   public static int getCharCount(String inputStr, char ch){
   	if (inputStr==null)
   		return 0;

   	int num=0;
   	for (int i=0; i<inputStr.length(); i++){
   		try{
   			char newCh	=inputStr.charAt(i);
   			if (newCh==ch)
   				num++;
   		}catch(Exception e){
   			break;
   		}
   	}
   	
   	return num;
   }


   /**
    *   Correct problems in Chinese letters
    * 
    *   @param str String to be corrected
    * 
    *   @return  String corrected
    *
    */
   public static String validChnStr(String str)   {
   	  try {
         	if(str == null) str = ""; 
    	 	return new String(str.getBytes("8859_1"));
	  }  catch(Exception ex) {
	  		ex.printStackTrace();
	  		return  str;
	  }
   }

   /**
    *   Continuous add a letter at the begining/end of a String, untill the length of the String equals to len
    * 
    *   @param str String to be expend
    * 
    *   @param headChar Adding chars at begining or end of a string
    * 
    *   @param ch  Character to be added
    * 
    *   @return  A new expended string 
    *
    */
   public static String appendStr(String str,boolean headChar, char ch, int len){
   	
   	if (str==null) str ="";
   	
   	while (str.length()<len) {
   		if (headChar){
   			str   =ch +str;
   	  	}else{
   			str   =str +ch;
   		}
   	}
   	return str;
   }	   


	/** 
	 *   Convert a normal string to a a 2-chars hex string
	 *   @param str Original string.
	 *   @return the hex string.
	 */
	public static String strToHex(String str){
		if (str==null)
			return "";
		else
			return byteToHex(str.getBytes());
        }

	/** 
	 *   Convert a 2-chars hex string to a normal string
	 *   @param hexStr Original hexstring.
	 *   @return the normal string.
	 */
	public static String hexToStr(String hexStr){
		if (hexStr==null)
			return "";
		else
			return new String(hexToByte(hexStr));
        }

    
    	private static final char         hexcodes[] =
    	{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D','E', 'F'};
    	
    	private static final int          shifts[]   =
    	{	28, 24, 20, 16, 12, 8, 4, 0    	};
	
	/** 
	 *   Convert a byte array to a 2-chars hex string
	 *   @param byteAry the byte array
	 *   @return the hex string.
	 */
	public static String byteToHex(byte[] byteAry){
		int digits=2; //2 chars hex.
        	StringBuffer result = new StringBuffer(digits);
        	for (int i=0; i<byteAry.length; i++){
        		byte b	=byteAry[i];
        		for (int j = 0; j < digits; j++){
            			result.append( hexcodes[ (int) ((b >> shifts[ j + (8 - digits) ]) & 15)]);
        		}
        	}
       		return result.toString();
    	}

        
        private static int getByte(char hexChar){
        	for (int i=0; i<hexcodes.length; i++){
        		if (hexChar==hexcodes[i])
        			return i;
        	}                        
        	return 0;
	}
	
	/** 
	 *   Convert a hex string to a byte array.
	 *   @param hexStr The hex string.
	 *   @return the byte array.
	 */
	public static byte[] hexToByte(String hexStr){
		byte[] byteAry	=new byte[hexStr.length()/2];		

		int i=0; 
		int j=0;
		while (j<hexStr.length()){
			//first char
			char	c=hexStr.charAt(j);
			int    value=getByte(c);
			
			//second char
			j++;
			if (j<hexStr.length()){			
				value <<=4;
				c	=hexStr.charAt(j);
				value	+=getByte(c);	
			}
			byteAry[i]=(byte)value;			
			i++;
			j=i*2;
		}
		  
		 return byteAry; 
	}
	
	   
   
   //--------Date/Time Functions-----------------

   /**
    *   Get system's current datetime
    * 
    *   @return  A datetime value
    *
    */
   public static Date now(){
    		Calendar cal = Calendar.getInstance();
		return cal.getTime();		    		
   }

   /**
    *   Get system's current date
    * 
    *   @return  A date value
    *
    */
   public static Date now_date(){
   		return s2d(d2s(now()));
   }
   
   /**
    *   Get system's current time
    * 
    *   @return  A time value
    *
    */
   public static Date now_time(){
   		return s2t(t2s(now()));
   }


   /**
    *   Get the first Day of a year which the date exists
    * 
    *   @param d the date within a year
    * 
    *   @return  A date value represents the first day of a year
    *
    */
   public static Date firstDayOfTheYear(Date d){
	int  year=getYear(d);
	return s2d(year + "-01-01");
   }

   /**
    *   Get the last day of a year which the date exists
    * 
    *   @param d the date within a year
    * 
    *   @return  A date value represents the last day of a year
    *
    */
   public static Date lastDayOfTheYear(Date d){
	int  year=getYear(d);
	return s2d(year + "-12-31");
   }

   /**
    *   Get the first day of this Year
    * 
    *   @return  A date value represents the first day of this year
    *
    */
   public static Date firstDayOfThisYear(){
	return firstDayOfTheYear(now());   		
   }

   /**
    *   Get the last day of this Year
    * 
    *   @return  A date value represents the last day of this year
    *
    */
   public static Date lastDayOfThisYear(){
	return lastDayOfTheYear(now()); 
   }

   /**
    *   Get the first Day of a month which the date exists
    * 
    *   @param d the date within a month
    * 
    *   @return  A date value represents the first day of a month
    *
    */
   public static Date firstDayOfTheMonth(Date d){
	int year=getYear(d);
	int month=getMonth(d);
	return s2d(year +"-" + month +"-01");
   }

   /**
    *   Get the last Day of a month which the date exists
    * 
    *   @param d the date within a month
    * 
    *   @return  A date value represents the last day of a month
    *
    */
   public static Date lastDayOfTheMonth(Date d){
	int year=getYear(d);
	int month=getMonth(d);
	
	//Firstly get the First day of next month:
	month++;
	if (month>12){
		year++;
		month=1;
	}
	
   	Calendar cal=Calendar.getInstance();
   	cal.set(Calendar.YEAR,year);
   	cal.set(Calendar.MONTH,month-1);   //Calendar.JANUARY==0
   	cal.set(Calendar.DATE,1);

	//Then get last day of this month:
   	cal.add(Calendar.DATE,-1);

	return cal.getTime();
   }


   /**
    *   Get the first Day of this month
    * 
    *   @return  A date value represents the first day of this month
    *
    */
   public static Date firstDayOfThisMonth(){
	return firstDayOfTheMonth(now());  		
   }

   /**
    *   Get the last Day of this month
    * 
    *   @return  A date value represents the last day of this month
    *
    */
   public static Date lastDayOfThisMonth(){
	return lastDayOfTheMonth(now()); 
   }


   /**
    *   Encode date from Year,Month and Day
    * 
    *   @param year  the Year part of a date
    * 
    *   @param month  the Month part of a date
    * 
    *   @param day  the Day part of a date
    * 
    *   @return  A date value encoded by the parameters
    *
    */
   public static Date getDate(int year,int month,int day){
   	Calendar cal = Calendar.getInstance();
   	cal.set(year,month,day);
   	return cal.getTime();
   }

   /**
    *   Increase or decrease date by offset
    * 
    *   @param curDate  the date value to be increased or decreased
    * 
    *   @param offset  the number of days, be negative value if decrease.
    * 
    *   @return  A date value adjusted
    *
    */
   public static Date getDate(Date curDate,int offset){
   	Calendar cal=Calendar.getInstance();
   	cal.setTime(curDate);

	//add offset days:
   	cal.add(Calendar.DATE,offset);

	return cal.getTime();
   }

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

   /**
    *   Get the month part from a date
    * 
    *   @param d  A date value to be extracted
    * 
    *   @return  the month part
    *
    */
   public static int getMonth(Date d){
   	Calendar cal = Calendar.getInstance();
   	cal.setTime(d);
   	return cal.get(Calendar.MONTH)+1;  //Calendar.JANUARY==0
   }

   /**
    *   Get the day part from a date
    * 
    *   @param d  A date value to be extracted

⌨️ 快捷键说明

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