下面列出了java.util.Date#toInstant ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void scheduleAvailability(String id, boolean availabilityRestricted, Date openDate, Date closeDate) {
// Remove any existing notifications for this area
scheduledInvocationManager
.deleteDelayedInvocation("org.sakaiproject.api.app.messageforums.ForumScheduleNotification", id);
if (availabilityRestricted) {
Instant openTime = null;
Instant closeTime = null;
if (openDate != null) {
openTime = openDate.toInstant();
}
if (closeDate != null) {
closeTime = closeDate.toInstant();
}
// Schedule the new notification
if (openTime != null && openTime.isAfter(Instant.now())) {
scheduledInvocationManager.createDelayedInvocation(openTime,
"org.sakaiproject.api.app.messageforums.ForumScheduleNotification", id);
} else if (closeTime != null && closeTime.isAfter(Instant.now())) {
scheduledInvocationManager.createDelayedInvocation(closeTime,
"org.sakaiproject.api.app.messageforums.ForumScheduleNotification", id);
}
}
}
public static Instant toInstant(Date date) {
if (date == null) {
return null;
} else {
return date.toInstant();
}
}
@Test
public void UDateToLocalTime() {
Date date = new Date();
System.out.println("date:" + date);
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
LocalTime localTime = localDateTime.toLocalTime();
System.out.println("localTime:" + localTime);
}
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;
}
public static LocalDate date2LocalDate(Date date) {
try {
Instant instant = date.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
LocalDate localDate = instant.atZone(zoneId).toLocalDate();
return localDate;
} catch (Exception e) {
return null;
}
}
public static synchronized Instant toInstant(int day) {
try {
Date date = intDayFormatter.parse("" + day);
return date.toInstant();
} catch (ParseException e) {
throw new ArgumentException("Invalid day format");
}
}
/**
* Parse date string.
*
* @param date the date
* @return the string
*/
public static String parseDate(Date date) {
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
return formatLocalDateTime(localDateTime);
}
/**
* Gets the compliant trend.This request expects asset group,domain and from
* as mandatory.If API receives asset group,domain and from as request
* parameters, it gives weekly based details of compliance info from
* mentioned date to till date by rule category
*
* @param request
* the request
* @return ResponseEntity
* @RequestBody request
*/
@RequestMapping(path = "/v1/trend/compliance", method = RequestMethod.POST)
public ResponseEntity<Object> getCompliantTrend(@RequestBody(required = true) CompliantTrendRequest request) {
Map<String, Object> response = new HashMap<>();
String assetGroup = request.getAg();
Date input = request.getFrom();
if (input == null) {
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("UTC"));
cal.add(Calendar.DATE, NEG_THIRTY);
input = cal.getTime();
}
Instant instant = input.toInstant();
ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
LocalDate fromDate = zdt.toLocalDate();
Map<String, String> filter = request.getFilters();
if (Strings.isNullOrEmpty(assetGroup) || MapUtils.isEmpty(filter) || Strings.isNullOrEmpty(filter.get(DOMAIN))) {
return ResponseUtils.buildFailureResponse(new Exception(ASSET_GROUP_DOMAIN));
}
String domain = filter.get(DOMAIN);
try {
Map<String, Object> trendData = trendService.getComplianceTrendProgress(assetGroup, fromDate, domain);
response.put(RESPONSE, trendData);
} catch (ServiceException e) {
LOGGER.error("Exception in getCompliantTrend()" ,e.getMessage());
return ResponseUtils.buildFailureResponse(e);
}
return ResponseUtils.buildSucessResponse(response);
}
public boolean makeAvailableHelper(boolean availabilityRestricted, Date openDate, Date closeDate) {
boolean makeAvailable = true;
if (availabilityRestricted) {
// availability is being restricted:
makeAvailable = false;
boolean afterOpen = false;
boolean beforeClose = false;
Instant openTime = null;
Instant closeTime = null;
if (openDate != null) {
openTime = openDate.toInstant();
}
if (closeDate != null) {
closeTime = closeDate.toInstant();
}
if (closeDate == null && openDate == null) {
// user didn't specify either, so open topic
makeAvailable = true;
}
if (openTime != null && openTime.isBefore(Instant.now())) {
afterOpen = true;
} else if (openTime == null) {
afterOpen = true;
}
if (closeTime != null && closeTime.isAfter(Instant.now())) {
beforeClose = true;
} else if (closeTime == null) {
beforeClose = true;
}
if (afterOpen && beforeClose) {
makeAvailable = true;
}
}
return makeAvailable;
}
private IModel<String> getDateAsString(IModel<Date> model) {
Date date = model.getObject();
if (date == null) {
return Model.of();
}
Instant instant = date.toInstant();
LocalDate localDate = instant.atZone(clientZone).toLocalDate();
return Model.of(localDate.format(getDateFormatter()));
}
@Override
public OffsetDateTime toOffsetDateTime(Date date) {
Instant instant = date.toInstant();
return OffsetDateTime.ofInstant(instant, ZoneId.systemDefault());
}
public static void main(String[] args) throws Throwable {
int N = 10000;
long t1970 = new java.util.Date(70, 0, 01).getTime();
Random r = new Random();
for (int i = 0; i < N; i++) {
int days = r.nextInt(50) * 365 + r.nextInt(365);
long secs = t1970 + days * 86400 + r.nextInt(86400);
int nanos = r.nextInt(NANOS_PER_SECOND);
int nanos_ms = nanos / 1000000 * 1000000; // millis precision
long millis = secs * 1000 + r.nextInt(1000);
LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC);
LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC);
Instant inst = Instant.ofEpochSecond(secs, nanos);
Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms);
///////////// java.util.Date /////////////////////////
Date jud = new java.util.Date(millis);
Instant inst0 = jud.toInstant();
if (jud.getTime() != inst0.toEpochMilli() ||
!jud.equals(Date.from(inst0))) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d");
}
// roundtrip only with millis precision
Date jud0 = Date.from(inst_ms);
if (jud0.getTime() != inst_ms.toEpochMilli() ||
!inst_ms.equals(jud0.toInstant())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: instant -> j.u.d -> instant");
}
//////////// java.util.GregorianCalendar /////////////
GregorianCalendar cal = new GregorianCalendar();
// non-roundtrip of tz name between j.u.tz and j.t.zid
cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE));
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(4);
cal.setTimeInMillis(millis);
ZonedDateTime zdt0 = cal.toZonedDateTime();
if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() ||
!cal.equals(GregorianCalendar.from(zdt0))) {
System.out.println("cal:" + cal);
System.out.println("zdt:" + zdt0);
System.out.println("calNew:" + GregorianCalendar.from(zdt0));
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: gcal -> zdt -> gcal");
}
inst0 = cal.toInstant();
if (cal.getTimeInMillis() != inst0.toEpochMilli()) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: gcal -> zdt");
}
ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault());
GregorianCalendar cal0 = GregorianCalendar.from(zdt);
if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() ||
!zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: zdt -> gcal -> zdt");
}
}
///////////// java.util.TimeZone /////////////////////////
for (String zidStr : TimeZone.getAvailableIDs()) {
// TBD: tzdt intergration
if (zidStr.startsWith("SystemV") ||
zidStr.contains("Riyadh8") ||
zidStr.equals("US/Pacific-New") ||
zidStr.equals("EST") ||
zidStr.equals("HST") ||
zidStr.equals("MST")) {
continue;
}
ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS);
if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) {
throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr);
}
TimeZone tz = TimeZone.getTimeZone(zidStr);
// no round-trip for alias and "GMT"
if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) &&
!ZoneId.SHORT_IDS.containsKey(zidStr) &&
!zidStr.startsWith("GMT")) {
throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr);
}
}
System.out.println("Passed!");
}
public static void main(String[] args) throws Throwable {
int N = 10000;
long t1970 = new java.util.Date(70, 0, 01).getTime();
Random r = new Random();
for (int i = 0; i < N; i++) {
int days = r.nextInt(50) * 365 + r.nextInt(365);
long secs = t1970 + days * 86400 + r.nextInt(86400);
int nanos = r.nextInt(NANOS_PER_SECOND);
int nanos_ms = nanos / 1000000 * 1000000; // millis precision
long millis = secs * 1000 + r.nextInt(1000);
LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC);
LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC);
Instant inst = Instant.ofEpochSecond(secs, nanos);
Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms);
///////////// java.util.Date /////////////////////////
Date jud = new java.util.Date(millis);
Instant inst0 = jud.toInstant();
if (jud.getTime() != inst0.toEpochMilli() ||
!jud.equals(Date.from(inst0))) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d");
}
// roundtrip only with millis precision
Date jud0 = Date.from(inst_ms);
if (jud0.getTime() != inst_ms.toEpochMilli() ||
!inst_ms.equals(jud0.toInstant())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: instant -> j.u.d -> instant");
}
//////////// java.util.GregorianCalendar /////////////
GregorianCalendar cal = new GregorianCalendar();
// non-roundtrip of tz name between j.u.tz and j.t.zid
cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE));
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(4);
cal.setTimeInMillis(millis);
ZonedDateTime zdt0 = cal.toZonedDateTime();
if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() ||
!cal.equals(GregorianCalendar.from(zdt0))) {
System.out.println("cal:" + cal);
System.out.println("zdt:" + zdt0);
System.out.println("calNew:" + GregorianCalendar.from(zdt0));
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: gcal -> zdt -> gcal");
}
inst0 = cal.toInstant();
if (cal.getTimeInMillis() != inst0.toEpochMilli()) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: gcal -> zdt");
}
ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault());
GregorianCalendar cal0 = GregorianCalendar.from(zdt);
if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() ||
!zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: zdt -> gcal -> zdt");
}
}
///////////// java.util.TimeZone /////////////////////////
for (String zidStr : TimeZone.getAvailableIDs()) {
// TBD: tzdt intergration
if (zidStr.startsWith("SystemV") ||
zidStr.contains("Riyadh8") ||
zidStr.equals("US/Pacific-New") ||
zidStr.equals("EST") ||
zidStr.equals("HST") ||
zidStr.equals("MST")) {
continue;
}
ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS);
if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) {
throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr);
}
TimeZone tz = TimeZone.getTimeZone(zidStr);
// no round-trip for alias and "GMT"
if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) &&
!ZoneId.SHORT_IDS.containsKey(zidStr) &&
!zidStr.startsWith("GMT")) {
throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr);
}
}
System.out.println("Passed!");
}
public static Instant toInstant(Date date) {
return date.toInstant();
}
public static void main(String[] args) throws Throwable {
int N = 10000;
long t1970 = new java.util.Date(70, 0, 01).getTime();
Random r = new Random();
for (int i = 0; i < N; i++) {
int days = r.nextInt(50) * 365 + r.nextInt(365);
long secs = t1970 + days * 86400 + r.nextInt(86400);
int nanos = r.nextInt(NANOS_PER_SECOND);
int nanos_ms = nanos / 1000000 * 1000000; // millis precision
long millis = secs * 1000 + r.nextInt(1000);
LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC);
LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC);
Instant inst = Instant.ofEpochSecond(secs, nanos);
Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms);
///////////// java.util.Date /////////////////////////
Date jud = new java.util.Date(millis);
Instant inst0 = jud.toInstant();
if (jud.getTime() != inst0.toEpochMilli() ||
!jud.equals(Date.from(inst0))) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d");
}
// roundtrip only with millis precision
Date jud0 = Date.from(inst_ms);
if (jud0.getTime() != inst_ms.toEpochMilli() ||
!inst_ms.equals(jud0.toInstant())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: instant -> j.u.d -> instant");
}
//////////// java.util.GregorianCalendar /////////////
GregorianCalendar cal = new GregorianCalendar();
// non-roundtrip of tz name between j.u.tz and j.t.zid
cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE));
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(4);
cal.setTimeInMillis(millis);
ZonedDateTime zdt0 = cal.toZonedDateTime();
if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() ||
!cal.equals(GregorianCalendar.from(zdt0))) {
System.out.println("cal:" + cal);
System.out.println("zdt:" + zdt0);
System.out.println("calNew:" + GregorianCalendar.from(zdt0));
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: gcal -> zdt -> gcal");
}
inst0 = cal.toInstant();
if (cal.getTimeInMillis() != inst0.toEpochMilli()) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: gcal -> zdt");
}
ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault());
GregorianCalendar cal0 = GregorianCalendar.from(zdt);
if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() ||
!zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) {
System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt);
throw new RuntimeException("FAILED: zdt -> gcal -> zdt");
}
}
///////////// java.util.TimeZone /////////////////////////
for (String zidStr : TimeZone.getAvailableIDs()) {
// TBD: tzdt intergration
if (zidStr.startsWith("SystemV") ||
zidStr.contains("Riyadh8") ||
zidStr.equals("US/Pacific-New") ||
zidStr.equals("EST") ||
zidStr.equals("HST") ||
zidStr.equals("MST")) {
continue;
}
ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS);
if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) {
throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr);
}
TimeZone tz = TimeZone.getTimeZone(zidStr);
// no round-trip for alias and "GMT"
if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) &&
!ZoneId.SHORT_IDS.containsKey(zidStr) &&
!zidStr.startsWith("GMT")) {
throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr);
}
}
System.out.println("Passed!");
}
@Override
public Instant convert(Date in, Context context) throws Exception {
if (in == null) return null;
return in.toInstant();
}
private String formatToUserDateFormat(Date date) {
if (date == null) return "";
Instant instant = date.toInstant();
return formatToUserInstantFormat(instant);
}
public static LocalDateTime date2LocalDateTime(final Date date) {
Instant instant = date.toInstant();
ZonedDateTime zdt = instant.atZone(DEFAULT_ZONE_ID);
return zdt.toLocalDateTime();
}
public static Instant dateToInstant(Date date) {
Objects.requireNonNull(date, "The provided date cannot be null");
return date.toInstant();
}
public static LocalDate toLocalDate(Date date) {
Instant instant = date.toInstant();
return instant.atZone(ZoneId.systemDefault()).toLocalDate();
}