List of usage examples for org.joda.time.format ISODateTimeFormat date
public static DateTimeFormatter date()
From source file:annis.gui.converter.DateTimeStringConverter.java
License:Apache License
@Override public String convertToPresentation(DateTime value, Class<? extends String> targetType, Locale locale) throws ConversionException { if (value == null) { return null; }/*from ww w. ja v a2 s . c o m*/ return ISODateTimeFormat.date().print(value); }
From source file:annis.security.User.java
License:Apache License
/** * Constructs a represention that is equal to the content of an ANNIS * user file.//from w w w. j a v a 2s . c o m * @return */ public Properties toProperties() { Properties props = new Properties(); if (passwordHash != null) { props.put("password", passwordHash); } if (groups != null && !groups.isEmpty()) { props.put("groups", Joiner.on(',').join(groups)); } if (permissions != null && !permissions.isEmpty()) { props.put("permissions", Joiner.on(',').join(permissions)); } if (expires != null) { props.put("expires", ISODateTimeFormat.date().print(expires)); } return props; }
From source file:com.cubeia.backoffice.operator.service.manager.OperatorManagerImpl.java
License:Open Source License
@Override public void create(Operator o) { if (operatorDAO.getOperator(o.getId()) != null) { throw new RuntimeException("Operator id [" + o.getId() + "] already exist. Name [" + o.getName() + "]"); }//w w w .j a v a 2s. c o m operatorDefault.addDefaultConfiguration(o.getConfig()); o.getConfig().put(OperatorConfigParameter.API_KEY, UUID.randomUUID().toString()); o.getConfig().put(OperatorConfigParameter.REGISTRATION_TIMESTAMP, ISODateTimeFormat.date().print(System.currentTimeMillis())); operatorDAO.save(o); }
From source file:com.facebook.presto.hive.DwrfHiveRecordCursor.java
License:Apache License
public DwrfHiveRecordCursor(RecordReader recordReader, long totalBytes, Properties splitSchema, List<HivePartitionKey> partitionKeys, List<HiveColumnHandle> columns, DateTimeZone hiveStorageTimeZone, DateTimeZone sessionTimeZone, TypeManager typeManager) { checkNotNull(recordReader, "recordReader is null"); checkArgument(totalBytes >= 0, "totalBytes is negative"); checkNotNull(splitSchema, "splitSchema is null"); checkNotNull(partitionKeys, "partitionKeys is null"); checkNotNull(columns, "columns is null"); checkNotNull(hiveStorageTimeZone, "hiveStorageTimeZone is null"); checkNotNull(sessionTimeZone, "sessionTimeZone is null"); this.recordReader = recordReader; this.totalBytes = totalBytes; this.sessionTimeZone = sessionTimeZone; int size = columns.size(); this.names = new String[size]; this.types = new Type[size]; this.hiveTypes = new HiveType[size]; this.fieldInspectors = new ObjectInspector[size]; this.hiveColumnIndexes = new int[size]; this.isPartitionColumn = new boolean[size]; this.loaded = new boolean[size]; this.booleans = new boolean[size]; this.longs = new long[size]; this.doubles = new double[size]; this.slices = new Slice[size]; this.nulls = new boolean[size]; // DWRF uses an epoch sensitive to the JVM default timezone, so we need to correct for this long hiveStorageCorrection = new DateTime(2015, 1, 1, 0, 0, hiveStorageTimeZone).getMillis() - new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC).getMillis(); long jvmCorrection = new DateTime(2015, 1, 1, 0, 0).getMillis() - new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC).getMillis(); timeZoneCorrection = hiveStorageCorrection - jvmCorrection; // initialize data columns StructObjectInspector rowInspector = getTableObjectInspector(splitSchema); for (int i = 0; i < columns.size(); i++) { HiveColumnHandle column = columns.get(i); names[i] = column.getName();/*from www. j av a 2 s.co m*/ types[i] = typeManager.getType(column.getTypeName()); hiveTypes[i] = column.getHiveType(); if (!column.isPartitionKey()) { fieldInspectors[i] = rowInspector.getStructFieldRef(column.getName()).getFieldObjectInspector(); } hiveColumnIndexes[i] = column.getHiveColumnIndex(); isPartitionColumn[i] = column.isPartitionKey(); } // parse requested partition columns Map<String, HivePartitionKey> partitionKeysByName = uniqueIndex(partitionKeys, HivePartitionKey.nameGetter()); for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) { HiveColumnHandle column = columns.get(columnIndex); if (column.isPartitionKey()) { HivePartitionKey partitionKey = partitionKeysByName.get(column.getName()); checkArgument(partitionKey != null, "Unknown partition key %s", column.getName()); byte[] bytes = partitionKey.getValue().getBytes(Charsets.UTF_8); Type type = types[columnIndex]; if (HiveUtil.isHiveNull(bytes)) { nulls[columnIndex] = true; } else if (BOOLEAN.equals(type)) { if (isTrue(bytes, 0, bytes.length)) { booleans[columnIndex] = true; } else if (isFalse(bytes, 0, bytes.length)) { booleans[columnIndex] = false; } else { String valueString = new String(bytes, Charsets.UTF_8); throw new IllegalArgumentException( String.format("Invalid partition value '%s' for BOOLEAN partition key %s", valueString, names[columnIndex])); } } else if (BIGINT.equals(type)) { if (bytes.length == 0) { throw new IllegalArgumentException(String.format( "Invalid partition value '' for BIGINT partition key %s", names[columnIndex])); } longs[columnIndex] = parseLong(bytes, 0, bytes.length); } else if (DOUBLE.equals(type)) { if (bytes.length == 0) { throw new IllegalArgumentException(String.format( "Invalid partition value '' for DOUBLE partition key %s", names[columnIndex])); } doubles[columnIndex] = parseDouble(bytes, 0, bytes.length); } else if (VARCHAR.equals(type)) { slices[columnIndex] = Slices.wrappedBuffer(Arrays.copyOf(bytes, bytes.length)); } else if (DATE.equals(type)) { longs[columnIndex] = ISODateTimeFormat.date().withZone(DateTimeZone.UTC) .parseMillis(partitionKey.getValue()); } else if (TIMESTAMP.equals(type)) { longs[columnIndex] = parseHiveTimestamp(partitionKey.getValue(), hiveStorageTimeZone); } else { throw new UnsupportedOperationException("Unsupported column type: " + type); } } } }
From source file:com.facebook.presto.hive.OrcHiveRecordCursor.java
License:Apache License
public OrcHiveRecordCursor(RecordReader recordReader, long totalBytes, Properties splitSchema, List<HivePartitionKey> partitionKeys, List<HiveColumnHandle> columns, DateTimeZone hiveStorageTimeZone, DateTimeZone sessionTimeZone, TypeManager typeManager) { checkNotNull(recordReader, "recordReader is null"); checkArgument(totalBytes >= 0, "totalBytes is negative"); checkNotNull(splitSchema, "splitSchema is null"); checkNotNull(partitionKeys, "partitionKeys is null"); checkNotNull(columns, "columns is null"); checkNotNull(hiveStorageTimeZone, "hiveStorageTimeZone is null"); checkNotNull(sessionTimeZone, "sessionTimeZone is null"); this.recordReader = recordReader; this.totalBytes = totalBytes; this.sessionTimeZone = sessionTimeZone; int size = columns.size(); this.names = new String[size]; this.types = new Type[size]; this.hiveTypes = new HiveType[size]; this.fieldInspectors = new ObjectInspector[size]; this.hiveColumnIndexes = new int[size]; this.isPartitionColumn = new boolean[size]; this.loaded = new boolean[size]; this.booleans = new boolean[size]; this.longs = new long[size]; this.doubles = new double[size]; this.slices = new Slice[size]; this.nulls = new boolean[size]; // ORC stores timestamps relative to 2015-01-01 00:00:00 but in the timezone of the writer // When reading back a timestamp the Hive ORC reader will an epoch in this machine's timezone // We must correct for the difference between the writer's timezone and this machine's // timezone (on 2015-01-01) long hiveStorageCorrection = new DateTime(2015, 1, 1, 0, 0, hiveStorageTimeZone).getMillis() - new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC).getMillis(); long jvmCorrection = new DateTime(2015, 1, 1, 0, 0).getMillis() - new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC).getMillis(); timeZoneCorrection = hiveStorageCorrection - jvmCorrection; // initialize data columns StructObjectInspector rowInspector = getTableObjectInspector(splitSchema); for (int i = 0; i < columns.size(); i++) { HiveColumnHandle column = columns.get(i); names[i] = column.getName();/*w ww . j a va2 s. c o m*/ types[i] = typeManager.getType(column.getTypeName()); hiveTypes[i] = column.getHiveType(); if (!column.isPartitionKey()) { fieldInspectors[i] = rowInspector.getStructFieldRef(column.getName()).getFieldObjectInspector(); } hiveColumnIndexes[i] = column.getHiveColumnIndex(); isPartitionColumn[i] = column.isPartitionKey(); } // parse requested partition columns Map<String, HivePartitionKey> partitionKeysByName = uniqueIndex(partitionKeys, HivePartitionKey.nameGetter()); for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) { HiveColumnHandle column = columns.get(columnIndex); if (column.isPartitionKey()) { HivePartitionKey partitionKey = partitionKeysByName.get(column.getName()); checkArgument(partitionKey != null, "Unknown partition key %s", column.getName()); byte[] bytes = partitionKey.getValue().getBytes(Charsets.UTF_8); if (HiveUtil.isHiveNull(bytes)) { nulls[columnIndex] = true; } else if (types[columnIndex].equals(BOOLEAN)) { if (isTrue(bytes, 0, bytes.length)) { booleans[columnIndex] = true; } else if (isFalse(bytes, 0, bytes.length)) { booleans[columnIndex] = false; } else { String valueString = new String(bytes, Charsets.UTF_8); throw new IllegalArgumentException( String.format("Invalid partition value '%s' for BOOLEAN partition key %s", valueString, names[columnIndex])); } } else if (types[columnIndex].equals(BIGINT)) { if (bytes.length == 0) { throw new IllegalArgumentException(String.format( "Invalid partition value '' for BIGINT partition key %s", names[columnIndex])); } longs[columnIndex] = parseLong(bytes, 0, bytes.length); } else if (types[columnIndex].equals(DOUBLE)) { if (bytes.length == 0) { throw new IllegalArgumentException(String.format( "Invalid partition value '' for DOUBLE partition key %s", names[columnIndex])); } doubles[columnIndex] = parseDouble(bytes, 0, bytes.length); } else if (types[columnIndex].equals(VARCHAR)) { slices[columnIndex] = Slices.wrappedBuffer(bytes); } else if (types[columnIndex].equals(DATE)) { longs[columnIndex] = ISODateTimeFormat.date().withZone(DateTimeZone.UTC) .parseMillis(partitionKey.getValue()); } else if (types[columnIndex].equals(TIMESTAMP)) { longs[columnIndex] = parseHiveTimestamp(partitionKey.getValue(), hiveStorageTimeZone); } else { throw new UnsupportedOperationException("Unsupported column type: " + types[columnIndex]); } } } }
From source file:com.helger.masterdata.postal.PostalCodeListReader.java
License:Apache License
public void readFromFile(@Nonnull final IReadableResource aRes) { ValueEnforcer.notNull(aRes, "Resource"); final IMicroDocument aDoc = MicroReader.readMicroXML(aRes); if (aDoc == null) throw new IllegalArgumentException("Passed resource is not an XML file: " + aRes); final IMicroElement eBody = aDoc.getDocumentElement().getFirstChildElement(ELEMENT_BODY); if (eBody == null) throw new IllegalArgumentException("Missing body element in file " + aRes); final LocalDate aNow = PDTFactory.getCurrentLocalDate(); // Read all countries for (final IMicroElement eCountry : eBody.getAllChildElements(ELEMENT_COUNTRY)) { final String sCountryName = eCountry.getAttributeValue(ATTR_NAME); final String sISO = eCountry.getAttributeValue(ATTR_ISO); final PostalCodeCountry aCountry = new PostalCodeCountry(sISO); // Read all postal code definitions for (final IMicroElement ePostalCode : eCountry.getAllChildElements(ELEMENT_POSTALCODES)) { final String sValidFrom = ePostalCode.getAttributeValue(ATTR_VALIDFROM); final LocalDate aValidFrom = sValidFrom == null ? null : ISODateTimeFormat.date().parseLocalDate(sValidFrom); final String sValidTo = ePostalCode.getAttributeValue(ATTR_VALIDTO); final LocalDate aValidTo = sValidTo == null ? null : ISODateTimeFormat.date().parseLocalDate(sValidTo); if (aValidFrom != null && aValidFrom.isAfter(aNow)) { MasterdataLogger.getInstance().info("Ignoring some postal code definitions of " + sCountryName + " because they are valid from " + aValidFrom.toString()); continue; }/*ww w.j a v a 2s. c o m*/ if (aValidTo != null && aValidTo.isBefore(aNow)) { MasterdataLogger.getInstance().info("Ignoring some postal code definitions of " + sCountryName + " because they are valid until " + aValidTo.toString()); continue; } // Read all formats for (final IMicroElement eFormat : ePostalCode.getAllChildElements(ELEMENT_FORMAT)) { final String sFormat = eFormat.getTextContent(); if (StringHelper.hasNoText(sFormat)) throw new IllegalArgumentException( "The country " + sISO + " contains an empty postal code format!"); // Parse into tokens final List<EPostalCodeFormatElement> aElements = _parseFormat(sFormat); if (aElements.isEmpty()) throw new IllegalStateException( "The country " + sISO + " contains an invalid format '" + sFormat + "'"); aCountry.addFormat(new PostalCodeFormat(sISO, aElements)); } // Is exactly one code present? for (final IMicroElement eOneCode : ePostalCode.getAllChildElements(ELEMENT_SPECIFIC)) aCountry.addSpecificPostalCode(eOneCode.getTextContent()); // Is a note present final IMicroElement eNote = ePostalCode.getFirstChildElement(ELEMENT_NOTE); if (eNote != null) aCountry.setNote(eNote.getTextContent()); } if (aCountry.getFormatCount() == 0 && aCountry.getSpecificPostalCodeCount() == 0) throw new IllegalStateException("Country " + sISO + " has no formats defined!"); m_aMgr.addCountry(aCountry); } }
From source file:com.helger.masterdata.tools.MainReadPostalCodeListExcel.java
License:Apache License
public static void main(final String[] args) throws Exception { final String sSource = "http://en.wikipedia.org/wiki/List_of_postal_codes"; final String sRevision = "20130209"; final File f = new File("src/test/resources/" + sRevision + "PostalCodes.xls"); final Workbook aWB = new HSSFWorkbook(FileUtils.getInputStream(f)); final Sheet aSheet = aWB.getSheetAt(0); final Iterator<Row> it = aSheet.rowIterator(); // Skip 1 row it.next();/* ww w . j ava 2 s .c om*/ final IMicroDocument aDoc = new MicroDocument(); final IMicroElement eRoot = aDoc.appendElement(PostalCodeListReader.ELEMENT_ROOT); final IMicroElement eHeader = eRoot.appendElement(PostalCodeListReader.ELEMENT_HEADER); eHeader.appendElement(PostalCodeListReader.ELEMENT_SOURCE).appendText(sSource); eHeader.appendElement(PostalCodeListReader.ELEMENT_REVISION).appendText(sRevision); final IMicroElement eBody = eRoot.appendElement(PostalCodeListReader.ELEMENT_BODY); final List<Item> aItems = new ArrayList<Item>(); int nRow = 0; while (it.hasNext()) { final Row aRow = it.next(); ++nRow; final String sCountry = ExcelReadUtils.getCellValueString(aRow.getCell(0)); if (StringHelper.hasNoText(sCountry)) { s_aLogger.warn("Line " + nRow + ": No country name present"); continue; } final Cell aDateCell = aRow.getCell(1); Date aIntroducedDate = null; if (aDateCell != null && aDateCell.getCellType() != Cell.CELL_TYPE_BLANK) { final Number aNum = ExcelReadUtils.getCellValueNumber(aDateCell); final int nYear = aNum.intValue(); if (nYear > 1800 && nYear < 3000) aIntroducedDate = PDTFactory.createLocalDate(nYear, DateTimeConstants.JANUARY, 1).toDate(); else aIntroducedDate = ExcelReadUtils.getCellValueJavaDate(aDateCell); } final String sISO = ExcelReadUtils.getCellValueString(aRow.getCell(2)); if (StringHelper.hasNoText(sISO)) { s_aLogger.warn("Line " + nRow + ": No ISO code for " + sCountry); continue; } final String sFormat = ExcelReadUtils.getCellValueString(aRow.getCell(3)); if (NO_CODES.equals(sFormat) || StringHelper.hasNoText(sFormat)) continue; final List<String> aFormats = StringHelper.getExploded("\n", sFormat); final String sNote = ExcelReadUtils.getCellValueString(aRow.getCell(4)); aItems.add(new Item(sCountry, aIntroducedDate, sISO, aFormats, sNote)); } // Convert to map, where the key is the ISO final IMultiMapListBased<String, Item> aMap = new MultiHashMapArrayListBased<String, Item>(); for (final Item aItem : aItems) aMap.putSingle(aItem.getISO(), aItem); // Sort all sub-lists by introduction date for (final List<Item> aSubList : aMap.values()) { ContainerHelper.getSortedInline(aSubList, new ComparatorItemValidFrom()); for (int i = 1; i < aSubList.size(); ++i) { final Item aPrevItem = aSubList.get(i - 1); final Item aThisItem = aSubList.get(i); if (aThisItem.getValidFrom() != null) aPrevItem.setValidTo(aThisItem.getValidFrom().minusDays(1)); } } // Print sorted by ISO code for (final Map.Entry<String, List<Item>> aEntry : ContainerHelper.getSortedByKey(aMap).entrySet()) { IMicroElement eCountry = null; for (final Item aItem : aEntry.getValue()) { if (eCountry == null) { // First item - ISO and name only once eCountry = eBody.appendElement(PostalCodeListReader.ELEMENT_COUNTRY); eCountry.setAttribute(PostalCodeListReader.ATTR_ISO, aItem.getISO()); eCountry.setAttribute(PostalCodeListReader.ATTR_NAME, aItem.getCountry()); } final IMicroElement ePostalCodes = eCountry.appendElement(PostalCodeListReader.ELEMENT_POSTALCODES); if (aItem.getValidFrom() != null) ePostalCodes.setAttribute(PostalCodeListReader.ATTR_VALIDFROM, ISODateTimeFormat.date().print(aItem.getValidFrom())); if (aItem.getValidTo() != null) ePostalCodes.setAttribute(PostalCodeListReader.ATTR_VALIDTO, ISODateTimeFormat.date().print(aItem.getValidTo())); for (final String sSingleFormat : aItem.getFormats()) if (sSingleFormat.startsWith(PREFIX_ONE_CODE)) ePostalCodes.appendElement(PostalCodeListReader.ELEMENT_SPECIFIC) .appendText(sSingleFormat.substring(PREFIX_ONE_CODE.length())); else { ePostalCodes.appendElement(PostalCodeListReader.ELEMENT_FORMAT).appendText(sSingleFormat); } if (StringHelper.hasText(aItem.getNote())) ePostalCodes.appendElement(PostalCodeListReader.ELEMENT_NOTE).appendText(aItem.getNote()); } } MicroWriter.writeToStream(aDoc, FileUtils.getOutputStream("src/main/resources/codelists/postal-codes-" + sRevision + ".xml")); s_aLogger.info("Done"); }
From source file:com.liteoc.logic.expressionTree.ArithmeticOpNode.java
License:LGPL
private String calculateGenericDate(String value1, String value2) { DateMidnight dm = new DateMidnight(ExpressionTreeHelper.getDate(value1).getTime()); DateTimeFormatter fmt = ISODateTimeFormat.date(); switch (op) { case PLUS: {/*from w w w .ja v a 2 s . c o m*/ dm = dm.plusDays(Double.valueOf(value2).intValue()); return fmt.print(dm); } case MINUS: { dm = dm.minusDays(Double.valueOf(value2).intValue()); return fmt.print(dm); } default: return null; // Bad operator! } }
From source file:com.liteoc.logic.expressionTree.OpenClinicaVariableNode.java
License:LGPL
private String calculateVariable() { if (number.equals("_CURRENT_DATE")) { DateMidnight dm = new DateMidnight(); DateTimeFormatter fmt = ISODateTimeFormat.date(); return fmt.print(dm); }//from ww w . j a va2s.c o m return null; }
From source file:com.liteoc.logic.expressionTree.OpenClinicaVariableNode.java
License:LGPL
private String testCalculateVariable() { if (number.equals("_CURRENT_DATE")) { DateMidnight dm = new DateMidnight(); DateTimeFormatter fmt = ISODateTimeFormat.date(); return fmt.print(dm); }/*from w w w. ja v a 2 s.co m*/ return null; }