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

📄 dwdate.java

📁 用来为垂直搜索引擎抓取数据的采集系统
💻 JAVA
字号:
/*
 * *****************************************************
 * Copyright (c) 2005 IIM Lab. All  Rights Reserved.
 * Created by xuehao at Nov 30, 2005
 * Contact: zxuehao@mail.ustc.edu.cn
 * *****************************************************
 */
package org.indigo.db;

import java.util.ArrayList;
import java.util.List;
/**
 * 主要完成对日期的操作。
 * @author wbz
 *
 */
public class DwDate
{
    private static DwDate itsDate=new DwDate();
    private DwDate()
    {
    }
    public static DwDate getInstance()
    {
        return itsDate;
    }
    /**
     * 对日期格式规范化。
     * @param strDate
     * @return
     */
    public java.sql.Date formatDate( String strDate )
    {
//        System.out.println( strDate );
        
        java.sql.Date thedate=null;
        java.util.Date uDate = new java.util.Date();

        if( strDate==null || strDate.equals("") )
        {
            return new java.sql.Date( uDate.getTime() );
        }
        List list = new ArrayList();
        StringBuffer buf = new StringBuffer();
        char []ch = strDate.toCharArray();
        int i=0; 
        /**
         * 对输入的参数循环读入到list中。
         * 
         */
        for( i=0; i<ch.length; i++ )
        {
            if( Character.isDigit(ch[i]) )
                buf.append( ch[i] );
            else
            {
                if( buf.length()!=0 )
                {
	                list.add( buf.toString() );
	                buf = new StringBuffer();
                }
            }
        }
        if( i==ch.length )
        {
            list.add( buf.toString() );
            buf = null;
        }
        String str;
        int year=-1,month=-1,day=-1;
        /**
         * 当遇到年月日都有情况时的处理代码。如:2007-12-21
         */
        if( list.size()>=3 )
        {
            str = list.get(0).toString();
            if(!str.equals(""))
            year = Integer.parseInt( str );
            year -= 1900;
            if( year<0 || year>8099 )
            {
                year = uDate.getYear();
            }
            str = list.get(1).toString();
            if(!str.equals(""))
            month = Integer.parseInt( str );
            month -= 1;
            if( month<0 || month>11 )
            {
                month = uDate.getMonth();
            }
            str = list.get(2).toString();
            
            try
			{
            	if(!str.equals(""))
            	day = Integer.parseInt( str );
			}catch( java.lang.NumberFormatException e )
			{
				day = uDate.getDate();
			}
        }else
        	/**
        	 * 当遇到只有月日时的处理代码,如12-20;
        	 */
        if( list.size()==2 )
        {
            year = uDate.getYear();
            str = list.get(0).toString();
            try
			{
            	if(!str.equals(""))
	            month = Integer.parseInt( str );
	            month -= 1;
	            if( month<0 || month>11 )
	            {
	                month = uDate.getMonth();
	            }
			}catch( java.lang.NumberFormatException e )
			{
				month = uDate.getMonth();
			}
            str = list.get(1).toString();
            try
			{
            	day = Integer.parseInt( str );
			}catch( java.lang.NumberFormatException e )
			{
				day = uDate.getDate();
			}
        }else
        	/**
        	 * 当日期只有日时的处理代码。
        	 */
        if( list.size()==1 )
        {
            year = uDate.getYear();
            month = uDate.getMonth();
            str = list.get(0).toString();
            if(!str.equals(""))
            day = Integer.parseInt( str );
            if( day<1 || day>31 )
            {
                day = uDate.getDate();
            }
        }else//当日期不正确时,取当前日期返回。
        {
            return new java.sql.Date( uDate.getTime() );
        }
        thedate = new java.sql.Date( year, month, day );
        
        return thedate;
    }
public static void main(String args[])
{
	String temp=itsDate.formatDate("2007-08-09").toString();
	System.out.println(temp);
}
}

⌨️ 快捷键说明

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