Example usage for org.joda.time DateTimeZone convertLocalToUTC

List of usage examples for org.joda.time DateTimeZone convertLocalToUTC

Introduction

In this page you can find the example usage for org.joda.time DateTimeZone convertLocalToUTC.

Prototype

public long convertLocalToUTC(long instantLocal, boolean strict) 

Source Link

Document

Converts a local instant to an actual UTC instant with the same local time.

Usage

From source file:io.confluent.connect.storage.util.DateTimeUtils.java

License:Open Source License

/**
 * Calculates next period of periodMs after currentTimeMs
 * starting from midnight in given timeZone.
 * If the next period is in next day then 12am of next day
 * will be returned//from w  w w. j  a  va 2s  . c o m
 *
 * @param currentTimeMs time to calculate at
 * @param periodMs period in ms
 * @param timeZone timezone to get midnight time
 * @return timestamp in ms
 */
public static long getNextTimeAdjustedByDay(long currentTimeMs, long periodMs, DateTimeZone timeZone) {
    long startOfDay = timeZone
            .convertLocalToUTC(timeZone.convertUTCToLocal(currentTimeMs) / DAY_IN_MS * DAY_IN_MS, true);
    long nextPeriodOffset = ((currentTimeMs - startOfDay) / periodMs + 1) * periodMs;
    long offset = Math.min(nextPeriodOffset, DAY_IN_MS);
    return startOfDay + offset;
}

From source file:io.prestosql.plugin.hive.GenericHiveRecordCursor.java

License:Apache License

private static long getLongExpressedValue(Object value, DateTimeZone hiveTimeZone) {
    if (value instanceof Date) {
        long storageTime = ((Date) value).getTime();
        // convert date from VM current time zone to UTC
        long utcMillis = storageTime + DateTimeZone.getDefault().getOffset(storageTime);
        return TimeUnit.MILLISECONDS.toDays(utcMillis);
    }/*ww  w.j a v  a2 s. c o  m*/
    if (value instanceof Timestamp) {
        // The Hive SerDe parses timestamps using the default time zone of
        // this JVM, but the data might have been written using a different
        // time zone. We need to convert it to the configured time zone.

        // the timestamp that Hive parsed using the JVM time zone
        long parsedJvmMillis = ((Timestamp) value).getTime();

        // remove the JVM time zone correction from the timestamp
        DateTimeZone jvmTimeZone = DateTimeZone.getDefault();
        long hiveMillis = jvmTimeZone.convertUTCToLocal(parsedJvmMillis);

        // convert to UTC using the real time zone for the underlying data
        long utcMillis = hiveTimeZone.convertLocalToUTC(hiveMillis, false);

        return utcMillis;
    }
    if (value instanceof Float) {
        return floatToRawIntBits(((Float) value));
    }
    return ((Number) value).longValue();
}

From source file:org.apache.falcon.regression.core.util.TimeUtil.java

License:Apache License

public static String getTimeWrtSystemTime(int minutes) {

    DateTime jodaTime = new DateTime(DateTimeZone.UTC);
    if (minutes > 0) {
        jodaTime = jodaTime.plusMinutes(minutes);
    } else {//from   w w w .  ja v  a  2 s  . c o  m
        jodaTime = jodaTime.minusMinutes(-1 * minutes);
    }
    DateTimeFormatter fmt = OozieUtil.getOozieDateTimeFormatter();
    DateTimeZone tz = DateTimeZone.getDefault();
    return fmt.print(tz.convertLocalToUTC(jodaTime.getMillis(), false));
}

From source file:org.cleverbus.common.Tools.java

License:Apache License

/**
 * Converts local instant of {@link DateTime} to standard UTC instant of {@link DateTime} with UTC time zone.
 *
 * @param localDateTime the local time// w  ww  . j a  v  a  2s  .  com
 * @return the UTC time
 */
public static DateTime toUTC(DateTime localDateTime) {
    DateTimeZone dateTimeZone = localDateTime.getZone();
    long utcTime = dateTimeZone.convertLocalToUTC(localDateTime.getMillis(), false);
    return new DateTime(utcTime, DateTimeZone.UTC);
}

From source file:org.jtalks.jcommune.plugin.api.web.dto.PostDto.java

License:Open Source License

/**
 * Gets millisecond representation of post creation date in UTC timezone
 *
 * We store dates in database in timezone of server. And to display correct time of creation draft
 * we convert UTC representation ot user's timezone in javascript
 *
 * @return millisecond representation of post creation date in UTC timezone
 *//*from  w  ww . j a  va  2 s  .  co m*/
public long getUtcCreationTime() {
    DateTimeZone zone = creationDate.getZone();
    return zone.convertLocalToUTC(creationDate.getMillis(), false);
}

From source file:org.jtalks.jcommune.plugin.api.web.velocity.tool.JodaDateTimeTool.java

License:Open Source License

/**
 * Represents dateTime object as string in specified locale and user's timezone.
 * Example: 01 Jan 2011 05:13//from  ww w.  j  av  a 2  s  .  c  o  m
 *
 * @param dateTime Date and time to be converted to string
 * @return dateTime string representation
 */
public String format(DateTime dateTime) {
    if (dateTime == null) {
        return "";
    }
    DateTimeZone timeZone = dateTime.getZone();
    long utcTime = timeZone.convertLocalToUTC(dateTime.getMillis(), false);
    dateTime = new DateTime(utcTime + offset);
    return formatter.withLocale(locale).print(dateTime);
}

From source file:org.jtalks.jcommune.web.tags.FormattedDate.java

License:Open Source License

/**
 * This method adds the corresponding offset to make the date and time
 * match local user's timezone. If value is not a DateTime it's
 * formatted as is//from  w ww  .j  a va 2s  .  com
 *
 * @param value value to be formatted as date
 * @throws JspTagException only from super() call
 */
@Override
public void setValue(Object value) throws JspTagException {
    if (value != null) {
        DateTime time = (DateTime) value;
        DateTimeZone zone = time.getZone();
        long utcTime = zone.convertLocalToUTC(time.getMillis(), false);
        time = new DateTime(utcTime + offset);
        super.setValue(time);
    } else {
        super.setValue(null);
    }
}