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

📄 extdateparser.java

📁 CRM源码This file describes some issues that should be implemented in future and how it should be imple
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright 2006-2007 Queplix Corp. * * Licensed under the Queplix Public License, Version 1.1.1 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.queplix.com/solutions/commercial-open-source/queplix-public-license/ * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */package com.queplix.core.modules.eqlext.utils;import com.queplix.core.utils.DateHelper;import com.queplix.core.utils.StringHelper;import com.queplix.core.utils.SystemHelper;import com.queplix.core.utils.log.AbstractLogger;import com.queplix.core.utils.log.Log;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Date;import java.util.List;import java.util.Locale;import java.util.StringTokenizer;import java.util.TimeZone;/** * Description: Date Parser and processor * @author [SVM] Maxim Suponya * @author [ALB] Andrey Baranov * @version $Revision: 1.1.1.1 $ $Date: 2005/09/12 15:30:41 $ */public final class ExtDateParser {    public static final int MIN_YEAR = 1900;    public static final int MAX_YEAR = 9999;    public static final String SECOND_SYMBOL = "s";    public static final String MINUTE_SYMBOL = "m";    public static final String HOUR_SYMBOL = "h";    public static final String DAY_SYMBOL = "d";    public static final String MONTH_SYMBOL = "M";    public static final String YEAR_SYMBOL = "y";    public static final char PLUS_OPERATION = '+';    public static final char MINUS_OPERATION = '-';    public static final String TODAY_FUNCTION = "today";    public static final String NOW_FUNCTION = "now";    public static final String DECIMAL_SEPARATOR = ".";    public static final String TIME_SEPARATOR = ":";    public static final String DATE_TIME_SEPARATOR = " ";    private static final String[] datePatterns_dayPos0 = {        "d/M/yy",        "dd/MM/yy",        "d/M/yyyy",        "dd/MM/yyyy",        "d MMMM yy",        "dd MMMM yy",        "d MMMM yyyy",        "dd MMMM yyyy",        "ddMMMMyy",        "ddMMMMyyyy",        "ddMMMyy",        "ddMMMyyyy",        "dd/MMM/yy",        "dd/MMM/yyyy",        "ddMMyy",        "ddMMyyyy"};    private static final String[] datePatterns_dayPos1 = {        "M/d/yy",        "MM/dd/yy",        "M/d/yyyy",        "MM/dd/yyyy",        "MMMM d yy",        "MMMM dd yy",        "MMMM d yyyy",        "MMMM dd yyyy",        "MMMMddyy",        "MMMMddyyyy",        "MMMddyy",        "MMMddyyyy",        "MMddyy",        "MMddyyyy"};    private static final String[] datePatterns = new String[datePatterns_dayPos0.length + datePatterns_dayPos1.length];    static {        System.arraycopy( datePatterns_dayPos0, 0, datePatterns, 0, datePatterns_dayPos0.length );        System.arraycopy( datePatterns_dayPos1, 0, datePatterns, datePatterns_dayPos0.length, datePatterns_dayPos1.length );    }    private static final String[] timePatterns = {        "h:mm a",        "h:mm:ss a",        "HH:mm:ss",        "HH:mm"};    private static final AbstractLogger logger = Log.getLog( ExtDateParser.class );    // --------------------------------------------------------------- public static methods    /**     * Parses Date from a string. Returns system date millis.     * Examples of strings to parse:     *      "3/18/2003 +1y" = March 18 2004 00:00:00     *      "today + 1m" = 1 month after today date     *      "now - 5h" = 5 hours befor current time     *      "1jan2003 00:00:00 + 1 10:00" = 1 day and 10 hours after 1 january 2003 00:00:00     *      "today + 0.5y" = half of the year after today     *      "now - 1 .1" = 1 day and 1 minute before current time     *      "January 1 1970 +1y -0.25y +15d -4h -3m -5s +1 1 +1 .1 -5:30 +3:45:01"     *     * @param s                 - String to parse     * @param isDayPosFirst     - Position of Day in a Date string, true - date before month, false - month before date     * @param locale            - user Locale     * @param tz                - User TimeZone     * @param dateUserPattern   - Date user pattern (example: "MM/dd/yy")     * @param timeUserPattern   - Time user pattern (example: "hh:mm a")     *     * @return system long representation of Date     * @throws ParseException     */    public static long parseDate( String s,                                  boolean isDayPosFirst,                                  Locale locale,                                  TimeZone tz,                                  String dateUserPattern,                                  String timeUserPattern )        throws ParseException {        // checking...        if( locale == null ) {            throw new IllegalArgumentException( "Locale is null" );        }        if( tz == null ) {            throw new IllegalArgumentException( "Timezone is null" );        }        if( StringHelper.isEmpty( dateUserPattern ) ) {            throw new IllegalArgumentException( "Date user pattern is null" );        }        // is null?        if( StringHelper.isEmpty( s ) ) {            throw new ParseException( "String date is null", 0 );        }        int minusPos = s.indexOf( PLUS_OPERATION );        int plusPos = s.indexOf( MINUS_OPERATION );        int firstOpPos = -1;        if( minusPos > 0 ) {            firstOpPos = ( plusPos < 0 ) ? minusPos : Math.min( minusPos, plusPos );        } else if( plusPos > 0 ) {            firstOpPos = ( minusPos < 0 ) ? plusPos : Math.min( minusPos, plusPos );        }        Date date;        // Detect date and operation positions        String dateString;        String opString;        if( firstOpPos > 0 ) {            dateString = s.substring( 0, firstOpPos );            opString = s.substring( firstOpPos, s.length() );        } else {            dateString = s;            opString = null;        }        //        // First. Parse date        //        try {            date = new Date( parseSingleDate( dateString, isDayPosFirst, locale, tz, dateUserPattern, timeUserPattern ) );        } catch( ParseException ex ) {            logger.ERROR( ex );            // set first position            throw new ParseException( ex.getMessage(), 0 );        }        if( !StringHelper.isEmpty( opString ) ) {            logger.DEBUG( "operation: " + opString );            //            // Second. Parse operations            //            int length = opString.length();            int i = 0;            while( i < length ) {                char c = opString.charAt( i++ );                if( c != PLUS_OPERATION && c != MINUS_OPERATION ) {                    continue;                }                StringBuffer sb = new StringBuffer();                while( i < length ) {                    char nextc = opString.charAt( i++ );                    boolean findOp = false;                    if( i == length ) {                        findOp = true;                        sb.append( nextc );                    } else if( nextc == PLUS_OPERATION || nextc == MINUS_OPERATION ) {                        findOp = true;                        i--;                    }                    if( findOp ) {                        // perform math operation                        try {                            logger.DEBUG( "perform math operation=" + c + " param=" + sb );                            long ms = dateMath( date, c, sb.toString() );                            date = new Date( ms );                        } catch( ParseException ex ) {                            logger.ERROR( ex );                            // set operation position                            throw new ParseException( ex.getMessage(), firstOpPos + i );                        }                        break;                    }                    sb.append( nextc );                }            }        }        // check MIN and MAX years        Calendar cal = Calendar.getInstance();        cal.setTime( date );        int year = cal.get( Calendar.YEAR );        if( year < MIN_YEAR || year > MAX_YEAR ) {            throw new ParseException( "Incorrect year", 0 );        }        return date.getTime();    }    /**     * Detect has date a time part     * @param s given date     * @return true if has     */    public static boolean hasTimePart( String s ) {        if( s == null ) {            return false;        } else if( s.toLowerCase().indexOf( NOW_FUNCTION ) >= 0 ) {            return true;        } else {            return( s.indexOf( TIME_SEPARATOR ) >= 0 );        }    }    // --------------------------------------------------------------- private static methods    /**     * Helper method to parse a date from a string     *     * @param s                 - String to parse     * @param isDayPosFirst     - Position of Day in a Date string, true - date before month, false - month before date     * @param locale            - user Locale     * @param tz                - User TimeZone     * @param dateUserPattern   - Date user pattern (example: "MM/dd/yy")     * @param timeUserPattern   - Time user pattern (example: "hh:mm a")     *     * @return milleseconds     * @throws ParseException     */    private static long parseSingleDate( String s,                                         boolean isDayPosFirst,                                         Locale locale,                                         TimeZone tz,                                         String dateUserPattern,                                         String timeUserPattern )        throws ParseException {        // Initialization        ParseException parseException = null;        String[] datePatterns = null;        if( isDayPosFirst ) {            datePatterns = datePatterns_dayPos0;        } else {            datePatterns = datePatterns_dayPos1;        }        s = s.trim();        boolean hasTimePart = ( s.indexOf( ':' ) >= 0 );        // now?        if( s.equalsIgnoreCase( NOW_FUNCTION ) ) {            return DateHelper.currentTimeMillis();        }        // today?        if( s.equalsIgnoreCase( TODAY_FUNCTION ) ) {            Calendar cal = Calendar.getInstance();            cal.setTime( new Date( DateHelper.toUser( DateHelper.currentTimeMillis(), tz ) ) );            cal.set( Calendar.HOUR_OF_DAY, 0 );            cal.set( Calendar.MINUTE, 0 );            cal.set( Calendar.SECOND, 0 );            return DateHelper.toSystem( cal.getTime().getTime(), tz );        }        //        // try user pattern

⌨️ 快捷键说明

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