下面列出了java.util.Date#from ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Decision(POST)
public OidcSession post(RestContext context) {
String oidcSessionId = UUID.randomUUID().toString();
OidcSession oidcSession = OidcSession.create(config.getSecureRandom());
storeProvider.getStore(OIDC_SESSION).write(oidcSessionId, oidcSession);
Cookie cookie = Cookie.create(COOKIE_NAME, oidcSessionId);
ZoneId zone = ZoneId.systemDefault();
Date expires = Date.from(
ZonedDateTime.of(LocalDateTime.now()
.plusSeconds(config.getOidcSessionExpires()), zone)
.toInstant());
cookie.setExpires(expires);
cookie.setPath("/");
context.setHeaders(Headers.of("Set-Cookie", cookie.toHttpString()));
return oidcSession;
}
@Override
public Date convertToDatabaseColumn(LocalDateTime localDateTime) {
if(localDateTime != null) {
Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
return Date.from(instant);
}
return null;
}
@Test
public void testGetDateWithoutConversionMask() throws KettleValueException, ParseException {
Calendar date = new GregorianCalendar( 2017, 9, 20 ); // month 9 = Oct
String value = "2017/10/20 00:00:00.000";
ValueMetaInterface stringValueMeta = new ValueMetaString( "test" );
Date expected = Date.from( date.toInstant() );
Date result = stringValueMeta.getDate( value );
assertEquals( expected, result );
}
/**
* Parse the date string input using the ISO_ZONED_DATE_TIME format such as '2017-12-03T10:15:30+01:00[Europe/Paris]'.
*
* @param inputDate
* The string that needs to be parsed.
*
* @throws Exception
* If not a valid date compared to ISO_ZONED_DATE_TIME format
*/
public static Date parseISODate(final String inputDate) {
Date convertedDate = null;
try {
LocalDateTime ldt = LocalDateTime.parse(inputDate, isoFormatter);
convertedDate = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());
} catch (Exception e) {
log.warn("Error parsing the date {} using the ISO_ZONED_DATE_TIME format", inputDate);
}
return convertedDate;
}
@Test
void recordVariableCreate() {
VariableInstanceEntity variable = new VariableInstanceEntityImpl();
Date createTime = Date.from(Instant.now().minusSeconds(5));
compositeHistoryManager.recordVariableCreate(variable, createTime);
verify(historyManager1).recordVariableCreate(same(variable), eq(createTime));
verify(historyManager2).recordVariableCreate(same(variable), eq(createTime));
}
private Date calc (Expression expression, Date valueParameter) {
if ((expression != null) && (valueParameter != null) && (! isnull)) {
Instant i = valueParameter.toInstant ();
double day = 24 * 60 * 60;
double epoch = i.getEpochSecond () / day;
double result = calc (expression, epoch);
if (result != epoch) {
i = Instant.ofEpochSecond ((long) (result * day));
valueParameter = Date.from (i);
}
}
return valueParameter;
}
BlobInfo( String containerName, String blobName, BlobProperties properties ) {
this(containerName, blobName, properties.getContentMd5(),
properties.getBlobSize(), properties.getETag(), properties.getContentType(),
toAtsBlobType(properties.getBlobType()),
Date.from(properties.getLastModified().toInstant()),
Date.from(properties.getCreationTime().toInstant()), properties.getMetadata(),
toAtsAccessTier(properties.getAccessTier()));
}
@ApiOperation(value = "[public] Get basic information of the user",
notes = "Get other user's basic information.")
@RequiredScope(ClientAppScope.PUBLIC)
@RequestMapping(value = "/{username}/basic", method = RequestMethod.GET)
public V1UserBasicDto basicByUsername(ClientAppUserAccessToken accessToken,
@PathVariable("username") String username) {
Account account = accountService.loadAccount(username);
return new V1UserBasicDto(account.getUsername(),
account.getDescription(),
Date.from(account.getCreateTime()));
}
@Test
public void testConvert() {
Instant now = Instant.now();
Date nowDate = Date.from(now);
//init
subscriptionEntity = new SubscriptionEntity();
subscriptionEntity.setApi(SUBSCRIPTION_API);
subscriptionEntity.setApplication(SUBSCRIPTION_APPLICATION);
subscriptionEntity.setClientId(SUBSCRIPTION_CLIENT_ID);
subscriptionEntity.setClosedAt(nowDate);
subscriptionEntity.setCreatedAt(nowDate);
subscriptionEntity.setEndingAt(nowDate);
subscriptionEntity.setId(SUBSCRIPTION_ID);
subscriptionEntity.setPausedAt(nowDate);
subscriptionEntity.setPlan(SUBSCRIPTION_PLAN);
subscriptionEntity.setProcessedAt(nowDate);
subscriptionEntity.setProcessedBy(SUBSCRIPTION_PROCESSED_BY);
subscriptionEntity.setReason(SUBSCRIPTION_REASON);
subscriptionEntity.setRequest(SUBSCRIPTION_REQUEST);
subscriptionEntity.setStartingAt(nowDate);
subscriptionEntity.setStatus(SubscriptionStatus.ACCEPTED);
subscriptionEntity.setSubscribedBy(SUBSCRIPTION_SUBSCRIBED_BY);
subscriptionEntity.setUpdatedAt(nowDate);
//Test
Subscription subscription = subscriptionMapper.convert(subscriptionEntity);
assertNotNull(subscription);
assertEquals(SUBSCRIPTION_API, subscription.getApi());
assertEquals(SUBSCRIPTION_APPLICATION, subscription.getApplication());
assertEquals(now.toEpochMilli(), subscription.getCreatedAt().toInstant().toEpochMilli());
assertEquals(now.toEpochMilli(), subscription.getEndAt().toInstant().toEpochMilli());
assertEquals(SUBSCRIPTION_ID, subscription.getId());
assertEquals(SUBSCRIPTION_PLAN, subscription.getPlan());
assertEquals(now.toEpochMilli(), subscription.getProcessedAt().toInstant().toEpochMilli());
assertEquals(SUBSCRIPTION_REQUEST, subscription.getRequest());
assertEquals(now.toEpochMilli(), subscription.getStartAt().toInstant().toEpochMilli());
assertEquals(StatusEnum.ACCEPTED, subscription.getStatus());
}
public static Date parseDate(String date) {
if (date == null) return null;
OffsetDateTime odt = OffsetDateTime.parse(date, dateTimeFormatter);
return Date.from(odt.toInstant());
}
private Date buildFeedUpdateTime(List<Article> articles, Instant fallbackTime) {
return Date.from(articles.stream()
.collect(maxBy(comparing(Article::getCreateTime)))
.map(Article::getCreateTime)
.orElse(fallbackTime));
}
/**
* 将LocalDateTime 转换成 Date
*/
public static Date localDateTimeToDate(LocalDateTime localDateTime) {
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zdt = localDateTime.atZone(zoneId);
return Date.from(zdt.toInstant());
}
/**
* @return the start of week (sunday in the US, monday in France) at 0am in the
* current timezone
*/
public static Date getStartOfThisWeek() {
return Date.from(LocalDate.now().minusDays(LocalDate.now().getDayOfWeek().getValue()).atStartOfDay()
.atZone(ZoneId.systemDefault()).toInstant());
}
@Override
public Date convert(Instant in, Context context) throws Exception {
if (in == null) return null;
return Date.from(in);
}
public static Date toDate(Instant instant) {
return Date.from(instant);
}
static Date fromZonedDateTime(ZonedDateTime dateTime) {
return Date.from(dateTime.toInstant());
}
/**
* 昨天零点
*
* @return
*/
public static Date yesterday() {
LocalDateTime startOfDay = LocalDate.now().minusDays(1).atStartOfDay();
Date today = Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
return new Date();
}
protected SamlAssertionWrapper createSamlAssertion(Idp idp, TrustedIdp trustedIdp, JsonMapObject claims,
String subjectName,
Instant notBefore,
Instant expires) throws Exception {
SamlCallbackHandler callbackHandler = new SamlCallbackHandler();
String issuer = idp.getServiceDisplayName();
if (issuer == null) {
issuer = idp.getRealm();
}
if (issuer != null) {
callbackHandler.setIssuer(issuer);
}
// Subject
SubjectBean subjectBean =
new SubjectBean(subjectName, SAML2Constants.NAMEID_FORMAT_UNSPECIFIED, SAML2Constants.CONF_BEARER);
callbackHandler.setSubjectBean(subjectBean);
// Conditions
ConditionsBean conditionsBean = new ConditionsBean();
conditionsBean.setNotAfter(new DateTime(Date.from(expires)));
if (notBefore != null) {
DateTime notBeforeDT = new DateTime(Date.from(notBefore));
conditionsBean.setNotBefore(notBeforeDT);
} else {
conditionsBean.setNotBefore(new DateTime());
}
callbackHandler.setConditionsBean(conditionsBean);
// Claims
String claimsHandler = getProperty(trustedIdp, CLAIMS_HANDLER);
if (claimsHandler != null) {
ClaimsHandler claimsHandlerImpl = (ClaimsHandler)Loader.loadClass(claimsHandler).newInstance();
AttributeStatementBean attrStatementBean = claimsHandlerImpl.handleClaims(claims);
callbackHandler.setAttrBean(attrStatementBean);
}
SAMLCallback samlCallback = new SAMLCallback();
SAMLUtil.doSAMLCallback(callbackHandler, samlCallback);
SamlAssertionWrapper assertion = new SamlAssertionWrapper(samlCallback);
Crypto crypto = CertsUtils.getCryptoFromCertificate(idp.getCertificate());
assertion.signAssertion(crypto.getDefaultX509Identifier(), idp.getCertificatePassword(),
crypto, false);
return assertion;
}
@Override
public Date now() {
return Date.from(ZonedDateTime.now(zoneId).toInstant());
}
/**
* 格式化日期(yyyy-MM-dd HH:mm:ss.SSS格式)
* @see frodez.constant.settings.DefTime#PRECISIVE_PATTERN
* @author Frodez
* @date 2019-02-17
*/
public static Date precisiveTime(String date) {
return Date.from(LocalDateTime.parse(date, DefTime.PRECISIVE_FORMATTER).atOffset(DefTime.DEFAULT_OFFSET).toInstant());
}