Java Code Examples for java.util.Date#toInstant()

The following examples show how to use java.util.Date#toInstant() . 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: ForumScheduleNotificationImpl.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
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);
		}
	}
}
 
Example 2
Source File: DateHelper.java    From flow-platform-x with Apache License 2.0 5 votes vote down vote up
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");
    }
}
 
Example 3
Source File: ODateField.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
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()));
}
 
Example 4
Source File: ForumScheduleNotificationImpl.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
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;
}
 
Example 5
Source File: TrendController.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 6
Source File: DateUtils.java    From myth with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 7
Source File: DateTools.java    From MyBox with Apache License 2.0 5 votes vote down vote up
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;
    }
}
 
Example 8
Source File: Column.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 9
Source File: LocalDateApiTest.java    From spring-boot-cookbook with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 10
Source File: TimeUtils.java    From pnc with Apache License 2.0 5 votes vote down vote up
public static Instant toInstant(Date date) {
    if (date == null) {
        return null;
    } else {
        return date.toInstant();
    }
}
 
Example 11
Source File: JavatimeTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
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!");
    }
 
Example 12
Source File: Primitives.java    From gremlin-ogm with Apache License 2.0 4 votes vote down vote up
public static Instant toInstant(Date date) {
  return date.toInstant();
}
 
Example 13
Source File: JavatimeTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
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!");
    }
 
Example 14
Source File: JavatimeTest.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
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!");
    }
 
Example 15
Source File: DateToJavaInstantConverter.java    From SimpleFlatMapper with MIT License 4 votes vote down vote up
@Override
public Instant convert(Date in, Context context) throws Exception {
    if (in == null) return null;
    return in.toInstant();
}
 
Example 16
Source File: DefaultDateTimeMapper.java    From crnk-framework with Apache License 2.0 4 votes vote down vote up
@Override
public OffsetDateTime toOffsetDateTime(Date date) {
	Instant instant = date.toInstant();
	return OffsetDateTime.ofInstant(instant, ZoneId.systemDefault());
}
 
Example 17
Source File: DateManagerServiceImpl.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
private String formatToUserDateFormat(Date date) {
	if (date == null) return "";
	Instant instant = date.toInstant();
	return formatToUserInstantFormat(instant);
}
 
Example 18
Source File: ToolDatePlus.java    From protools with Apache License 2.0 4 votes vote down vote up
public static LocalDateTime date2LocalDateTime(final Date date) {
    Instant instant = date.toInstant();
    ZonedDateTime zdt = instant.atZone(DEFAULT_ZONE_ID);
    return zdt.toLocalDateTime();
}
 
Example 19
Source File: DateConverters.java    From Java-Coding-Problems with MIT License 2 votes vote down vote up
public static Instant dateToInstant(Date date) {

        Objects.requireNonNull(date, "The provided date cannot be null");

        return date.toInstant();
    }
 
Example 20
Source File: DateTool.java    From axelor-open-suite with GNU Affero General Public License v3.0 2 votes vote down vote up
public static LocalDate toLocalDate(Date date) {

    Instant instant = date.toInstant();

    return instant.atZone(ZoneId.systemDefault()).toLocalDate();
  }