List of usage examples for org.joda.time.format DateTimeFormatter parseDateTime
public DateTime parseDateTime(String text)
From source file:eu.eidas.auth.commons.DateUtil.java
License:Open Source License
/** * Fulfils dateValue with a valid date. The following rules are applied: * a) If the dateValue only contains the year then fulfils with last year's day. * e.g. this method returns 19951231 to the 1995 dateValue. * b) If the dateValue contains the year and the month then fulfils with last month's day. * e.g. this method returns 19950630 to the 199505 dateValue. * * @param dateValue The date to be fulfilled. * * @return The dateValue fulfilled./*from w ww . j a v a2 s . com*/ */ @Nonnull private static String fulfilDate(@Nonnull String dateValue) { Preconditions.checkNotLonger(dateValue, "dateValue", FULL_DATE_SIZE); final StringBuilder strBuf = new StringBuilder(FULL_DATE_SIZE); strBuf.append(dateValue); // if the IdP just provides the year then we must fulfil the date. if (dateValue.length() == YEAR_DATE_SIZE) { strBuf.append(EIDASValues.LAST_MONTH.toString()); } // if the IdP provides the year and the month then we must fulfil the // date. if (dateValue.length() == MONTH_DATE_SIZE || strBuf.length() == MONTH_DATE_SIZE) { // IdP doesn't provide the day, so we will use DateTime to // calculate it. final String noDayCons = EIDASValues.NO_DAY_DATE_FORMAT.toString(); final DateTimeFormatter fmt = DateTimeFormat.forPattern(noDayCons); final DateTime dateTime = fmt.parseDateTime(strBuf.toString()); // Append the last month's day. strBuf.append(dateTime.dayOfMonth().withMaximumValue().getDayOfMonth()); } return strBuf.toString(); }
From source file:eu.eidas.auth.commons.DateUtil.java
License:Open Source License
/** * Validates the dateValue format: a) if has a valid size; b) if has a numeric * value; Note: dateValue must have the format yyyyMMdd. * * @param dateValueTmp The date to be validated. * @param pattern The accepted date format. * * @return true if the date has a valid format. *//*from w w w . ja va 2s. co m*/ public static boolean isValidFormatDate(final String dateValueTmp, final String pattern) { boolean retVal = true; try { final String dateValue = fulfilDate(dateValueTmp); final DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern); fmt.parseDateTime(dateValue); } catch (final Exception e) { // We catch Exception because we only have to return false // value! LOG.info("BUSINESS EXCEPTION : error validating date {}", e); retVal = false; } return retVal; }
From source file:eu.eidas.auth.commons.DateUtil.java
License:Open Source License
/** * Calculates the age for a given date string. * * @param dateVal The date to be validated. * @param now The current date.//from ww w . java 2 s . c om * @param pattern The date pattern. * * @return The age value. */ public static int calculateAge(final String dateVal, final DateTime now, final String pattern) { if (isValidFormatDate(dateVal, pattern)) { try { final String dateValueTemp = fulfilDate(dateVal); final DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern); final DateTime dateTime = fmt.parseDateTime(dateValueTemp); // Calculating age final Years age = Years.yearsBetween(dateTime, now); return age.getYears(); } catch (final IllegalArgumentException e) { LOG.info("BUSINESS EXCEPTION : Invalid date format (" + pattern + ") or an invalid dateValue."); throw new SecurityEIDASException(EidasErrors.get(EidasErrorKey.INVALID_ATTRIBUTE_VALUE.errorCode()), EidasErrors.get(EidasErrorKey.INVALID_ATTRIBUTE_VALUE.errorMessage()), e); } } else { LOG.info("BUSINESS EXCEPTION : Couldn't calculate Age, invalid date!"); throw new SecurityEIDASException(EidasErrors.get(EidasErrorKey.INVALID_ATTRIBUTE_VALUE.errorCode()), EidasErrors.get(EidasErrorKey.INVALID_ATTRIBUTE_VALUE.errorMessage())); } }
From source file:eu.eidas.auth.engine.core.validator.eidas.EidasAttributeValidator.java
License:EUPL
private static void verifyDate(String nodeDate) throws ValidationException { DateTimeFormatter fmt; fmt = DateTimeFormat.forPattern("yyyy-MM-dd"); try {//from ww w . jav a 2 s .co m fmt.parseDateTime(nodeDate); } catch (IllegalArgumentException e) { throw new ValidationException("Date has wrong format {}", e); } }
From source file:eu.hydrologis.jgrass.geonotes.util.ExifHandler.java
License:Open Source License
/** * Extract the creation {@link DateTime} from the map of tags read by {@link #readMetaData(File)}. * // w w w . ja v a2s . co m * @param tags2ValuesMap the map of tags read by {@link #readMetaData(File)}. * @return the datetime. */ public static DateTime getCreationDatetimeUtc(HashMap<String, String> tags2ValuesMap) { String creationDate = tags2ValuesMap.get(TiffConstants.EXIF_TAG_CREATE_DATE.name); creationDate = creationDate.replaceAll("'", ""); creationDate = creationDate.replaceFirst(":", "-"); creationDate = creationDate.replaceFirst(":", "-"); String dateTimeFormatterYYYYMMDDHHMMSS_string = "yyyy-MM-dd HH:mm:ss"; DateTimeFormatter dateTimeFormatterYYYYMMDDHHMMSS = DateTimeFormat .forPattern(dateTimeFormatterYYYYMMDDHHMMSS_string); DateTime parseDateTime = dateTimeFormatterYYYYMMDDHHMMSS.parseDateTime(creationDate); DateTime dt = parseDateTime.toDateTime(DateTimeZone.UTC); return dt; }
From source file:eu.hydrologis.jgrass.gpsnmea.geopaparazzi.ImportGeopaparazziFolderWizard.java
License:Open Source License
private void mediaToGeonotes(File geopapFolderFile, IProgressMonitor pm) throws Exception { File folder = new File(geopapFolderFile, "media"); if (!folder.exists()) { // try to see if it is an old version of geopaparazzi folder = new File(geopapFolderFile, "pictures"); if (!folder.exists()) { // ignoring non existing things return; }//ww w . j a v a2s. c o m } File[] listFiles = folder.listFiles(); List<String> nonTakenFilesList = new ArrayList<String>(); DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyyMMddHHmmss"); //$NON-NLS-1$ pm.beginTask("Importing media...", listFiles.length); Session session = null; Transaction transaction = null; try { session = DatabasePlugin.getDefault().getActiveDatabaseConnection().openSession(); transaction = session.beginTransaction(); for (File file : listFiles) { String name = file.getName(); if (name.endsWith("jpg") || file.getName().endsWith("JPG") || file.getName().endsWith("png") || file.getName().endsWith("PNG") || file.getName().endsWith("3gp")) { String[] nameSplit = name.split("[_\\|.]"); //$NON-NLS-1$ String dateString = nameSplit[1]; String timeString = nameSplit[2]; DateTime dateTime = formatter.parseDateTime(dateString + timeString); Properties locationProperties = new Properties(); String mediaPath = file.getAbsolutePath(); int lastDot = mediaPath.lastIndexOf("."); //$NON-NLS-1$ String nameNoExt = mediaPath.substring(0, lastDot); String infoPath = nameNoExt + ".properties"; //$NON-NLS-1$ File infoFile = new File(infoPath); if (!infoFile.exists()) { nonTakenFilesList.add(mediaPath); continue; } locationProperties.load(new FileInputStream(infoFile)); String azimuthString = locationProperties.getProperty("azimuth"); //$NON-NLS-1$ String latString = locationProperties.getProperty("latitude"); //$NON-NLS-1$ String lonString = locationProperties.getProperty("longitude"); //$NON-NLS-1$ String altimString = locationProperties.getProperty("altim"); //$NON-NLS-1$ Double azimuth = null; if (azimuthString != null) azimuth = Double.parseDouble(azimuthString); double lat = 0.0; double lon = 0.0; if (latString.contains("/")) { // this is an exif string lat = exifFormat2degreeDecimal(latString); lon = exifFormat2degreeDecimal(lonString); } else { lat = Double.parseDouble(latString); lon = Double.parseDouble(lonString); } double altim = Double.parseDouble(altimString); // save those points also in the database log GpsLogTable gpsLog = new GpsLogTable(); gpsLog.setUtcTime(dateTime); gpsLog.setEast(lon); gpsLog.setNorth(lat); gpsLog.setAltimetry(altim); gpsLog.setNumberOfTrackedSatellites(-1); gpsLog.setHorizontalDilutionOfPosition(-1); session.save(gpsLog); // and then create the photo geonote GeonotesHandler geonotesHandler = new GeonotesHandler(lon, lat, name, "Imported from Geopaparazzi", PHOTO, dateTime, azimuth, null, null, null); geonotesHandler.addMedia(file, file.getName()); FieldbookView fieldBookView = GeonotesPlugin.getDefault().getFieldbookView(); if (fieldBookView != null) { geonotesHandler.addObserver(fieldBookView); } geonotesHandler.notifyObservers(NOTIFICATION.NOTEADDED); } pm.worked(1); } } finally { pm.done(); if (transaction != null) transaction.commit(); if (session != null) session.close(); } if (nonTakenFilesList.size() > 0) { final StringBuilder sB = new StringBuilder(); sB.append("For the following images no *.properties file could be found:\n"); for (String p : nonTakenFilesList) { sB.append(p).append("\n"); } Display.getDefault().asyncExec(new Runnable() { public void run() { Shell shell = PlatformUI.getWorkbench().getDisplay().getActiveShell(); MessageDialog.openWarning(shell, "Warning", sB.toString()); } }); } else { Display.getDefault().asyncExec(new Runnable() { public void run() { Shell shell = PlatformUI.getWorkbench().getDisplay().getActiveShell(); MessageDialog.openInformation(shell, "Info", "All photos were successfully imported."); } }); } }
From source file:eu.stork.peps.auth.commons.DateUtil.java
License:Open Source License
/** * Fulfils dateValue with a valid date. The following roles are applied: a) If * the dateValue only contains the year then fulfils with last year's day. * e.g. this method returns 19951231 to the 1995 dateValue. b) If the * dateValue contains the year and the month then fulfils with last month's * day. e.g. this method returns 19950630 to the 199505 dateValue. * /*from w w w.j ava 2 s .c om*/ * @param dateValue The date to be fulfilled. * * @return The dateValue fulfilled. */ private static String fulfilDate(final String dateValue) { final StringBuffer strBuf = new StringBuffer(); strBuf.append(dateValue); // if the IdP just provides the year then we must fullfil the date. if (dateValue.length() == YEAR_DATE_SIZE) { strBuf.append(PEPSValues.LAST_MONTH.toString()); } // if the IdP provides the year and the month then we must fullfil the // date. if (dateValue.length() == MONTH_DATE_SIZE || strBuf.length() == MONTH_DATE_SIZE) { // IdP doesn't provide the day, so we will use DateTime to // calculate it. final String noDayCons = PEPSValues.NO_DAY_DATE_FORMAT.toString(); final DateTimeFormatter fmt = DateTimeFormat.forPattern(noDayCons); final DateTime dateTime = fmt.parseDateTime(strBuf.toString()); // Append the last month's day. strBuf.append(dateTime.dayOfMonth().withMaximumValue().getDayOfMonth()); } return strBuf.toString(); }
From source file:eu.stork.peps.auth.commons.DateUtil.java
License:Open Source License
/** * Validates the dateValue format: a) if has a valid size; b) if has a numeric * value; Note: dateValue must have the format yyyyMMdd. * //from w ww. j a va 2s. c o m * @param dateValueTmp The date to be validated. * @param pattern The accepted date format. * * @return true if the date has a valid format. */ public static boolean isValidFormatDate(final String dateValueTmp, final String pattern) { boolean retVal = true; try { final String dateValue = DateUtil.fulfilDate(dateValueTmp); final DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern); fmt.parseDateTime(dateValue); } catch (final Exception e) { // We catch Exception because we only have to return false // value! retVal = false; } return retVal; }
From source file:eu.stork.peps.auth.commons.DateUtil.java
License:Open Source License
/** * Calculates the age for a given date string. * /* w w w . j a v a 2s .co m*/ * @param dateVal The date to be validated. * @param now The current date. * @param pattern The date pattern. * * @return The age value. */ public static int calculateAge(final String dateVal, final DateTime now, final String pattern) { if (DateUtil.isValidFormatDate(dateVal, pattern)) { try { final String dateValueTemp = DateUtil.fulfilDate(dateVal); final DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern); final DateTime dateTime = fmt.parseDateTime(dateValueTemp); // Calculating age final Years age = Years.yearsBetween(dateTime, now); return age.getYears(); } catch (final IllegalArgumentException e) { LOG.warn("Invalid date format (" + pattern + ") or an invalid dateValue."); throw new SecurityPEPSException(PEPSUtil.getConfig(PEPSErrors.INVALID_ATTRIBUTE_VALUE.errorCode()), PEPSUtil.getConfig(PEPSErrors.INVALID_ATTRIBUTE_VALUE.errorMessage()), e); } } else { LOG.warn("Couldn't calculate Age, invalid date!"); throw new SecurityPEPSException(PEPSUtil.getConfig(PEPSErrors.INVALID_ATTRIBUTE_VALUE.errorCode()), PEPSUtil.getConfig(PEPSErrors.INVALID_ATTRIBUTE_VALUE.errorMessage())); } }
From source file:fr.acxio.tools.agia.cmis.CmisWriter.java
License:Apache License
private Object scanValue(String sValue) { Object aResult = sValue;//w w w . jav a2s. co m if (sValue.startsWith("%")) { String aFormatString = sValue.substring(1, sValue.indexOf("%", 1)); String aValue = sValue.substring(2 + aFormatString.length()); if ("B".equals(aFormatString)) { aResult = Boolean.valueOf(aValue); } else if ("D".equals(aFormatString)) { aResult = Long.valueOf(aValue); } else if ("F".equals(aFormatString)) { aResult = Double.valueOf(aValue); } else if (aFormatString.startsWith("T")) { DateTimeFormatter aFormatter = DateTimeFormat.forPattern(aFormatString.substring(1)); aResult = aFormatter.parseDateTime(aValue).toDate(); } } return aResult; }