下面列出了怎么用org.apache.commons.lang3.time.DateParser的API类实例代码及写法,或者点击链接到github查看源代码。
private static Date parseDate(String dateString) {
if (dateString == null) return null;
Matcher m = secondsSinceEpochPattern.matcher(dateString);
if (m.matches()) {
return new Date(Long.parseLong(dateString) * 1000);
}
for (DateParser format : allowableInputFormats){
try {
return format.parse(dateString);
}
catch (ParseException ex) {
}
}
return null;
}
@Test
public void CreateJwt_ExplicitNotBeforeTime() throws Exception {
String notBeforeString = "2017-08-14T11:00:21.269-0700";
Map properties = new HashMap();
properties.put("algorithm", "RS256");
properties.put("debug", "true");
properties.put("not-before", notBeforeString);
properties.put("private-key", privateKeyMap.get("rsa-private-3"));
properties.put("expiresIn", "300"); // seconds
properties.put("claim_testname", "CreateJwt_ExplicitNotBeforeTime");
properties.put("claim_jti", java.util.UUID.randomUUID().toString());
JwtCreatorCallout callout = new JwtCreatorCallout(properties);
ExecutionResult result = callout.execute(msgCtxt, exeCtxt);
// check result and output
Assert.assertEquals(result, ExecutionResult.SUCCESS);
// retrieve and check output
String jwt = msgCtxt.getVariable("jwt_jwt");
System.out.println("jwt: " + jwt);
String jwtClaims = msgCtxt.getVariable("jwt_claims");
Assert.assertNotNull(jwtClaims, "jwt_claims");
System.out.println("claims: " + jwtClaims);
JsonNode claimsNode = om.readTree(jwtClaims);
String nbfAsText = claimsNode.get("nbf").asText();
Assert.assertNotNull(nbfAsText, "nbf");
DateParser dp = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSSZ", TimeZone.getTimeZone("UTC"));
Date notBefore = dp.parse(notBeforeString);
int secondsNbfExpected = (int) (notBefore.getTime()/1000);
int secondsNbfActual = Integer.parseInt(nbfAsText);
Assert.assertEquals( secondsNbfActual, secondsNbfExpected, "nbf");
}