private Timestamp convertToTimestamp(Object object) { final long millis; if (object instanceof Long) { millis = (Long) object; } else { // use 'provided' Joda time final DateTime value = (DateTime) object; millis = value.toDate().getTime(); } return new Timestamp(millis - LOCAL_TZ.getOffset(millis)); }
/** * Since all dates from the database are in UTC, we must convert the local date to the date in * UTC time. This function performs that conversion using the TimeZone offset. * * @param localDate The local datetime to convert to a UTC datetime, in milliseconds. * @return The UTC date (the local datetime + the TimeZone offset) in milliseconds. */ public static long getUTCDateFromLocal(long localDate) { TimeZone tz = TimeZone.getDefault(); long gmtOffset = tz.getOffset(localDate); return localDate + gmtOffset; }
TimeZone tz = TimeZone.getDefault(); Date now = new Date(); int offsetFromUtc = tz.getOffset(now.getTime()) / 1000;
public void setTimeZone(TimeZone timeZone) { Calendar calendar = Calendar.getInstance(timeZone); calendar.setTime(startTime.getTime()); startTime = calendar; calendar = Calendar.getInstance(timeZone); calendar.setTime(endTime.getTime()); endTime = calendar; agencyTimeZoneOffset = timeZone.getOffset(startTime.getTimeInMillis()); } }
public static String getCurrentTimezoneOffset() { TimeZone tz = TimeZone.getDefault(); Calendar cal = GregorianCalendar.getInstance(tz); int offsetInMillis = tz.getOffset(cal.getTimeInMillis()); String offset = String.format("%02d:%02d", Math.abs(offsetInMillis / 3600000), Math.abs((offsetInMillis / 60000) % 60)); offset = (offsetInMillis >= 0 ? "+" : "-") + offset; return offset; }
public static String getTimeArea() { return String.valueOf( TimeZone.getDefault().getOffset(new Date().getTime() / 1000)) .toString(); }
/** * Since all dates from the database are in UTC, we must convert the local date to the date in * UTC time. This function performs that conversion using the TimeZone offset. * * @param localDate The local datetime to convert to a UTC datetime, in milliseconds. * @return The UTC date (the local datetime + the TimeZone offset) in milliseconds. */ public static long getUTCDateFromLocal(long localDate) { TimeZone tz = TimeZone.getDefault(); long gmtOffset = tz.getOffset(localDate); return localDate + gmtOffset; }
int offset = tz.getOffset(calendar.getTimeInMillis()); if (offset != 0) { int hours = Math.abs((offset / (60 * 1000)) / 60);
private String createArchiveFileName(final String originalFlowConfigFileName) { TimeZone tz = TimeZone.getDefault(); Calendar cal = GregorianCalendar.getInstance(tz); int offsetInMillis = tz.getOffset(cal.getTimeInMillis()); final int year = cal.get(Calendar.YEAR); final int month = cal.get(Calendar.MONTH) + 1; final int day = cal.get(Calendar.DAY_OF_MONTH); final int hour = cal.get(Calendar.HOUR_OF_DAY); final int min = cal.get(Calendar.MINUTE); final int sec = cal.get(Calendar.SECOND); String offset = String.format("%s%02d%02d", (offsetInMillis >= 0 ? "+" : "-"), Math.abs(offsetInMillis / 3600000), Math.abs((offsetInMillis / 60000) % 60)); return String.format("%d%02d%02dT%02d%02d%02d%s_%s", year, month, day, hour, min, sec, offset, originalFlowConfigFileName); }
public static Date toUtcDate(TimeZone browserTZ, TimeZone targetTZ, Date localDate) { if (localDate == null) { return null; } long local = localDate.getTime(); int targetOffsetFromUTC = targetTZ.getOffset(local); int browserOffsetFromUTC = browserTZ.getOffset(local); return new Date(local - targetOffsetFromUTC + browserOffsetFromUTC); }
// **** YOUR CODE **** BEGIN **** long ts = System.currentTimeMillis(); Date localTime = new Date(ts); String format = "yyyy/MM/dd HH:mm:ss"; SimpleDateFormat sdf = new SimpleDateFormat(format); // Convert Local Time to UTC (Works Fine) sdf.setTimeZone(TimeZone.getTimeZone("UTC")); Date gmtTime = new Date(sdf.format(localTime)); System.out.println("Local:" + localTime.toString() + "," + localTime.getTime() + " --> UTC time:" + gmtTime.toString() + "," + gmtTime.getTime()); // **** YOUR CODE **** END **** // Convert UTC to Local Time Date fromGmt = new Date(gmtTime.getTime() + TimeZone.getDefault().getOffset(localTime.getTime())); System.out.println("UTC time:" + gmtTime.toString() + "," + gmtTime.getTime() + " --> Local:" + fromGmt.toString() + "-" + fromGmt.getTime());
/** * Since all dates from the database are in UTC, we must convert the local date to the date in * UTC time. This function performs that conversion using the TimeZone offset. * * @param localDate The local datetime to convert to a UTC datetime, in milliseconds. * @return The UTC date (the local datetime + the TimeZone offset) in milliseconds. */ public static long getUTCDateFromLocal(long localDate) { TimeZone tz = TimeZone.getDefault(); long gmtOffset = tz.getOffset(localDate); return localDate + gmtOffset; }
int offset = tz.getOffset(calendar.getTimeInMillis()); if (offset != 0) { int hours = Math.abs((offset / (60 * 1000)) / 60);
private Date convertToDate(Object object) { final long millis; if (object instanceof Integer) { final Integer value = (Integer) object; // adopted from Apache Calcite final long t = (long) value * 86400000L; millis = t - (long) LOCAL_TZ.getOffset(t); } else { // use 'provided' Joda time final LocalDate value = (LocalDate) object; millis = value.toDate().getTime(); } return new Date(millis); }
public VelocityContext getVelocityContext(Map<String, Object> modelMap, Class<? extends SparkController> controller, String viewName) { HashMap<String, Object> context = new HashMap<>(modelMap); context.put("railsAssetsService", railsAssetsService); context.put("webpackAssetsService", webpackAssetsService); context.put("securityService", securityService); context.put("maintenanceModeService", maintenanceModeService); context.put("currentUser", SessionUtils.currentUsername()); context.put("controllerName", humanizedControllerName(controller)); context.put("viewName", viewName); context.put("currentVersion", CurrentGoCDVersion.getInstance()); context.put("toggles", Toggles.class); context.put("goUpdate", versionInfoService.getGoUpdate()); context.put("goUpdateCheckEnabled", versionInfoService.isGOUpdateCheckEnabled()); context.put("serverTimezoneUTCOffset", TimeZone.getDefault().getOffset(new Date().getTime())); context.put("spaRefreshInterval", SystemEnvironment.goSpaRefreshInterval()); context.put("spaTimeout", SystemEnvironment.goSpaTimeout()); context.put("showAnalyticsDashboard", showAnalyticsDashboard()); context.put("devMode", !new SystemEnvironment().useCompressedJs()); return new VelocityContext(context); }
/** * Since all dates from the database are in UTC, we must convert the local date to the date in * UTC time. This function performs that conversion using the TimeZone offset. * * @param localDate The local datetime to convert to a UTC datetime, in milliseconds. * @return The UTC date (the local datetime + the TimeZone offset) in milliseconds. */ public static long getUTCDateFromLocal(long localDate) { TimeZone tz = TimeZone.getDefault(); long gmtOffset = tz.getOffset(localDate); return localDate + gmtOffset; }
@Override public String toString(final Object obj) { final Calendar calendar = (Calendar)obj; final Instant instant = Instant.ofEpochMilli(calendar.getTimeInMillis()); final int offsetInMillis = calendar.getTimeZone().getOffset(calendar.getTimeInMillis()); final OffsetDateTime offsetDateTime = OffsetDateTime.ofInstant(instant, ZoneOffset.ofTotalSeconds(offsetInMillis / 1000)); return STD_DATE_TIME.format(offsetDateTime); } }
/** * Translate a date & time from a users time zone to the another * (probably server) time zone to assist in creating a simple trigger with * the right date & time. * * @param date the date to translate * @param src the original time-zone * @param dest the destination time-zone * @return the translated date */ public static Date translateTime(Date date, TimeZone src, TimeZone dest) { Date newDate = new Date(); int offset = (dest.getOffset(date.getTime()) - src.getOffset(date.getTime())); newDate.setTime(date.getTime() - offset); return newDate; }
public FlowConfigurationDTO createFlowConfigurationDto(final String autoRefreshInterval, final Long defaultBackPressureObjectThreshold, final String defaultBackPressureDataSizeThreshold) { final FlowConfigurationDTO dto = new FlowConfigurationDTO(); // get the refresh interval final long refreshInterval = FormatUtils.getTimeDuration(autoRefreshInterval, TimeUnit.SECONDS); dto.setAutoRefreshIntervalSeconds(refreshInterval); dto.setSupportsManagedAuthorizer(AuthorizerCapabilityDetection.isManagedAuthorizer(authorizer)); dto.setSupportsConfigurableUsersAndGroups(AuthorizerCapabilityDetection.isConfigurableUserGroupProvider(authorizer)); dto.setSupportsConfigurableAuthorizer(AuthorizerCapabilityDetection.isConfigurableAccessPolicyProvider(authorizer)); final Date now = new Date(); dto.setTimeOffset(TimeZone.getDefault().getOffset(now.getTime())); dto.setCurrentTime(now); dto.setDefaultBackPressureDataSizeThreshold(defaultBackPressureDataSizeThreshold); dto.setDefaultBackPressureObjectThreshold(defaultBackPressureObjectThreshold); return dto; }