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:com.helger.datetime.format.PDTFromString.java

License:Apache License

@Nullable
public static DateTime getDateTimeFromString(@Nullable final String sValue,
        @Nonnull final DateTimeFormatter aDF) {
    if (aDF == null)
        throw new NullPointerException("dateTimeFormatter");

    if (StringHelper.hasText(sValue))
        try {/*  w ww.  j  av a 2 s .  c  o m*/
            return aDF.parseDateTime(sValue);
        } catch (final IllegalArgumentException ex) {
            if (s_aLogger.isDebugEnabled())
                s_aLogger.debug("Failed to parse date '" + sValue + "' with " + aDF);
        }
    return null;
}

From source file:com.hula.lang.util.DateUtil.java

License:Apache License

/**
 * Convert a String into a Date//from  w w w .ja va  2  s .  c o  m
 * 
 * @param string The string to convert
 * @param format The format of the String
 * @return A Date
 */
public static Date toDate(String string, String format) {
    DateTimeFormatter fmt = DateTimeFormat.forPattern(format);
    DateTime dt = fmt.parseDateTime(string);
    return dt.toDate();
}

From source file:com.hust.duc.businessobjects.db.SubscriptionsDb.java

License:Open Source License

/**
 * Check if the given channel has new videos (by looking into the {@link SubscriptionsVideosTable}
 * [i.e. video cache table]).//from w w w .j ava2s.c  o  m
 *
 * @param channel Channel to check.
 *
 * @return True if the user hasn't visited the channel and new videos have been uploaded in the
 * meantime; false otherwise.
 */
public boolean channelHasNewVideos(YouTubeChannel channel) {
    DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
    String query = String.format("SELECT COUNT(*) FROM %s WHERE %s = ? AND %s > ?",
            SubscriptionsVideosTable.TABLE_NAME, SubscriptionsVideosTable.COL_CHANNEL_ID,
            SubscriptionsVideosTable.COL_YOUTUBE_VIDEO_DATE);
    Cursor cursor = SubscriptionsDb.getSubscriptionsDb().getReadableDatabase().rawQuery(query, new String[] {
            channel.getId(),
            fmt.parseDateTime(new DateTime(new Date(channel.getLastVisitTime())).toString()).toString() });
    boolean channelHasNewVideos = false;

    if (cursor.moveToFirst())
        channelHasNewVideos = cursor.getInt(0) > 0;

    cursor.close();
    return channelHasNewVideos;
}

From source file:com.hust.duc.businessobjects.db.SubscriptionsDb.java

License:Open Source License

/**
 * Loop through each video saved in the passed {@link YouTubeChannel} and save it into the database, if it's not already been saved
 * @param channel//from w  w w  .  j  a v a 2 s. c  om
 */
public void saveChannelVideos(YouTubeChannel channel) {
    Gson gson = new Gson();
    DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
    for (YouTubeVideo video : channel.getYouTubeVideos()) {
        if (!hasVideo(video)) {
            ContentValues values = new ContentValues();
            values.put(SubscriptionsVideosTable.COL_CHANNEL_ID, channel.getId());
            values.put(SubscriptionsVideosTable.COL_YOUTUBE_VIDEO_ID, video.getId());
            values.put(SubscriptionsVideosTable.COL_YOUTUBE_VIDEO, gson.toJson(video).getBytes());
            values.put(SubscriptionsVideosTable.COL_YOUTUBE_VIDEO_DATE,
                    fmt.parseDateTime(video.getPublishDate().toStringRfc3339()).toString());

            getWritableDatabase().insert(SubscriptionsVideosTable.TABLE_NAME, null, values);
        }
    }
}

From source file:com.ikon.servlet.ValidateLicenseServlet.java

private int remainingDays() {
    DateTimeFormatter formatter = DateTimeFormat.forPattern("d MMM yyyy");
    DateTime expiryDate = null;/* w ww. j  a va  2 s  .c o m*/
    try {
        expiryDate = formatter.parseDateTime(FileUtils.readFileToString(LICENSE_PATH));
    } catch (IOException e) {
        e.printStackTrace();
    }
    return Days.daysBetween(new DateTime(), expiryDate).getDays();
}

From source file:com.inbravo.scribe.rest.service.crm.CRMMessageFormatUtils.java

License:Open Source License

/**
 * //  w  ww  .jav  a2s. co  m
 * @param finishDate
 * @return
 * @throws Exception
 */
public static final DateTime validateInputDate(final String date, final String permittedDateFormats)
        throws Exception {

    logger.debug(
            "----Inside validateInputDate, date: " + date + " & permittedDateFormats: " + permittedDateFormats);

    /* Seperate all the formats */
    final String[] defaultDateFormats = permittedDateFormats.split(",");

    /* Create array for all date parsing formats */
    final DateTimeParser[] dateTimeParser = new DateTimeParser[defaultDateFormats.length];

    /* Parse with individual formats */
    for (int i = 0; i < defaultDateFormats.length; i++) {

        /* If format is valid */
        if (defaultDateFormats[i] != null && !"".equals(defaultDateFormats[i])) {

            /* Create new parser for each format */
            dateTimeParser[i] = DateTimeFormat.forPattern(defaultDateFormats[i].trim()).getParser();
        }
    }

    /* Final date formater builder */
    final DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder().append(null, dateTimeParser)
            .toFormatter();

    /* Parse user supplied date */
    final DateTime updatedDate = dateTimeFormatter.parseDateTime(date);

    logger.debug("----Inside validateInputDate, updated date: " + updatedDate);

    /* Return updated date */
    return updatedDate;
}

From source file:com.inbravo.scribe.rest.service.crm.zh.ZHCRMMessageFormatUtils.java

License:Open Source License

public static void main(String[] args) {

    final DateTimeFormatter zhDateTimeFormatter = DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss Z");

    System.out.println(zhDateTimeFormatter.parseDateTime("2012/11/02 01:00:19 -0700"));

}

From source file:com.inbravo.scribe.rest.service.writer.utils.ConvertTimeZone.java

License:Open Source License

public static void main(String[] args) throws ParseException {

    DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser();

    DateTime dateTimeHere = parser.parseDateTime("2012-10-31T17:25:54.478-07:00");

    DateTime secondDateTime = dateTimeHere.withZone(DateTimeZone.forID("PST8PDT"));

    System.out.println("second: " + secondDateTime);
}

From source file:com.knewton.mapreduce.example.StudentEventAbstractMapper.java

License:Apache License

/**
 * Sets up a DateTime interval for excluding student events. When start time is not set then it
 * defaults to the beginning of time. If end date is not specified then it defaults to
 * "the end of time"./*from  ww  w.  j a v  a2s. co  m*/
 */
private void setupTimeRange(Configuration conf) {
    DateTimeFormatter dtf = DateTimeFormat.forPattern(DATE_TIME_STRING_FORMAT).withZoneUTC();
    String startDateStr = conf.get(PropertyConstants.START_DATE.txt);
    String endDateStr = conf.get(PropertyConstants.END_DATE.txt);
    // No need to instantiate timeRange.
    if (startDateStr == null && endDateStr == null) {
        return;
    }
    DateTime startDate;
    if (startDateStr != null) {
        startDate = dtf.parseDateTime(startDateStr);
    } else {
        startDate = new DateTime(0).year().withMinimumValue().withZone(DateTimeZone.UTC);
    }
    DateTime endDate;
    if (endDateStr != null) {
        endDate = dtf.parseDateTime(endDateStr);
    } else {
        endDate = new DateTime(0).withZone(DateTimeZone.UTC).year().withMaximumValue();
    }
    this.timeRange = new Interval(startDate, endDate);
}

From source file:com.knewton.mapreduce.StudentEventAbstractMapper.java

License:Apache License

/**
 * Sets up a DateTime interval for excluding student events. When start time is not set then it
 * defaults to the beginning of time. If end date is not specified then it defaults to
 * "the end of time".//  w w  w . j  a v  a2  s .  c o m
 *
 * @param conf
 */
private void setupTimeRange(Configuration conf) {
    DateTimeFormatter dtf = DateTimeFormat.forPattern(DATE_TIME_STRING_FORMAT).withZoneUTC();
    String startDateStr = conf.get(START_DATE_PARAMETER_NAME);
    String endDateStr = conf.get(END_DATE_PARAMETER_NAME);
    // No need to instantiate timeRange.
    if (startDateStr == null && endDateStr == null) {
        return;
    }
    DateTime startDate;
    if (startDateStr != null) {
        startDate = dtf.parseDateTime(startDateStr);
    } else {
        startDate = new DateTime(Long.MIN_VALUE + ONE_DAY_IN_MILLIS).withZone(DateTimeZone.UTC);
    }
    DateTime endDate;
    if (endDateStr != null) {
        endDate = dtf.parseDateTime(endDateStr);
    } else {
        endDate = new DateTime(Long.MAX_VALUE - ONE_DAY_IN_MILLIS).withZone(DateTimeZone.UTC);
    }
    this.timeRange = new Interval(startDate, endDate);
}