Example usage for org.joda.time.format DateTimeFormatter parseDateTime

List of usage examples for org.joda.time.format DateTimeFormatter parseDateTime

Introduction

In this page you can find the example usage for org.joda.time.format DateTimeFormatter parseDateTime.

Prototype

public DateTime parseDateTime(String text) 

Source Link

Document

Parses a date-time from the given text, returning a new DateTime.

Usage

From source file:org.vpac.ndg.query.Query.java

License:Open Source License

private DateTime[] determineTemporalBounds(QueryDefinition qd) {
    DateTime temporalBounds[] = new DateTime[2];
    DateTimeFormatter fmt = ISODateTimeFormat.dateOptionalTimeParser().withZoneUTC();
    GridDefinition gd = qd.output.grid;/*from www  . ja va 2 s  .c o m*/
    if (gd.timeMin != null && !gd.timeMin.isEmpty())
        temporalBounds[0] = fmt.parseDateTime(qd.output.grid.timeMin);
    if (gd.timeMax != null && !gd.timeMax.isEmpty())
        temporalBounds[1] = fmt.parseDateTime(qd.output.grid.timeMax);
    return temporalBounds;
}

From source file:org.wicketopia.joda.util.format.JodaFormatSupport.java

License:Apache License

public T convertToObject(String value, Locale locale) {
    if (Strings.isEmpty(value)) {
        return null;
    }// www  .j  a va2 s .  com

    DateTimeFormatter format = formatProvider.getFormatter();

    if (format == null) {
        throw new IllegalStateException("format must be not null");
    }
    format = format.withLocale(locale).withPivotYear(pivotYear);
    if (applyTimeZoneDifference) {
        TimeZone zone = getClientTimeZone();
        // instantiate now/ current time
        MutableDateTime dt = new MutableDateTime();
        if (zone != null) {
            // set time zone for client
            format = format.withZone(DateTimeZone.forTimeZone(zone));
            dt.setZone(DateTimeZone.forTimeZone(zone));
        }
        try {
            // parse date retaining the time of the submission
            int result = format.parseInto(dt, value, 0);
            if (result < 0) {
                throw new ConversionException(new ParseException("unable to parse date " + value, ~result));
            }
        } catch (RuntimeException e) {
            throw new ConversionException(e);
        }
        // apply the server time zone to the parsed value
        dt.setZone(getServerTimeZone());
        return translator.fromDateTime(dt.toDateTime());
    } else {
        try {
            DateTime date = format.parseDateTime(value);
            return date == null ? null : translator.fromDateTime(date);
        } catch (RuntimeException e) {
            throw new ConversionException(e);
        }
    }
}

From source file:org.wicketstuff.calendar.util.DateConverter.java

License:Apache License

/**
 * @see org.apache.wicket.util.convert.IConverter#convertToObject(java.lang.String,
 *      java.util.Locale)//w ww . j ava  2s.c om
 */
public Object convertToObject(String value, Locale locale) {
    DateTimeFormatter format = getFormat();

    if (applyTimeZoneDifference) {
        TimeZone zone = getClientTimeZone();
        // instantiate now/ current time
        MutableDateTime dt = new MutableDateTime();
        if (zone != null) {
            // set time zone for client
            format = format.withZone(DateTimeZone.forTimeZone(zone));
            dt.setZone(DateTimeZone.forTimeZone(zone));
        }
        // parse date retaining the time of the submission
        format.parseInto(dt, value, 0);
        // apply the server time zone to the parsed value
        dt.setZone(getTimeZone());
        return dt.toDate();
    } else {
        return format.parseDateTime(value).toDate();
    }
}

From source file:org.wicketstuff.datetime.DateConverter.java

License:Apache License

/**
 * @see org.apache.wicket.util.convert.IConverter#convertToObject(java.lang.String,
 *      java.util.Locale)/*from   w  w w  .  ja  va  2s.com*/
 */
@Override
public Date convertToObject(String value, Locale locale) {
    if (Strings.isEmpty(value)) {
        return null;
    }

    DateTimeFormatter format = getFormat(locale);
    if (format == null) {
        throw new IllegalStateException("format must be not null");
    }

    if (applyTimeZoneDifference) {
        TimeZone zone = getClientTimeZone();
        DateTime dateTime;

        // set time zone for client
        format = format.withZone(getTimeZone());

        try {
            // parse date retaining the time of the submission
            dateTime = format.parseDateTime(value);
        } catch (RuntimeException e) {
            throw newConversionException(e, locale);
        }
        // apply the server time zone to the parsed value
        if (zone != null) {
            dateTime = dateTime.withZoneRetainFields(DateTimeZone.forTimeZone(zone));
        }

        return dateTime.toDate();
    } else {
        try {
            DateTime date = format.parseDateTime(value);
            return date.toDate();
        } catch (RuntimeException e) {
            throw newConversionException(e, locale);
        }
    }
}

From source file:org.xmlcml.euclid.JodaDate.java

License:Apache License

/** return null if fails
 * /*  www  . j av a  2s . c  o m*/
 * @param formatter
 * @param s
 * @return
 */
public static DateTime parseQuietly(DateTimeFormatter formatter, String s) {
    DateTime dateTime = null;
    if (formatter != null) {
        try {
            dateTime = formatter.parseDateTime(s);
        } catch (Exception e) {
            //
        }
    }
    return dateTime;
}

From source file:org.xmlcml.euclid.JodaDate.java

License:Apache License

public static DateTime parseDate(String date, String format) {
    DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(format);
    if (format.endsWith("Z")) {
    } else {//from  w ww.  ja va 2 s  . co m
        dateTimeFormatter = dateTimeFormatter.withZone(DateTimeZone.forID("UTC"));
    }
    DateTime dateTime = dateTimeFormatter.parseDateTime(date);
    return dateTime.withZone(DateTimeZone.forID("UTC"));
}

From source file:org.yamj.common.tools.DateTimeTools.java

License:Open Source License

/**
 * Parses a string in the provided format to a DateTime
 *
 * @param stringDate the date to parse//from   w w  w  . j  av  a  2s  .  com
 * @param datePattern the pattern to parse
 * @return
 */
public static DateTime parseDate(String stringDate, String datePattern) {
    DateTimeFormatter formatter = DateTimeFormat.forPattern(datePattern);
    return formatter.parseDateTime(stringDate);
}

From source file:piecework.model.DateSearchFacet.java

License:Educational Community License

public Criteria criteria(DateRange value) {
    Criteria criteria = where(query);//from  www .  ja va  2s .c om
    DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTimeParser();
    try {
        if (StringUtils.isNotEmpty(value.getAfter())) {
            DateTime dateTime = dateTimeFormatter.parseDateTime(value.getAfter());
            criteria.gt(dateTime.toDate());
        }
        if (StringUtils.isNotEmpty(value.getBefore())) {
            DateTime dateTime = dateTimeFormatter.parseDateTime(value.getBefore());
            criteria.lt(dateTime.toDate());
        }
    } catch (Exception e) {
        LOG.warn("Unable to parse " + value + " as a datetime object", e);
    }

    return criteria;
}

From source file:playground.mzilske.d4d.CreatePopulation.java

License:Open Source License

private Map<Id, List<Sighting>> readSightings(String startDate, String filename, final int populationIdSuffix) {
    final Map<Id, List<Sighting>> sightings = new HashMap<Id, List<Sighting>>();

    final DateTimeFormatter dateTimeFormat = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
    final DateTime beginning = dateTimeFormat.parseDateTime(startDate);
    TabularFileParser tfp = new TabularFileParser();
    TabularFileParserConfig tabularFileParserConfig = new TabularFileParserConfig();
    tabularFileParserConfig.setFileName(filename);
    tabularFileParserConfig.setDelimiterRegex("\t");
    TabularFileHandler handler = new TabularFileHandler() {

        @Override/*from w  w w  .  jav  a  2 s . c  o m*/
        public void startRow(String[] row) {

            DateTime sightingTime = dateTimeFormat.parseDateTime(row[1]);

            Id<Person> personId = Id.create(row[0] + "_" + Integer.toString(populationIdSuffix), Person.class);
            String cellTowerId = row[2];

            long timeInSeconds = (sightingTime.getMillis() - beginning.getMillis()) / 1000;

            if (!cellTowerId.equals("-1")) {
                CellTower cellTower = zones.cellTowers.get(cellTowerId);
                if (cellTower != null) {
                    if (interestedInTime(timeInSeconds)) {
                        List<Sighting> sightingsOfPerson = sightings.get(personId);
                        if (sightingsOfPerson == null) {
                            sightingsOfPerson = new ArrayList<Sighting>();
                            sightings.put(personId, sightingsOfPerson);
                        }

                        Sighting sighting = new Sighting(personId, timeInSeconds, cellTowerId);
                        sightingsOfPerson.add(sighting);
                        cellTower.nSightings++;
                    }
                }
            }

        }

        private boolean interestedInTime(long timeInSeconds) {
            if ((timeInSeconds >= 0.0) && (timeInSeconds < 24 * 60 * 60)) {
                return true;
            } else {
                return false;
            }
        }
    };
    tfp.parse(tabularFileParserConfig, handler);
    return sightings;
}