Example usage for org.joda.time.format ISODateTimeFormat date

List of usage examples for org.joda.time.format ISODateTimeFormat date

Introduction

In this page you can find the example usage for org.joda.time.format ISODateTimeFormat date.

Prototype

public static DateTimeFormatter date() 

Source Link

Document

Returns a formatter for a full date as four digit year, two digit month of year, and two digit day of month (yyyy-MM-dd).

Usage

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

License:Apache License

private List<String> getFilteredPartitionNames(SemiTransactionalHiveMetastore metastore,
        SchemaTableName tableName, List<HiveColumnHandle> partitionKeys,
        TupleDomain<ColumnHandle> effectivePredicate) {
    checkArgument(effectivePredicate.getDomains().isPresent());

    List<String> filter = new ArrayList<>();
    for (HiveColumnHandle partitionKey : partitionKeys) {
        Domain domain = effectivePredicate.getDomains().get().get(partitionKey);
        if (domain != null && domain.isNullableSingleValue()) {
            Object value = domain.getNullableSingleValue();
            Type type = domain.getType();
            if (value == null) {
                filter.add(HivePartitionKey.HIVE_DEFAULT_DYNAMIC_PARTITION);
            } else if (type instanceof CharType) {
                Slice slice = (Slice) value;
                filter.add(padSpaces(slice, type).toStringUtf8());
            } else if (type instanceof VarcharType) {
                Slice slice = (Slice) value;
                filter.add(slice.toStringUtf8());
            }//from www .  j  a  v a  2  s.  c  om
            // Types above this have only a single possible representation for each value.
            // Types below this may have multiple representations for a single value.  For
            // example, a boolean column may represent the false value as "0", "false" or "False".
            // The metastore distinguishes between these representations, so we cannot prune partitions
            // unless we know that all partition values use the canonical Java representation.
            else if (!assumeCanonicalPartitionKeys) {
                filter.add(PARTITION_VALUE_WILDCARD);
            } else if (type instanceof DecimalType && !((DecimalType) type).isShort()) {
                Slice slice = (Slice) value;
                filter.add(Decimals.toString(slice, ((DecimalType) type).getScale()));
            } else if (type instanceof DecimalType && ((DecimalType) type).isShort()) {
                filter.add(Decimals.toString((long) value, ((DecimalType) type).getScale()));
            } else if (type instanceof DateType) {
                DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.date().withZoneUTC();
                filter.add(dateTimeFormatter.print(TimeUnit.DAYS.toMillis((long) value)));
            } else if (type instanceof TimestampType) {
                // we don't have time zone info, so just add a wildcard
                filter.add(PARTITION_VALUE_WILDCARD);
            } else if (type instanceof TinyintType || type instanceof SmallintType
                    || type instanceof IntegerType || type instanceof BigintType || type instanceof DoubleType
                    || type instanceof RealType || type instanceof BooleanType) {
                filter.add(value.toString());
            } else {
                throw new PrestoException(NOT_SUPPORTED,
                        format("Unsupported partition key type: %s", type.getDisplayName()));
            }
        } else {
            filter.add(PARTITION_VALUE_WILDCARD);
        }
    }

    // fetch the partition names
    return metastore.getPartitionNamesByParts(tableName.getSchemaName(), tableName.getTableName(), filter)
            .orElseThrow(() -> new TableNotFoundException(tableName));
}

From source file:jongo.JongoUtils.java

License:Open Source License

/**
 * Check if a string has the ISO date format. Uses the ISODateTimeFormat.date() from JodaTime
 * and returns a DateTime instance. The correct format is yyyy-MM-dd or yyyyMMdd
 * @param arg the string to check//w  w w .ja  v  a 2  s.co m
 * @return a DateTime instance if the string is in the correct ISO format.
 */
public static DateTime isDate(final String arg) {
    if (arg == null)
        return null;
    DateTime ret = null;
    DateTimeFormatter df;
    if (arg.contains("-")) {
        df = ISODateTimeFormat.date();
    } else {
        df = ISODateTimeFormat.basicDate();
    }

    try {
        ret = df.parseDateTime(arg);
    } catch (IllegalArgumentException e) {
        l.debug(arg + " is not a valid ISO date");
    }

    return ret;
}

From source file:jongo.JongoUtils.java

License:Open Source License

/**
 * Infers the java.sql.Types of the given String and returns the JDBC mappable Object corresponding to it.
 * The conversions are like this:/*from ww w.j  a  va2  s. c  om*/
 * String -> String
 * Numeric -> Integer
 * Date or Time -> Date
 * Decimal -> BigDecimal
 * ??? -> TimeStamp
 * @param val a String with the value to be mapped
 * @return a JDBC mappable object instance with the value
 */
public static Object parseValue(String val) {
    Object ret = null;
    if (!StringUtils.isWhitespace(val) && StringUtils.isNumeric(val)) {
        try {
            ret = Integer.valueOf(val);
        } catch (Exception e) {
            l.debug(e.getMessage());
        }
    } else {
        DateTime date = JongoUtils.isDateTime(val);
        if (date != null) {
            l.debug("Got a DateTime " + date.toString(ISODateTimeFormat.dateTime()));
            ret = new java.sql.Timestamp(date.getMillis());
        } else {
            date = JongoUtils.isDate(val);
            if (date != null) {
                l.debug("Got a Date " + date.toString(ISODateTimeFormat.date()));
                ret = new java.sql.Date(date.getMillis());
            } else {
                date = JongoUtils.isTime(val);
                if (date != null) {
                    l.debug("Got a Time " + date.toString(ISODateTimeFormat.time()));
                    ret = new java.sql.Time(date.getMillis());
                }
            }
        }

        if (ret == null && val != null) {
            l.debug("Not a datetime. Try someting else. ");
            try {
                ret = new BigDecimal(val);
            } catch (NumberFormatException e) {
                l.debug(e.getMessage());
                ret = val;
            }
        }
    }
    return ret;
}

From source file:nl.gridline.zieook.runners.OAIImport.java

License:Apache License

private void findOutGranularity(Harvester harvester) throws OAIException {
    /*// ww  w  . j ava  2  s. c o m
     * OAI only has two date types: YYYY-MM-DD or YYYY-MM-DDThh:mm:ssZ
     * (see GranularityType) but Joda && java have yyyy instead of YYYY so, we need a little bit of conversion here)
     */
    try {
        IdentifyType identifier = harvester.identify();
        if (identifier != null) {
            earliest = identifier.getEarliestDatestamp();

            if (dateformat != null) {
                dateTimeFormat = DateTimeFormat.forPattern(dateformat);
            } else {
                if (identifier.getGranularity() != null) {
                    String dtf = identifier.getGranularity().value();
                    // These date are not ISO8601 but still occur in the case we have:
                    if ("YYYY-MM-DD".equals(dtf)) {
                        dateTimeFormat = DateTimeFormat.forPattern("yyyy-MM-dd");
                        LOG.warn("granularity of <{}> is set to yyyy-MM-DD from '{}'", harvester.getBaseUrl(),
                                dtf);
                    } else {
                        dateTimeFormat = ISODateTimeFormat.date();
                        LOG.info("granularity of <{}> is set to 'YYYY-MM-DDThh:mm:ssZ' from '{}'",
                                harvester.getBaseUrl(), dtf);
                    }
                } else {
                    dateTimeFormat = ISODateTimeFormat.date();
                    LOG.info("granularity of <{}> is set to 'YYYY-MM-DDThh:mm:ssZ' from no granularity",
                            harvester.getBaseUrl());
                }
            }
        }
    } catch (IOException e) {
        LOG.error("failed to determine server granularity", e);
    } catch (JAXBException e) {
        LOG.error("failed to determine server granularity", e);
    }
}

From source file:nl.gridline.zieook.runners.OaiImportTool.java

License:Apache License

private Long parseDate(String date) {
    if (date == null) {
        return null;
    }/*from   w  ww.  j  a va  2 s  . co  m*/

    try {
        return DateTimeFormat.forPattern("yyyy-MM-dd").parseMillis(date);
    } catch (IllegalArgumentException e) {
        // failed...
        LOG.error("parsing string date into millis: Failed for 'yyyy-MM-dd' format: {}", date);
    }
    try {
        return ISODateTimeFormat.date().parseMillis(date);
    } catch (IllegalArgumentException e) {
        LOG.error("parsing string date into millis: Failed for 'ISODateTimeFormat' format: {}", date);
    }
    return null;
}

From source file:nl.mpcjanssen.simpletask.AddTask.java

License:Open Source License

private void insertDate(final int dateType) {
    Dialog d = Util.createDeferDialog(this, dateType, false, new Util.InputDialogListener() {
        @Override//from  www  .j av a  2s  . c  o  m
        public void onClick(String selected) {
            if (selected.equals("pick")) {
                /* Note on some Android versions the OnDateSetListener can fire twice
                 * https://code.google.com/p/android/issues/detail?id=34860
                 * With the current implementation which replaces the dates this is not an
                 * issue. The date is just replaced twice
                 */
                final DateTime today = new DateTime();
                DatePickerDialog dialog = new DatePickerDialog(AddTask.this,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                                month++;
                                DateTime date = ISODateTimeFormat.date()
                                        .parseDateTime(year + "-" + month + "-" + day);
                                insertDateAtSelection(dateType, date);
                            }
                        }, today.getYear(), today.getMonthOfYear() - 1, today.getDayOfMonth());

                dialog.show();
            } else {
                insertDateAtSelection(dateType, Util.addInterval(new DateTime(), selected));
            }
        }
    });
    d.show();
}

From source file:nl.mpcjanssen.simpletask.AddTask.java

License:Open Source License

private void insertDateAtSelection(int dateType, DateTime date) {
    DateTimeFormatter formatter = ISODateTimeFormat.date();
    replaceDate(dateType, formatter.print(date));
}

From source file:nl.mpcjanssen.simpletask.Simpletask.java

License:GNU General Public License

private void deferTasks(List<Task> tasks, final int dateType) {
    final List<Task> tasksToDefer = tasks;
    Dialog d = Util.createDeferDialog(this, dateType, true, new Util.InputDialogListener() {
        @Override//from   ww w. j  a va  2  s.c  o  m
        public void onClick(String selected) {
            if (selected != null && selected.equals("pick")) {
                final DateTime today = new DateTime();
                DatePickerDialog dialog = new DatePickerDialog(Simpletask.this,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                                month++;

                                DateTime date = ISODateTimeFormat.date()
                                        .parseDateTime(year + "-" + month + "-" + day);
                                deferTasks(date, tasksToDefer, dateType);

                            }
                        }, today.getYear(), today.getMonthOfYear() - 1, today.getDayOfMonth());

                dialog.show();
            } else {
                deferTasks(selected, tasksToDefer, dateType);
            }
        }
    });
    d.show();
}

From source file:nz.net.ultraq.jaxb.adapters.XMLLocalDateAdapter.java

License:Apache License

/**
 * Converts a Joda LocalDate to an XML/ISO8601 date/time string.
 * /*from  www. ja v a2  s .com*/
 * @param value
 * @return XML date/time string.
 */
@Override
public String marshal(LocalDate value) {
    return value == null ? null : ISODateTimeFormat.date().withOffsetParsed().print(value);
}

From source file:org.akaza.openclinica.logic.expressionTree.OpenClinicaBeanVariableNode.java

License:LGPL

private Object calculateVariable() {
    if (number.equals("_CURRENT_DATE")) {
        String ssTimeZone = getExpressionBeanService().getSSTimeZone();
        if (ssTimeZone == "" || ssTimeZone == null)
            ssTimeZone = TimeZone.getDefault().getID();

        DateTimeZone ssZone = DateTimeZone.forID(ssTimeZone);
        DateMidnight dm = new DateMidnight(ssZone);
        DateTimeFormatter fmt = ISODateTimeFormat.date();
        return fmt.print(dm);
    }//from   w w  w .j  a va 2s.  co  m
    return null;
}