Example usage for org.joda.time DateTimeZone forTimeZone

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

Introduction

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

Prototype

public static DateTimeZone forTimeZone(TimeZone zone) 

Source Link

Document

Gets a time zone instance for a JDK TimeZone.

Usage

From source file:org.apache.beam.sdk.extensions.sql.zetasql.DateFunctions.java

License:Apache License

public DateTime date(DateTime ts, String timezone) {
    return ts.withZoneRetainFields(DateTimeZone.forTimeZone(TimeZone.getTimeZone(timezone)));
}

From source file:org.apache.beam.sdk.extensions.sql.zetasql.TimestampFunctions.java

License:Apache License

@Strict
public static DateTime timestamp(String timestampStr, String timezone) {
    return DateTimeUtils.findDateTimePattern(timestampStr)
            .withZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone(timezone))).parseDateTime(timestampStr);
}

From source file:org.apache.beam.sdk.extensions.sql.zetasql.TimestampFunctions.java

License:Apache License

@Strict
public static DateTime timestamp(Integer numOfDays, String timezone) {
    return new DateTime((long) numOfDays * DateTimeUtils.MILLIS_PER_DAY, DateTimeZone.UTC)
            .withZoneRetainFields(DateTimeZone.forTimeZone(TimeZone.getTimeZone(timezone)));
}

From source file:org.apache.druid.java.util.common.DateTimes.java

License:Apache License

@SuppressForbidden(reason = "DateTimeZone#forID")
public static DateTimeZone inferTzFromString(String tzId) {
    try {/* w w  w  .  j a  v a  2  s . com*/
        return DateTimeZone.forID(tzId);
    } catch (IllegalArgumentException e) {
        // also support Java timezone strings
        return DateTimeZone.forTimeZone(TimeZone.getTimeZone(tzId));
    }
}

From source file:org.apache.hadoop.hive.ql.optimizer.calcite.translator.ASTBuilder.java

License:Apache License

public static ASTNode literal(RexLiteral literal, boolean useTypeQualInLiteral) {
    Object val = null;
    int type = 0;
    SqlTypeName sqlType = literal.getType().getSqlTypeName();

    switch (sqlType) {
    case BINARY:/*w w  w  .j  a  va2  s . com*/
    case DATE:
    case TIME:
    case TIMESTAMP:
    case INTERVAL_DAY:
    case INTERVAL_DAY_HOUR:
    case INTERVAL_DAY_MINUTE:
    case INTERVAL_DAY_SECOND:
    case INTERVAL_HOUR:
    case INTERVAL_HOUR_MINUTE:
    case INTERVAL_HOUR_SECOND:
    case INTERVAL_MINUTE:
    case INTERVAL_MINUTE_SECOND:
    case INTERVAL_MONTH:
    case INTERVAL_SECOND:
    case INTERVAL_YEAR:
    case INTERVAL_YEAR_MONTH:
        if (literal.getValue() == null) {
            return ASTBuilder.construct(HiveParser.TOK_NULL, "TOK_NULL").node();
        }
        break;
    case TINYINT:
    case SMALLINT:
    case INTEGER:
    case BIGINT:
    case DOUBLE:
    case DECIMAL:
    case FLOAT:
    case REAL:
    case VARCHAR:
    case CHAR:
    case BOOLEAN:
        if (literal.getValue3() == null) {
            return ASTBuilder.construct(HiveParser.TOK_NULL, "TOK_NULL").node();
        }
    }

    switch (sqlType) {
    case TINYINT:
        if (useTypeQualInLiteral) {
            val = literal.getValue3() + "Y";
        } else {
            val = literal.getValue3();
        }
        type = HiveParser.IntegralLiteral;
        break;
    case SMALLINT:
        if (useTypeQualInLiteral) {
            val = literal.getValue3() + "S";
        } else {
            val = literal.getValue3();
        }
        type = HiveParser.IntegralLiteral;
        break;
    case INTEGER:
        val = literal.getValue3();
        type = HiveParser.IntegralLiteral;
        break;
    case BIGINT:
        if (useTypeQualInLiteral) {
            val = literal.getValue3() + "L";
        } else {
            val = literal.getValue3();
        }
        type = HiveParser.IntegralLiteral;
        break;
    case DOUBLE:
        val = literal.getValue3() + "D";
        type = HiveParser.NumberLiteral;
        break;
    case DECIMAL:
        val = literal.getValue3() + "BD";
        type = HiveParser.NumberLiteral;
        break;
    case FLOAT:
    case REAL:
        val = literal.getValue3();
        type = HiveParser.Number;
        break;
    case VARCHAR:
    case CHAR:
        val = literal.getValue3();
        String escapedVal = BaseSemanticAnalyzer.escapeSQLString(String.valueOf(val));
        type = HiveParser.StringLiteral;
        val = "'" + escapedVal + "'";
        break;
    case BOOLEAN:
        val = literal.getValue3();
        type = ((Boolean) val).booleanValue() ? HiveParser.KW_TRUE : HiveParser.KW_FALSE;
        break;
    case DATE: {
        //Calcite Calendar is always GMT, Hive atm uses JVM local
        final Calendar c = (Calendar) literal.getValue();
        final DateTime dt = new DateTime(c.getTimeInMillis(), DateTimeZone.forTimeZone(c.getTimeZone()));
        type = HiveParser.TOK_DATELITERAL;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        val = df.format(dt.toDateTime(DateTimeZone.getDefault()).toDate());
        val = "'" + val + "'";
    }
        break;
    case TIME:
    case TIMESTAMP: {
        //Calcite Calendar is always GMT, Hive atm uses JVM local
        final Calendar c = (Calendar) literal.getValue();
        final DateTime dt = new DateTime(c.getTimeInMillis(), DateTimeZone.forTimeZone(c.getTimeZone()));
        type = HiveParser.TOK_TIMESTAMPLITERAL;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        val = df.format(dt.toDateTime(DateTimeZone.getDefault()).toDate());
        val = "'" + val + "'";
    }
        break;
    case INTERVAL_YEAR:
    case INTERVAL_MONTH:
    case INTERVAL_YEAR_MONTH: {
        type = HiveParser.TOK_INTERVAL_YEAR_MONTH_LITERAL;
        BigDecimal monthsBd = (BigDecimal) literal.getValue();
        HiveIntervalYearMonth intervalYearMonth = new HiveIntervalYearMonth(monthsBd.intValue());
        val = "'" + intervalYearMonth.toString() + "'";
    }
        break;
    case INTERVAL_DAY:
    case INTERVAL_DAY_HOUR:
    case INTERVAL_DAY_MINUTE:
    case INTERVAL_DAY_SECOND:
    case INTERVAL_HOUR:
    case INTERVAL_HOUR_MINUTE:
    case INTERVAL_HOUR_SECOND:
    case INTERVAL_MINUTE:
    case INTERVAL_MINUTE_SECOND:
    case INTERVAL_SECOND: {
        type = HiveParser.TOK_INTERVAL_DAY_TIME_LITERAL;
        BigDecimal millisBd = (BigDecimal) literal.getValue();

        // Calcite literal is in millis, convert to seconds
        BigDecimal secsBd = millisBd.divide(BigDecimal.valueOf(1000));
        HiveIntervalDayTime intervalDayTime = new HiveIntervalDayTime(secsBd);
        val = "'" + intervalDayTime.toString() + "'";
    }
        break;
    case NULL:
        type = HiveParser.TOK_NULL;
        break;

    //binary type should not be seen.
    case BINARY:
    default:
        throw new RuntimeException("Unsupported Type: " + sqlType);
    }

    return (ASTNode) ParseDriver.adaptor.create(type, String.valueOf(val));
}

From source file:org.apache.hadoop.hive.ql.optimizer.calcite.translator.ExprNodeConverter.java

License:Apache License

@Override
public ExprNodeDesc visitLiteral(RexLiteral literal) {
    RelDataType lType = literal.getType();

    if (RexLiteral.value(literal) == null) {
        switch (literal.getType().getSqlTypeName()) {
        case BOOLEAN:
            return new ExprNodeConstantDesc(TypeInfoFactory.booleanTypeInfo, null);
        case TINYINT:
            return new ExprNodeConstantDesc(TypeInfoFactory.byteTypeInfo, null);
        case SMALLINT:
            return new ExprNodeConstantDesc(TypeInfoFactory.shortTypeInfo, null);
        case INTEGER:
            return new ExprNodeConstantDesc(TypeInfoFactory.intTypeInfo, null);
        case BIGINT:
            return new ExprNodeConstantDesc(TypeInfoFactory.longTypeInfo, null);
        case FLOAT:
        case REAL:
            return new ExprNodeConstantDesc(TypeInfoFactory.floatTypeInfo, null);
        case DOUBLE:
            return new ExprNodeConstantDesc(TypeInfoFactory.doubleTypeInfo, null);
        case DATE:
            return new ExprNodeConstantDesc(TypeInfoFactory.dateTypeInfo, null);
        case TIME:
        case TIMESTAMP:
            return new ExprNodeConstantDesc(TypeInfoFactory.timestampTypeInfo, null);
        case BINARY:
            return new ExprNodeConstantDesc(TypeInfoFactory.binaryTypeInfo, null);
        case DECIMAL:
            return new ExprNodeConstantDesc(
                    TypeInfoFactory.getDecimalTypeInfo(lType.getPrecision(), lType.getScale()), null);
        case VARCHAR:
        case CHAR:
            return new ExprNodeConstantDesc(TypeInfoFactory.stringTypeInfo, null);
        case INTERVAL_YEAR:
        case INTERVAL_MONTH:
        case INTERVAL_YEAR_MONTH:
            return new ExprNodeConstantDesc(TypeInfoFactory.intervalYearMonthTypeInfo, null);
        case INTERVAL_DAY:
        case INTERVAL_DAY_HOUR:
        case INTERVAL_DAY_MINUTE:
        case INTERVAL_DAY_SECOND:
        case INTERVAL_HOUR:
        case INTERVAL_HOUR_MINUTE:
        case INTERVAL_HOUR_SECOND:
        case INTERVAL_MINUTE:
        case INTERVAL_MINUTE_SECOND:
        case INTERVAL_SECOND:
            return new ExprNodeConstantDesc(TypeInfoFactory.intervalDayTimeTypeInfo, null);
        case OTHER:
        default:/* w  w  w. j  ava2 s  . c  om*/
            return new ExprNodeConstantDesc(TypeInfoFactory.voidTypeInfo, null);
        }
    } else {
        switch (literal.getType().getSqlTypeName()) {
        case BOOLEAN:
            return new ExprNodeConstantDesc(TypeInfoFactory.booleanTypeInfo,
                    Boolean.valueOf(RexLiteral.booleanValue(literal)));
        case TINYINT:
            return new ExprNodeConstantDesc(TypeInfoFactory.byteTypeInfo,
                    Byte.valueOf(((Number) literal.getValue3()).byteValue()));
        case SMALLINT:
            return new ExprNodeConstantDesc(TypeInfoFactory.shortTypeInfo,
                    Short.valueOf(((Number) literal.getValue3()).shortValue()));
        case INTEGER:
            return new ExprNodeConstantDesc(TypeInfoFactory.intTypeInfo,
                    Integer.valueOf(((Number) literal.getValue3()).intValue()));
        case BIGINT:
            return new ExprNodeConstantDesc(TypeInfoFactory.longTypeInfo,
                    Long.valueOf(((Number) literal.getValue3()).longValue()));
        case FLOAT:
        case REAL:
            return new ExprNodeConstantDesc(TypeInfoFactory.floatTypeInfo,
                    Float.valueOf(((Number) literal.getValue3()).floatValue()));
        case DOUBLE:
            return new ExprNodeConstantDesc(TypeInfoFactory.doubleTypeInfo,
                    Double.valueOf(((Number) literal.getValue3()).doubleValue()));
        case DATE: {
            final Calendar c = (Calendar) literal.getValue();
            return new ExprNodeConstantDesc(TypeInfoFactory.dateTypeInfo,
                    new java.sql.Date(c.getTimeInMillis()));
        }
        case TIME:
        case TIMESTAMP: {
            final Calendar c = (Calendar) literal.getValue();
            final DateTime dt = new DateTime(c.getTimeInMillis(), DateTimeZone.forTimeZone(c.getTimeZone()));
            return new ExprNodeConstantDesc(TypeInfoFactory.timestampTypeInfo, new Timestamp(dt.getMillis()));
        }
        case BINARY:
            return new ExprNodeConstantDesc(TypeInfoFactory.binaryTypeInfo, literal.getValue3());
        case DECIMAL:
            return new ExprNodeConstantDesc(
                    TypeInfoFactory.getDecimalTypeInfo(lType.getPrecision(), lType.getScale()),
                    HiveDecimal.create((BigDecimal) literal.getValue3()));
        case VARCHAR:
        case CHAR: {
            return new ExprNodeConstantDesc(TypeInfoFactory.stringTypeInfo, literal.getValue3());
        }
        case INTERVAL_YEAR:
        case INTERVAL_MONTH:
        case INTERVAL_YEAR_MONTH: {
            BigDecimal monthsBd = (BigDecimal) literal.getValue();
            return new ExprNodeConstantDesc(TypeInfoFactory.intervalYearMonthTypeInfo,
                    new HiveIntervalYearMonth(monthsBd.intValue()));
        }
        case INTERVAL_DAY:
        case INTERVAL_DAY_HOUR:
        case INTERVAL_DAY_MINUTE:
        case INTERVAL_DAY_SECOND:
        case INTERVAL_HOUR:
        case INTERVAL_HOUR_MINUTE:
        case INTERVAL_HOUR_SECOND:
        case INTERVAL_MINUTE:
        case INTERVAL_MINUTE_SECOND:
        case INTERVAL_SECOND: {
            BigDecimal millisBd = (BigDecimal) literal.getValue();
            // Calcite literal is in millis, we need to convert to seconds
            BigDecimal secsBd = millisBd.divide(BigDecimal.valueOf(1000));
            return new ExprNodeConstantDesc(TypeInfoFactory.intervalDayTimeTypeInfo,
                    new HiveIntervalDayTime(secsBd));
        }
        case OTHER:
        default:
            return new ExprNodeConstantDesc(TypeInfoFactory.voidTypeInfo, literal.getValue3());
        }
    }
}

From source file:org.apache.isis.applib.clock.Clock.java

License:Apache License

public static LocalDate getTimeAsLocalDate() {
    final DateTimeZone timeZone = DateTimeZone.forTimeZone(TimeZone.getDefault());
    return new LocalDate(getTime(), timeZone);
}

From source file:org.apache.isis.applib.value.DateTime.java

License:Apache License

public DateTime(final Date date, final TimeZone timeZone) {
    final DateTimeZone tz = DateTimeZone.forTimeZone(timeZone);
    this.dateTime = new org.joda.time.DateTime(date, tz);
}

From source file:org.apache.isis.schema.utils.jaxbadapters.JodaDateTimeXMLGregorianCalendarAdapter.java

License:Apache License

public static DateTime parse(final XMLGregorianCalendar xgc) {
    if (xgc == null)
        return null;

    final GregorianCalendar gc = xgc.toGregorianCalendar();
    final Date time = gc.getTime();
    final TimeZone timeZone = gc.getTimeZone();

    final DateTimeZone dateTimeZone = DateTimeZone.forTimeZone(timeZone);
    return new DateTime(time, dateTimeZone);
}

From source file:org.apache.sqoop.connector.jdbc.oracle.util.OracleQueries.java

License:Apache License

public static void setConnectionTimeZone(Connection connection, String timeZone) {
    String timeZoneStr = timeZone;
    if (StringUtils.isEmpty(timeZoneStr)) {
        timeZoneStr = "GMT";
    }/*from w ww.ja  va2 s. com*/
    TimeZone timeZoneObj = TimeZone.getTimeZone(timeZoneStr);
    try {
        Method methSession = oracleConnectionClass.getMethod("setSessionTimeZone", String.class);
        Method methDefault = oracleConnectionClass.getMethod("setDefaultTimeZone", TimeZone.class);
        methSession.invoke(connection, timeZoneObj.getID());
        methDefault.invoke(connection, timeZoneObj);
        TimeZone.setDefault(timeZoneObj);
        DateTimeZone.setDefault(DateTimeZone.forTimeZone(timeZoneObj));
        LOG.info("Session Time Zone set to " + timeZoneObj.getID());
    } catch (Exception e) {
        LOG.error("Error setting time zone: " + e.getMessage());
    }
}