Java Code Examples for org.apache.commons.lang.time.DateUtils#parseDate()

The following examples show how to use org.apache.commons.lang.time.DateUtils#parseDate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ModeUtils.java    From DataLink with Apache License 2.0 6 votes vote down vote up
public static boolean isYearlyMatch(String pattern, String value) {
    if (!StringUtils.endsWith(pattern, YEAR_SUFFIX)) {
        throw new ValidationException("the pattern must end with " + YEAR_SUFFIX);
    }

    String valueSuffix = StringUtils.substring(value, value.length() - 4);
    String valuePrefix = StringUtils.substring(value, 0, value.length() - 4);

    if (StringUtils.substringBeforeLast(pattern, YEAR_SUFFIX).equals(valuePrefix)) {
        try {
            DateUtils.parseDate(valueSuffix, new String[]{"yyyy"});
            return true;
        } catch (ParseException e) {
        }
    }
    return false;
}
 
Example 2
Source File: RdsBinlogOpenApiTest.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimple() throws Throwable {
    Date startTime = DateUtils.parseDate("2018-08-10 12:00:00", new String[] { "yyyy-MM-dd HH:mm:ss" });
    Date endTime = DateUtils.parseDate("2018-08-11 12:00:00", new String[] { "yyyy-MM-dd HH:mm:ss" });
    String url = "https://rds.aliyuncs.com/";
    String ak = "";
    String sk = "";
    String dbInstanceId = "";

    RdsBackupPolicy backupPolicy = RdsBinlogOpenApi.queryBinlogBackupPolicy(url, ak, sk, dbInstanceId);
    System.out.println(backupPolicy);

    List<BinlogFile> binlogFiles = RdsBinlogOpenApi.listBinlogFiles(url, ak, sk, dbInstanceId, startTime, endTime);
    System.out.println(binlogFiles);
}
 
Example 3
Source File: ModeUtils.java    From DataLink with Apache License 2.0 5 votes vote down vote up
public static String tryBuildYearlyPattern(String value) {
    String valueSuffix = StringUtils.substring(value, value.length() - 4);
    try {
        DateUtils.parseDate(valueSuffix, new String[]{"yyyy"});
        return StringUtils.substring(value, 0, value.length() - 4) + YEAR_SUFFIX;
    } catch (ParseException e) {
    }
    return value;
}
 
Example 4
Source File: DateTypeConverter.java    From bdf3 with Apache License 2.0 5 votes vote down vote up
@Override
public Object fromText(Class<?> type, String text) {
	if (StringUtils.isBlank(text)) {
		return null;
	}
	try {
		return DateUtils.parseDate(text, getDateFarmats());
	} catch (ParseException e) {
		try {
			return DateUtil.getJavaDate(Double.valueOf(text));
		} catch (Exception e2) {
			throw new RuntimeException("[" + text + "]无法转换为日期类型。");
		}
	}
}
 
Example 5
Source File: SsioIntegrationTest.java    From sep4j with Apache License 2.0 5 votes vote down vote up
public void setDate(String s) {
	if (s == null) {
		return;
	}
	try {
		Date d = DateUtils.parseDate(s, new String[] { "yyyy-MM-dd HH:mm:ss" });
		this.setDate(d);
	} catch (ParseException e) {
		throw new IllegalArgumentException(e);
	}

}
 
Example 6
Source File: SepShowcases.java    From sep4j with Apache License 2.0 5 votes vote down vote up
/**
 * if the cell is of String type
 * 
 * @param birthDayString
 * @throws ParseException
 */
public void setBirthDay(String birthDayString) throws ParseException {
	if (birthDayString == null) {
		return;
	}
	birthDay = DateUtils.parseDate(birthDayString,
			new String[] { "yyyy-MM-dd" });
}
 
Example 7
Source File: HeaderUtilsTestRecord.java    From sep4j with Apache License 2.0 5 votes vote down vote up
public void setDateProp(String s) {
	if (s == null) {
		return;
	}
	try {
		Date d = DateUtils.parseDate(s, new String[] { "yyyy-MM-dd HH:mm:ss" });
		this.setDateProp(d);
	} catch (ParseException e) {
		throw new IllegalArgumentException(e);
	}

}
 
Example 8
Source File: RdsBinlogOpenApiTest.java    From canal with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimple() throws Throwable {
    Date startTime = DateUtils.parseDate("2018-08-10 12:00:00", new String[] { "yyyy-MM-dd HH:mm:ss" });
    Date endTime = DateUtils.parseDate("2018-08-11 12:00:00", new String[] { "yyyy-MM-dd HH:mm:ss" });
    String url = "https://rds.aliyuncs.com/";
    String ak = "";
    String sk = "";
    String dbInstanceId = "";

    RdsBackupPolicy backupPolicy = RdsBinlogOpenApi.queryBinlogBackupPolicy(url, ak, sk, dbInstanceId);
    System.out.println(backupPolicy);

    List<BinlogFile> binlogFiles = RdsBinlogOpenApi.listBinlogFiles(url, ak, sk, dbInstanceId, startTime, endTime);
    System.out.println(binlogFiles);
}
 
Example 9
Source File: AssetUtils.java    From usergrid with Apache License 2.0 5 votes vote down vote up
/** Attempt to parse the Date from a Date-based header, primarily If-Modified-Since */
public static Date fromIfModifiedSince( String headerValue ) {
    Date moded = null;
    if ( !StringUtils.isEmpty( headerValue ) ) {
        try {
            moded = DateUtils.parseDate( headerValue, DEFAULT_PATTERNS );
        }
        catch ( ParseException pe ) {
            logger.error( "Could not parse date format from If-Modified-Since header: {}", headerValue );
        }
    }
    return moded;
}
 
Example 10
Source File: MysqlMethodInvocationHandler.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
private SQLExpr invokeAddDate(SQLMethodInvokeExpr expr, boolean negative) throws SQLNonTransientException {
    List<SQLExpr> parameters = expr.getParameters();
    if (parameters.size() != 2) {
        throwSyntaxError(expr);
    }
    SQLExpr p1 = parameters.get(0);
    SQLExpr p2 = parameters.get(1);
    if (p1 instanceof SQLMethodInvokeExpr) {
        p1 = doInvoke((SQLMethodInvokeExpr) p1);
    }
    if (p1 instanceof SQLCharExpr) {
        String time = ((SQLCharExpr) p1).getText();
        Integer delta = null;
        String unit = null;
        if (p2 instanceof SQLIntegerExpr) {
            delta = (Integer) ((SQLIntegerExpr) p2).getNumber();
            unit = "DAY";
        } else if (p2 instanceof MySqlIntervalExpr) {
            SQLIntegerExpr value = (SQLIntegerExpr) ((MySqlIntervalExpr) p2).getValue();
            delta = (Integer) value.getNumber();
            unit = ((MySqlIntervalExpr) p2).getUnit().name();
        } else {
            throwSyntaxError(p2);
        }
        try {
            Date date = DateUtils.parseDate(time, SUPPORT_PATTERNS);
            Date result;
            delta = negative ? -delta : delta;
            if ("MONTH".equals(unit)) {
                result = DateUtils.addMonths(date, delta);
            } else if ("DAY".equals(unit)) {
                result = DateUtils.addDays(date, delta);
            } else if ("HOUR".equals(unit)) {
                result = DateUtils.addHours(date, delta);
            } else if ("MINUTE".equals(unit)) {
                result = DateUtils.addMinutes(date, delta);
            } else if ("SECOND".equals(unit)) {
                result = DateUtils.addSeconds(date, delta);
            } else {
                return null;
            }
            String ret = DateFormatUtils.format(result, "yyyy-MM-dd HH:mm:ss");
            return new SQLIdentifierExpr(ret);
        } catch (ParseException e) {
            LOGGER.error("",e);
        }
    }
    return null;
}
 
Example 11
Source File: MoreIndexingFilter.java    From anthelion with Apache License 2.0 4 votes vote down vote up
private long getTime(String date, String url) {
  long time = -1;
  try {
    time = HttpDateFormat.toLong(date);
  } catch (ParseException e) {
// try to parse it as date in alternative format
try {
    Date parsedDate = DateUtils.parseDate(date,
    new String [] {
        "EEE MMM dd HH:mm:ss yyyy",
        "EEE MMM dd HH:mm:ss yyyy zzz",
        "EEE MMM dd HH:mm:ss zzz yyyy",
        "EEE, MMM dd HH:mm:ss yyyy zzz",
        "EEE, dd MMM yyyy HH:mm:ss zzz",
        "EEE,dd MMM yyyy HH:mm:ss zzz",
        "EEE, dd MMM yyyy HH:mm:sszzz",
        "EEE, dd MMM yyyy HH:mm:ss",
        "EEE, dd-MMM-yy HH:mm:ss zzz",
        "yyyy/MM/dd HH:mm:ss.SSS zzz",
        "yyyy/MM/dd HH:mm:ss.SSS",
        "yyyy/MM/dd HH:mm:ss zzz",
        "yyyy/MM/dd",
        "yyyy.MM.dd HH:mm:ss",
        "yyyy-MM-dd HH:mm",
        "MMM dd yyyy HH:mm:ss. zzz",
        "MMM dd yyyy HH:mm:ss zzz",
        "dd.MM.yyyy HH:mm:ss zzz",
        "dd MM yyyy HH:mm:ss zzz",
        "dd.MM.yyyy; HH:mm:ss",
        "dd.MM.yyyy HH:mm:ss",
        "dd.MM.yyyy zzz",
        "yyyy-MM-dd'T'HH:mm:ss'Z'"
    });
    time = parsedDate.getTime();
          // if (LOG.isWarnEnabled()) {
    //   LOG.warn(url + ": parsed date: " + date +" to:"+time);
          // }
} catch (Exception e2) {
          if (LOG.isWarnEnabled()) {
      LOG.warn(url + ": can't parse erroneous date: " + date);
          }
}
  }
  return time;
}
 
Example 12
Source File: MoreIndexingFilter.java    From nutch-htmlunit with Apache License 2.0 4 votes vote down vote up
private long getTime(String date, String url) {
  long time = -1;
  try {
    time = HttpDateFormat.toLong(date);
  } catch (ParseException e) {
// try to parse it as date in alternative format
try {
    Date parsedDate = DateUtils.parseDate(date,
    new String [] {
        "EEE MMM dd HH:mm:ss yyyy",
        "EEE MMM dd HH:mm:ss yyyy zzz",
        "EEE MMM dd HH:mm:ss zzz yyyy",
        "EEE, MMM dd HH:mm:ss yyyy zzz",
        "EEE, dd MMM yyyy HH:mm:ss zzz",
        "EEE,dd MMM yyyy HH:mm:ss zzz",
        "EEE, dd MMM yyyy HH:mm:sszzz",
        "EEE, dd MMM yyyy HH:mm:ss",
        "EEE, dd-MMM-yy HH:mm:ss zzz",
        "yyyy/MM/dd HH:mm:ss.SSS zzz",
        "yyyy/MM/dd HH:mm:ss.SSS",
        "yyyy/MM/dd HH:mm:ss zzz",
        "yyyy/MM/dd",
        "yyyy.MM.dd HH:mm:ss",
        "yyyy-MM-dd HH:mm",
        "MMM dd yyyy HH:mm:ss. zzz",
        "MMM dd yyyy HH:mm:ss zzz",
        "dd.MM.yyyy HH:mm:ss zzz",
        "dd MM yyyy HH:mm:ss zzz",
        "dd.MM.yyyy; HH:mm:ss",
        "dd.MM.yyyy HH:mm:ss",
        "dd.MM.yyyy zzz",
        "yyyy-MM-dd'T'HH:mm:ss'Z'"
    });
    time = parsedDate.getTime();
          // if (LOG.isWarnEnabled()) {
    //   LOG.warn(url + ": parsed date: " + date +" to:"+time);
          // }
} catch (Exception e2) {
          if (LOG.isWarnEnabled()) {
      LOG.warn(url + ": can't parse erroneous date: " + date);
          }
}
  }
  return time;
}