📄 extdateparser.java
字号:
// try { String userPattern = dateUserPattern; if( hasTimePart && timeUserPattern != null ) { userPattern += DATE_TIME_SEPARATOR + timeUserPattern; } SimpleDateFormat sdf = new SimpleDateFormat( userPattern, locale ); return DateHelper.toSystem( sdf.parse( s ).getTime(), tz ); } catch( ParseException e ) { parseException = e; } // // try other possible patterns // if( hasTimePart ) { // try to parse date WITH time for( int i = 0; i < datePatterns.length; i++ ) { for( int j = 0; j < timePatterns.length; j++ ) { try { return tryToParse( s, datePatterns[i] + DATE_TIME_SEPARATOR + timePatterns[j], locale, tz ); } catch( ParseException e ) {} } } } else { // try to parse date WITHOUT time for( int i = 0; i < datePatterns.length; i++ ) { try { return tryToParse( s, datePatterns[i], locale, tz ); } catch( ParseException e ) {} } } // throw exception throw parseException; } /** * Helper Method to try to parse a date * * @param s - String to parse * @param pattern - Pattern * @param locale - User Locale * @param tz - User TimeZone * * @return long representation of date * @throws ParseException */ private static long tryToParse( String s, String pattern, Locale locale, TimeZone tz ) throws ParseException { // maybe user locale? try { SimpleDateFormat sdf = new SimpleDateFormat( pattern, locale ); return DateHelper.toSystem( sdf.parse( s ).getTime(), tz ); } catch( ParseException ex1 ) { // maybe system locale? try { SimpleDateFormat sdf = new SimpleDateFormat( pattern, SystemHelper.SYSTEM_LOCALE ); return DateHelper.toSystem( sdf.parse( s ).getTime(), tz ); } catch( ParseException ex2 ) { // throw top exception throw ex1; } } } /** * Changes date according to operation and an argumet stinrg s * Example: "January 1 2003 0:00:00", "+", "1h" will return January 1 2003 1:00:00 * * @param date - Date to work on * @param operation - math operation, currently can be a PLUS_OPERATION or a MINUS_OPERATION * @param s - an argument * * @return long reprsentation of date * @throws ParseException */ private static long dateMath( Date date, char operation, String s ) throws ParseException { int op; if( operation == MINUS_OPERATION ) { op = -1; } else { op = 1; } s = s.trim(); DateMathHelper dmh = new DateMathHelper(); if( s.indexOf( DATE_TIME_SEPARATOR ) == -1 && s.indexOf( TIME_SEPARATOR ) == -1 ) { dmh.chekForLetterIdentifers( s ); dmh.proceedRemainder(); } else { StringTokenizer st = new StringTokenizer( s, TIME_SEPARATOR, false ); List params = new ArrayList(); while( st.hasMoreTokens() ) { params.add( st.nextToken() ); } String firstParam = ( String ) params.get( 0 ); int pos1 = firstParam.indexOf( DATE_TIME_SEPARATOR ); if( pos1 > 0 ) { String firstPart = firstParam.substring( 0, pos1 ); dmh.setDays( parseDouble( firstPart ) ); dmh.proceedRemainder(); // if it happen to be something like .1 or 1. String secondPart = firstParam.substring( pos1 + 1 ); int pos2 = secondPart.indexOf( DECIMAL_SEPARATOR ); if( pos2 == 0 ) { dmh.setMinutes( parseDouble( secondPart.substring( 1 ) ) ); } else if( pos2 == secondPart.length() ) { dmh.setHours( parseDouble( secondPart.substring( 0, pos2 - 1 ) ) ); } else { // if not, then it's probably hours.. dmh.setHours( parseDouble( secondPart ) ); } } else { dmh.setHours( parseDouble( firstParam ) ); } if( params.size() == 2 ) { dmh.setMinutes( parseDouble( ( String ) params.get( 1 ) ) ); } else if( params.size() == 3 ) { dmh.setMinutes( parseDouble( ( String ) params.get( 1 ) ) ); dmh.setSeconds( parseDouble( ( String ) params.get( 2 ) ) ); } } return dmh.getDate( date, op ); } // Try to parse string as double // And throw ParseException if failed private static double parseDouble( String s ) throws ParseException { try { return Double.parseDouble( s ); } catch( NumberFormatException ex ) { throw new ParseException( ex.getMessage(), 0 ); } } //------------------------------------------------------------ data processors /** * Helper inner class for dateMath method */ private static class DateMathHelper { private double years = 0; private double months = 0; private double days = 0; private double hours = 0; private double minutes = 0; private double seconds = 0; //check if there's something familiar public void chekForLetterIdentifers( String s ) throws ParseException { int pos; if( ( pos = s.indexOf( YEAR_SYMBOL ) ) > 0 ) { String numberOfYears = s.substring( 0, pos ); years = parseDouble( numberOfYears ); } else if( ( pos = s.indexOf( MONTH_SYMBOL ) ) > 0 ) { String numberOfMonths = s.substring( 0, pos ); months = parseDouble( numberOfMonths ); } else if( ( pos = s.indexOf( DAY_SYMBOL ) ) > 0 ) { String numberOfDays = s.substring( 0, pos ); days = parseDouble( numberOfDays ); } else if( ( pos = s.indexOf( HOUR_SYMBOL ) ) > 0 ) { String numberOfHours = s.substring( 0, pos ); hours = parseDouble( numberOfHours ); } else if( ( pos = s.indexOf( MINUTE_SYMBOL ) ) > 0 ) { String numberOfMinutes = s.substring( 0, pos ); minutes = parseDouble( numberOfMinutes ); } else if( ( pos = s.indexOf( SECOND_SYMBOL ) ) > 0 ) { String numberOfSeconds = s.substring( 0, pos ); seconds = parseDouble( numberOfSeconds ); } else { hours = parseDouble( s ); } } //if it wasn't an integer, then move reminder down through months, days, hours... public void proceedRemainder() { double i; if( years < 1 && years > 0 ) { months = 12 * years; } else if( ( i = years % ( int ) years ) > 0 ) { months = 12 * i; } if( months < 1 && months > 0 ) { days = 30 * months; } else if( ( i = months % ( int ) months ) > 0 ) { days = 30 * i; } if( days < 1 && days > 0 ) { hours = 24 * days; } else if( ( i = days % ( int ) days ) > 0 ) { hours = 24 * i; } if( hours < 1 && hours > 0 ) { minutes = 60 * hours; } else if( ( i = hours % ( int ) hours ) > 0 ) { minutes = 60 * i; } if( minutes < 1 && minutes > 0 ) { seconds = 60 * minutes; } else if( ( i = minutes % ( int ) minutes ) > 0 ) { seconds = 60 * i; } } public long getDate( Date date, int op ) { Calendar cal = Calendar.getInstance(); cal.setTime( date ); if( years != 0 ) { cal.add( Calendar.YEAR, ( int ) years * op ); } if( months != 0 ) { cal.add( Calendar.MONTH, ( int ) months * op ); } if( days != 0 ) { cal.add( Calendar.DATE, ( int ) days * op ); } if( hours != 0 ) { cal.add( Calendar.HOUR, ( int ) hours * op ); } if( minutes != 0 ) { cal.add( Calendar.MINUTE, ( int ) minutes * op ); } if( seconds != 0 ) { cal.add( Calendar.SECOND, ( int ) seconds * op ); } return cal.getTime().getTime(); } public double getYears() { return years; } public void setYears( double years ) { this.years = years; } public double getMonths() { return months; } public void setMonths( double months ) { this.months = months; } public double getDays() { return days; } public void setDays( double days ) { this.days = days; } public double getHours() { return hours; } public void setHours( double hours ) { this.hours = hours; } public double getMinutes() { return minutes; } public void setMinutes( double minutes ) { this.minutes = minutes; } public double getSeconds() { return seconds; } public void setSeconds( double seconds ) { this.seconds = seconds; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -