Example usage for org.joda.time DateMidnight parse

List of usage examples for org.joda.time DateMidnight parse

Introduction

In this page you can find the example usage for org.joda.time DateMidnight parse.

Prototype

public static DateMidnight parse(String str, DateTimeFormatter formatter) 

Source Link

Document

Parses a DateMidnight from the specified string using a formatter.

Usage

From source file:it.d4nguard.rgrpg.util.dynacast.adapters.DateTimeAdapter.java

License:Open Source License

/**
 * {@inheritDoc}/*from   w ww .jav a 2 s  .  c  o m*/
 */
@Override
public ReadableInstant adapt(String value) {
    // value is a string formatted as: "07/04/1987[dd/MM/yyyy]"
    String date = "";
    DateTimeFormatter fmt;
    Triplet<String, String, String> tri = StringUtils.getBetween(value, '[', ']');
    date = tri.getLeft();
    if (tri.hasCenter())
        fmt = DateTimeFormat.forPattern(tri.getCenter());
    else
        fmt = ISODateTimeFormat.localDateOptionalTimeParser();
    fmt = fmt.withLocale(Locale.getDefault());
    if (getType().equals(DateTime.class))
        return DateTime.parse(date, fmt);
    else if (getType().equals(DateMidnight.class))
        return DateMidnight.parse(date, fmt);
    else if (getType().equals(Instant.class))
        return Instant.parse(date, fmt);
    else if (getType().equals(MutableDateTime.class))
        return MutableDateTime.parse(date, fmt);
    else
        throw new UnsupportedOperationException("type");
}

From source file:org.filteredpush.qc.date.DateUtils.java

License:Apache License

/**
 * Test to see if a string appears to represent a date range of more than one day.
 * /*from  w w  w  .  ja  v a2  s. c  o m*/
 * @param eventDate to check
 * @return true if a date range, false otherwise.
 */
public static boolean isRange(String eventDate) {
    boolean isRange = false;
    if (eventDate != null) {
        String[] dateBits = eventDate.split("/");
        if (dateBits != null && dateBits.length == 2) {
            //probably a range.
            DateTimeParser[] parsers = { DateTimeFormat.forPattern("yyyy-MM").getParser(),
                    DateTimeFormat.forPattern("yyyy").getParser(),
                    ISODateTimeFormat.dateOptionalTimeParser().getParser() };
            DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
            try {
                // must be at least a 4 digit year.
                if (dateBits[0].length() > 3 && dateBits[1].length() > 3) {
                    DateMidnight startDate = LocalDate.parse(dateBits[0], formatter).toDateMidnight();
                    DateMidnight endDate = LocalDate.parse(dateBits[1], formatter).toDateMidnight();
                    // both start date and end date must parse as dates.
                    isRange = true;
                }
            } catch (Exception e) {
                // not a date range
                e.printStackTrace();
                logger.debug(e.getMessage());
            }
        } else if (dateBits != null && dateBits.length == 1) {
            logger.debug(dateBits[0]);
            // Date bits does not contain a /
            // Is eventDate in the form yyyy-mm-dd, if so, not a range  
            DateTimeParser[] parsers = { DateTimeFormat.forPattern("yyyy-MM-dd").getParser(), };
            DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
            try {
                DateMidnight date = DateMidnight.parse(eventDate, formatter);
                isRange = false;
            } catch (Exception e) {
                logger.debug(e.getMessage());
                // not parsable with the yyyy-mm-dd parser.
                DateTimeParser[] parsers2 = { DateTimeFormat.forPattern("yyyy-MM").getParser(),
                        DateTimeFormat.forPattern("yyyy").getParser(), };
                formatter = new DateTimeFormatterBuilder().append(null, parsers2).toFormatter();
                try {
                    // must be at least a 4 digit year.
                    if (dateBits[0].length() > 3) {
                        DateMidnight startDate = DateMidnight.parse(dateBits[0], formatter);
                        // date must parse as either year or year and month dates.
                        isRange = true;
                    }
                } catch (Exception e1) {
                    // not a date range
                }

            }

        }
    }
    return isRange;
}

From source file:org.filteredpush.qc.date.DateUtils.java

License:Apache License

/**
 * Given a string that may be a date or a date range, extract a interval of
 * dates from that date range (ignoring time (thus the duration for the 
 * interval will be from one date midnight to another).
 * /*from w ww .j av a2s . c  o m*/
 * This probably is not the method you want - given 1950-01-05, it returns an
 * interval from midnight as the start of the 5th to midnight on the start of the 6th,
 * simply grabbing start end days from this interval will return 5 and 6, which
 * probably isn't what you are expecting.  
 * 
 * @see DateUtils#extractInterval(String) which is probably the method you want.
 * 
 * @param eventDate a string containing a dwc:eventDate from which to extract an interval.
 * @return An interval from one DateMidnight to another DateMidnight, null if no interval can be extracted.
 */
public static Interval extractDateInterval(String eventDate) {
    Interval result = null;
    DateTimeParser[] parsers = { DateTimeFormat.forPattern("yyyy-MM").getParser(),
            DateTimeFormat.forPattern("yyyy").getParser(),
            ISODateTimeFormat.dateOptionalTimeParser().getParser() };
    DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
    if (eventDate != null && eventDate.contains("/") && isRange(eventDate)) {
        String[] dateBits = eventDate.split("/");
        try {
            // must be at least a 4 digit year.
            if (dateBits[0].length() > 3 && dateBits[1].length() > 3) {
                DateMidnight startDate = DateMidnight.parse(dateBits[0], formatter);
                DateMidnight endDate = DateMidnight.parse(dateBits[1], formatter);
                if (dateBits[1].length() == 4) {
                    result = new Interval(startDate, endDate.plusMonths(12).minusDays(1));
                } else if (dateBits[1].length() == 7) {
                    result = new Interval(startDate, endDate.plusMonths(1).minusDays(1));
                } else {
                    result = new Interval(startDate, endDate);
                }
            }
        } catch (Exception e) {
            // not a date range
            logger.error(e.getMessage());
        }
    } else {
        try {
            DateMidnight startDate = DateMidnight.parse(eventDate, formatter);
            logger.debug(eventDate);
            logger.debug(startDate);
            if (eventDate.length() == 4) {
                result = new Interval(startDate, startDate.plusMonths(12).minusDays(1));
            } else if (eventDate.length() == 7) {
                result = new Interval(startDate, startDate.plusMonths(1).minusDays(1));
            } else {
                result = new Interval(startDate, startDate.plusDays(1));
            }
        } catch (IllegalFieldValueException ex) {
            // can parse as a date but some token has an out of range value
            logger.debug(ex);
        } catch (Exception e) {
            // not a date
            logger.error(e.getMessage(), e);
        }
    }
    return result;
}

From source file:org.filteredpush.qc.date.DateUtils.java

License:Apache License

/**
 * Given a string that may be a date or a date range, extract a interval of
 * dates from that date range, up to the end milisecond of the last day.
 * /*from   www . j a  v a  2 s .c  o  m*/
 * @see DateUtils#extractDateInterval(String) which returns a pair of DateMidnights.
 * 
 * @param eventDate a string containing a dwc:eventDate from which to extract an interval.
 * @return an interval from the beginning of event date to the end of event date.
 */
public static Interval extractInterval(String eventDate) {
    Interval result = null;
    DateTimeParser[] parsers = { DateTimeFormat.forPattern("yyyy-MM").getParser(),
            DateTimeFormat.forPattern("yyyy").getParser(),
            ISODateTimeFormat.dateOptionalTimeParser().getParser() };
    DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
    if (eventDate != null && eventDate.contains("/") && isRange(eventDate)) {
        String[] dateBits = eventDate.split("/");
        try {
            // must be at least a 4 digit year.
            if (dateBits[0].length() > 3 && dateBits[1].length() > 3) {
                DateMidnight startDate = DateMidnight.parse(dateBits[0], formatter);
                DateTime endDate = DateTime.parse(dateBits[1], formatter);
                logger.debug(startDate);
                logger.debug(endDate);
                if (dateBits[1].length() == 4) {
                    result = new Interval(startDate, endDate.plusMonths(12).minus(1l));
                } else if (dateBits[1].length() == 7) {
                    result = new Interval(startDate, endDate.plusMonths(1).minus(1l));
                } else {
                    result = new Interval(startDate, endDate.plusDays(1).minus(1l));
                }
                logger.debug(result);
            }
        } catch (Exception e) {
            // not a date range
            logger.error(e.getMessage());
        }
    } else {
        try {
            DateMidnight startDate = DateMidnight.parse(eventDate, formatter);
            logger.debug(startDate);
            if (eventDate.length() == 4) {
                DateTime endDate = startDate.toDateTime().plusMonths(12).minus(1l);
                result = new Interval(startDate, endDate);
                logger.debug(result);
            } else if (eventDate.length() == 7) {
                DateTime endDate = startDate.toDateTime().plusMonths(1).minus(1l);
                result = new Interval(startDate, endDate);
                logger.debug(result);
            } else {
                DateTime endDate = startDate.toDateTime().plusDays(1).minus(1l);
                result = new Interval(startDate, endDate);
                logger.debug(result);
            }
        } catch (Exception e) {
            // not a date
            logger.error(e.getMessage());
        }
    }
    return result;
}

From source file:org.filteredpush.qc.date.DateUtils.java

License:Apache License

/**
 * Extract a single joda date from an event date.
 * /*  ww  w  .j av  a  2s. c o m*/
 * @param eventDate an event date from which to try to extract a DateMidnight
 * @return a DateMidnight or null if a date cannot be extracted
 */
public static DateMidnight extractDate(String eventDate) {
    DateMidnight result = null;
    DateTimeParser[] parsers = { DateTimeFormat.forPattern("yyyy-MM").getParser(),
            DateTimeFormat.forPattern("yyyy").getParser(),
            DateTimeFormat.forPattern("yyyy-MM-dd/yyyy-MM-dd").getParser(),
            ISODateTimeFormat.dateOptionalTimeParser().getParser(), ISODateTimeFormat.date().getParser() };
    DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
    try {
        result = DateMidnight.parse(eventDate, formatter);
        logger.debug(result);
    } catch (Exception e) {
        // not a date
        logger.error(e.getMessage());
    }
    return result;
}

From source file:org.key2gym.client.highlighters.ExpirationDateHighlighter.java

License:Apache License

/**
 * Updates the component's highlight./* w w  w.  jav a 2 s  . c om*/
 */
@Override
protected ColorScheme getHighlightModelFor(String text) {

    DateMidnight value;

    try {
        value = DateMidnight.parse(text, DateTimeFormat.forPattern("dd-MM-yyyy"));
    } catch (IllegalArgumentException ex) {
        value = null;
    }

    if (value == null) {
        return NULL_SCHEME;
    }

    int daysTillExpiration = Days.daysBetween(DateMidnight.now(), value).getDays();

    if (daysTillExpiration > 2) {
        return NOT_SOON_SCHEME;
    } else if (daysTillExpiration > 0) {
        return SOON_SCHEME;
    } else {
        return PASSED_SCHEME;
    }
}

From source file:org.projectforge.web.wicket.converter.JodaDateConverter.java

License:Open Source License

/**
 * Attempts to convert a String to a Date object. Pre-processes the input by invoking the method preProcessInput(), then uses an ordered
 * list of DateFormat objects (supplied by getDateFormats()) to try and parse the String into a Date.
 *///from   w w w .  j a v a 2 s  . c  o m
@Override
public DateMidnight convertToObject(final String value, final Locale locale) {
    if (StringUtils.isBlank(value) == true) {
        return null;
    }
    final String[] formatStrings = getFormatStrings(locale);
    final DateTimeFormatter[] dateFormats = new DateTimeFormatter[formatStrings.length];

    for (int i = 0; i < formatStrings.length; i++) {
        dateFormats[i] = getDateTimeFormatter(formatStrings[i], locale);
    }
    DateMidnight date = null;
    for (final DateTimeFormatter formatter : dateFormats) {
        try {
            date = DateMidnight.parse(value, formatter);
            break;
        } catch (final Exception ex) { /* Do nothing, we'll get lots of these. */
            if (log.isDebugEnabled() == true) {
                log.debug(ex.getMessage(), ex);
            }
        }
    }
    // If we successfully parsed, return a date, otherwise send back an error
    if (date != null) {
        return date;
    } else {
        log.info("Unparseable date string (user's input): " + value);
        throw new ConversionException("validation.error.general"); // Message key will not be used (dummy).
    }
}