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

📄 mojiutil.java

📁 免疫遗传算法java语言版源程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * IgaLib -> String
 * Copyright (C) 2001  Tosiki IGA , IgaLib project member
 *   (http://homepage2.nifty.com/igat/igapyon/index.html)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package jp.ne.nifty.iga.tosiki.string;

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

/**
 * 擔杮岅暥帤楍憖嶌偵娭偡傞儐乕僥傿儕僥傿僋儔僗<BR>
 * Java尵岅傗JDBC僪儔僀僶偼偦傕偦傕偼墷暷偱奐敪偝傟偰偒偨傕偺側偺偱丄
 * 擔杮岅(摿偵僟僽儖僶僀僩暥帤)偺憖嶌娭學偑嬯庤側応崌偑偁傝傑偡<BR>
 * 偙偺僋儔僗偼 偦偺傛偆側 擔杮岅偵傑偮傢傞暥帤憖嶌傗 擔杮揑暥帤憖嶌
 * 偵傑偮傢傞儊僜僢僪傪廤傔偨僋儔僗偱偡丅
 *
 *   @author Tosiki IGA , Yamauchi Toyohiko , Shinichi Hayashi , Eiichirou Takahashi
 */
public class MojiUtil
{
	/**
	 * 僨僶僢僌昗弨弌椡傪峴偆偐偳偆偐僼儔僌丅
	 */
	protected static final boolean IS_DEBUG=false;

	/**
	 * 梌偊傜傟偨暥帤楍偺嵍懁傪愗傝偮傔傞儊僜僢僪<BR>
	 * 敿妏嬻敀偲慡妏嬻敀傪愗傝偮傔傑偡
	 * 
	 *   @param String strInput 擖椡暥帤楍
	 *   @return String 愗傝偮傔屻暥帤楍
	 */
	public static final String trimLeft(String strInput)
	{
		try{
			StringReader reader=new StringReader(strInput);
			StringWriter writer=new StringWriter();
			StringBuffer strbufWhiteSpace=new StringBuffer();

			for(boolean isLeftSpaceEnded=false;;)
			{
				int iRead=reader.read();
				if(iRead<0)break;
				if(isLeftSpaceEnded==false)
				{
					if(iRead==' '
					|| iRead=='丂')
					{
						continue;
					}
				}

				// 嵍懁僗儁乕僗廔椆
				isLeftSpaceEnded=true;
				writer.write(iRead);
			}

			reader.close();
			writer.flush();
			writer.close();

			return writer.toString();
		}catch(IOException ex){
			System.out.println(ex.toString());
			ex.printStackTrace();
			return null;
		}
	}

	/**
	 * 梌偊傜傟偨暥帤楍傪巜掕挿偝傑偱嵍懁偵巜掕暥帤傪杽傔傞儊僜僢僪
	 * 2僶僀僩1暥帤偺暥帤偼2暥帤偲偟偰僇僂儞僩偟傑偡丅
	 * 
	 *   @param String strInput 擖椡暥帤楍
	 *   @param int iLength 惗惉偟偨偄挿偝
	 *   @param char cFill 杽傔傞暥帤
	 *   @return String 晹暘暥帤楍
	 */
	public static final String padLeft(String strInput,int iLength,char cFill)
	{
		try{
			int iInputLength=strInput.getBytes().length;

			StringWriter writer=new StringWriter();

			boolean isZenkakuHalfAmari=false;
			for(int iCurrentLength=iInputLength;;)
			{
				if(cFill>0xff)
				{
					iCurrentLength+=2;
					if(iCurrentLength>iLength)
					{
						if(iCurrentLength==(iLength+1))
						{
							isZenkakuHalfAmari=true;
						}
						break;
					}
				}
				else
				{
					iCurrentLength+=1;
				}

				if(iCurrentLength<=iLength)
				{
					writer.write(cFill);
				}
				else
				{
					break;
				}
			}

			writer.write(strInput);

			writer.flush();
			writer.close();

			if(isZenkakuHalfAmari==false)
			{
				return writer.toString();
			}
			else
			{
				return " "+writer.toString();
			}
		}catch(IOException ex){
			System.out.println(ex.toString());
			ex.printStackTrace();
			return null;
		}
	}

	/**
	 * 梌偊傜傟偨暥帤楍傪壜擻側尷傝擔晅僨乕僞壔偟傑偡丅
	 * YYMMDD YYYYMMDD丄YY/MM/DD YY.MM.DD 
	 * (擭寧擔+) 帪暘 帪暘昩 帪:暘 帪:暘:昩
	 * 摉弶偼 暯惉丄H側偳偼張棟偱偒傑偣傫丅
	 * 
	 *   @param String strInput 擖椡暥帤楍
	 *   @return Date 拪弌偝傟偨擔晅
	 */
	public static final Date toDate(String strInput)
	{
		strInput=ZenkakuUtil.normalizeZenkaku(strInput);

		if(strInput.length()<=6)
		{
			// 6寘埲壓偺応崌偼 2000擭偑徣棯偝傟偰偄傞傕偺偲壖掕偟傑偡丅
			strInput="20"+strInput;
		}

		try{
			StringReader reader=new StringReader(strInput);
			CharArrayWriter writer=new CharArrayWriter();

			boolean isYearMode=true;

			int iDateFirst=-1;
			int iDateSecond=-1;
			int iDateThird=-1;

			int iHourFirst=-1;
			int iHourSecond=-1;
			int iHourThird=-1;

			for(;;)
			{
				int iRead=reader.read();
				if(iRead<0)break;
				switch(iRead)
				{
					case '0':
					case '1':
					case '2':
					case '3':
					case '4':
					case '5':
					case '6':
					case '7':
					case '8':
					case '9':
						writer.write(iRead);
						break;
					case '/':
					case '.':
					case '擭':
					case '寧':
					case '擔':
						// 擔晅僙僷儗乕僞
						isYearMode=true;
						break;
					case ':':
					case '帪':
					case '暘':
					case '昩':
					case ' ': // 擔晅偲帪崗偺娫偵嬻敀偑擖傞偩傠偆偐傜
						// 帪崗僙僷儗乕僞
						isYearMode=false;
						break;
					case 'H':
					case '暯':
						// 暯惉僗僀僢僠
						// 尰嵼張棟偱偒傑偣傫丅
						break;
					case 'S':
					case '徍':
						// 徍榓僗僀僢僠
						// 尰嵼張棟偱偒傑偣傫丅
						break;
					case 'T':
					case '戝':
						// 戝惓僗僀僢僠
						// 尰嵼張棟偱偒傑偣傫丅
						break;
					case 'M':
					case '柧':
						// 柧帯僗僀僢僠
						// 尰嵼張棟偱偒傑偣傫丅
						break;
				}

				if(writer.size()>=4
				|| (iDateFirst!=-1 && writer.size()>=2))
				{
					if(isYearMode)
					{
						if(iDateFirst==-1)
						{
							iDateFirst=toInteger(writer.toString());
						}
						else if(iDateSecond==-1)
						{
							iDateSecond=toInteger(writer.toString());
						}
						else if(iDateThird==-1)
						{
							iDateThird=toInteger(writer.toString());
						}
					}
					else
					{
						if(iHourFirst==-1)
						{
							iHourFirst=toInteger(writer.toString());
						}
						else if(iHourSecond==-1)
						{
							iHourSecond=toInteger(writer.toString());
						}
						else if(iHourThird==-1)
						{
							iHourThird=toInteger(writer.toString());
						}
					}
					writer.reset();
				}
			}

			if(isYearMode)
			{
				if(iDateFirst==-1)
				{
					iDateFirst=toInteger(writer.toString());
				}
				else if(iDateSecond==-1)
				{
					iDateSecond=toInteger(writer.toString());
				}
				else if(iDateThird==-1)
				{
					iDateThird=toInteger(writer.toString());
				}
			}
			else
			{
				if(iHourFirst==-1)
				{
					iHourFirst=toInteger(writer.toString());
				}
				else if(iHourSecond==-1)
				{
					iHourSecond=toInteger(writer.toString());
				}
				else if(iHourThird==-1)
				{
					iHourThird=toInteger(writer.toString());
				}
			}

			// 偲傝偁偊偢 杮擔偺00:00:00傪僙僢僩丅
			Calendar calendarResult=Calendar.getInstance();
			calendarResult=trimTime(calendarResult);

			if(iDateThird!=-1)
			{
				if(IS_DEBUG)System.out.println(""+iDateFirst+"擭"+iDateSecond+"寧"+iDateThird+"擔");
				calendarResult.set(Calendar.YEAR,iDateFirst);
				calendarResult.set(Calendar.MONTH,iDateSecond-1);
				calendarResult.set(Calendar.DATE,iDateThird);
			}
			else if(iDateSecond!=-1)
			{
				if(IS_DEBUG)System.out.println(""+iDateFirst+"寧"+iDateSecond+"擔");
				calendarResult.set(Calendar.MONTH,iDateFirst-1);
				calendarResult.set(Calendar.DATE,iDateSecond);
			}

			if(iHourThird!=-1)
			{
				// 帪暘昩
				if(IS_DEBUG)System.out.println(""+iHourFirst+"帪"+iHourSecond+"暘"+iHourThird+"昩");
				calendarResult.set(Calendar.HOUR_OF_DAY,iHourFirst);
				calendarResult.set(Calendar.MINUTE,iHourSecond);
				calendarResult.set(Calendar.SECOND,iHourThird);
			}
			else if(iHourSecond!=-1)
			{
				// 帪暘
				if(IS_DEBUG)System.out.println(""+iHourFirst+"帪"+iHourSecond+"暘"+0+"昩");
				calendarResult.set(Calendar.HOUR_OF_DAY,iHourFirst);
				calendarResult.set(Calendar.MINUTE,iHourSecond);
				calendarResult.set(Calendar.SECOND,0);
			}

			writer.close();

			return calendarResult.getTime();
		}catch(IOException ex){
			System.out.println(ex.toString());
			ex.printStackTrace();
			return null;
		}
	}

	/**
	 * 梌偊傜傟偨Calendar偺帪崗晹暘傪愗傝偮傔傑偡丅(00:00:00偵僙僢僩偟傑偡)
	 * 

⌨️ 快捷键说明

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