Example usage for org.joda.time.format DateTimeFormat forPattern

List of usage examples for org.joda.time.format DateTimeFormat forPattern

Introduction

In this page you can find the example usage for org.joda.time.format DateTimeFormat forPattern.

Prototype

public static DateTimeFormatter forPattern(String pattern) 

Source Link

Document

Factory to create a formatter from a pattern string.

Usage

From source file:com.cohort.util.Calendar2.java

License:Open Source License

/**
 * This converts s into a double with epochSeconds.
 *
 * @param dateTimeFormat one of the ISO8601 formats above, or a Joda format.
 *   If it starts with "yyyy-MM", sourceTime will be parsed with Calendar2.parseISODateTimeZulu();
 *   else parse with Joda. /*from ww w  . j a va2s .  com*/
 * @return the epochSeconds value or NaN if trouble
 */
public static double toEpochSeconds(String sourceTime, String dateTimeFormat) {
    try {
        if (dateTimeFormat.startsWith("yyyy-MM"))
            //parse with Calendar2.parseISODateTime
            return safeIsoStringToEpochSeconds(sourceTime);

        //parse with Joda
        DateTimeFormatter formatter = DateTimeFormat.forPattern(dateTimeFormat).withZone(DateTimeZone.UTC);
        return formatter.parseMillis(sourceTime) / 1000.0; //thread safe

    } catch (Throwable t) {
        if (verbose && sourceTime != null && sourceTime.length() > 0)
            String2.log("  EDVTimeStamp.sourceTimeToEpochSeconds: Invalid sourceTime=" + sourceTime + " format="
                    + dateTimeFormat + "\n" + t.toString());
        return Double.NaN;
    }
}

From source file:com.cohort.util.Calendar2.java

License:Open Source License

/**
 * This converts sa into a DoubleArray with epochSeconds.
 *
 * @param dateTimeFormat one of the ISO8601 formats above, or a Joda format.
 *   If it starts with "yyyy-MM", sa strings will be parsed with Calendar2.parseISODateTimeZulu();
 *   else parse with Joda. //w ww  .jav a 2 s .c om
 * @return a DoubleArray with the epochSeconds values (any/all will be NaN if touble)
 */
public static DoubleArray toEpochSeconds(StringArray sa, String dateTimeFormat) {
    int n = sa.size();
    DoubleArray da = new DoubleArray(n, false);
    if (dateTimeFormat == null || dateTimeFormat.length() == 0) {
        da.addN(n, Double.NaN);
        return da;
    }
    try {

        if (dateTimeFormat.startsWith("yyyy-MM")) {
            //use Calendar2
            for (int i = 0; i < n; i++)
                da.add(safeIsoStringToEpochSeconds(sa.get(i)));
        } else {
            //use Joda
            boolean printError = verbose;
            DateTimeFormatter formatter = DateTimeFormat.forPattern(dateTimeFormat).withZone(DateTimeZone.UTC);
            da.addN(n, Double.NaN);
            for (int i = 0; i < n; i++) {
                String s = sa.get(i);
                if (s != null && s.length() > 0) {
                    try {
                        da.set(i, formatter.parseMillis(s) / 1000.0); //thread safe
                    } catch (Throwable t2) {
                        if (printError) {
                            String2.log(
                                    "  EDVTimeStamp.sourceTimeToEpochSeconds: error while parsing sourceTime="
                                            + s + " with format=" + dateTimeFormat + "\n" + t2.toString());
                            printError = false;
                        }
                    }
                }
            }
        }

    } catch (Throwable t) {
        if (verbose)
            String2.log("  Calendar2.toEpochSeconds: format=" + dateTimeFormat + ", Unexpected error="
                    + t.toString());
    }
    return da;

}

From source file:com.collective.celos.Util.java

License:Apache License

public static String toNominalTimeFormat(DateTime dt) {
    return dt.toString(DateTimeFormat.forPattern("YYYY-MM-dd'T'HH:mm'Z"));
}

From source file:com.confighub.core.utils.DateTimeUtils.java

License:Open Source License

public static Date parseISO8601Date(String dateString, Date cutoff) throws ConfigException {
    if (Utils.isBlank(dateString))
        return null;

    try {//from   w  w  w . j av a 2 s  .co  m
        DateTimeFormatter parser = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ");
        DateTime dt = parser.parseDateTime(dateString);
        dt = dt.toDateTime(DateTimeZone.UTC);
        Date date = dt.toDate();

        if (null != cutoff && date.before(cutoff))
            return cutoff;

        return date;
    } catch (Exception e) {
        throw new ConfigException(Error.Code.DATE_API_FORMAT_ERROR);
    }
}

From source file:com.constellio.app.conf.RegisteredLicense.java

private LocalDateTime parse(String dateYYYYMMDD) {
    if (dateYYYYMMDD == null) {
        return null;
    } else {/* w ww.j  av  a2  s. c om*/
        DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyyMMdd");
        return LocalDateTime.parse(dateYYYYMMDD.replace("-", "").replace("_", "").replace("/", ""), formatter);
    }
}

From source file:com.constellio.model.packaging.custom.CustomPluginsPackagingService.java

public LocalDateTime extractLicenseDateAttribute(File licenseFile, String theLicenseContent, String method) {
    String value = extractLicenseAttribute(licenseFile, theLicenseContent, method);
    try {//from   w w  w . j a va  2  s  . c om
        DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyyMMdd");
        return LocalDateTime.parse(value.replace("-", ""), formatter);
    } catch (IllegalArgumentException e) {
        throw new CustomPluginsPackagingServiceException.InvalidDate(licenseFile, method, e);
    }

}

From source file:com.court.controller.HomeFXMLController.java

/**
 * Initializes the controller class.//from  w w w.  java  2s  .  c o m
 */
@Override
public void initialize(URL url, ResourceBundle rb) {

    XYChart.Series<String, Number> series_1 = new XYChart.Series<>();
    Map<String, Number> map = getLoanReleasedData();

    for (Map.Entry<String, Number> entry : map.entrySet()) {
        String key = new SimpleDateFormat("MMM")
                .format(DateTime.parse(entry.getKey(), DateTimeFormat.forPattern("yyyy-MM-dd")).toDate());
        Number value = entry.getValue();
        series_1.getData().add(new XYChart.Data<>(key, value));
    }

    //================================================================================================
    XYChart.Series<String, Number> series_2 = new XYChart.Series<>();
    Map<String, Number> map1 = getLoanCollectionData();

    for (Map.Entry<String, Number> entry : map1.entrySet()) {
        String key = new SimpleDateFormat("MMM")
                .format(DateTime.parse(entry.getKey(), DateTimeFormat.forPattern("yyyy-MM-dd")).toDate());
        Number value = entry.getValue();
        series_2.getData().add(new XYChart.Data<>(key, value));
    }

    loan_release_chart.getData().add(series_1);
    loan_collection_chart.getData().add(series_2);
    //live update time tile =============================
    FxUtilsHandler.startTimeOf(timeTile);
    updateMemberCountTile(mbr_count_txt);
    updateMonthlyCollection(tot_col_txt);
    updateLoanAmtCollectionCurrentMonth(ln_amt_tot_label);

    //=========================
    collection_tile.setText("TOTAL COLLECTION - " + new SimpleDateFormat("MMMMM").format(new Date()));
    ln_amt_total_tile.setText("TOTAL LOAN AMOUNT GIVEN - " + new SimpleDateFormat("MMMMM").format(new Date()));

}

From source file:com.cronutils.mapper.format.DateTimeFormatBuilder.java

License:Apache License

public DateTimeFormatter createPatternFor(String expression) {
    DateTimeFormatLocaleStrategy localeStrategy = createLocaleStrategyInstance();
    Validate.notBlank(expression);//from  w ww. j  a va2  s.co m
    expression = expression.replaceAll("\\s+", " ");
    expression = expression.replace(" AM", "AM").replace(" am", "am").replace(" PM", "PM").replace(" pm", "pm");
    String[] parts = expression.split(" ");
    StringBuilder builder = new StringBuilder();
    for (String part : parts) {
        builder.append(String.format("%s ", localeStrategy.retrievePattern(part)));
    }
    return DateTimeFormat.forPattern(builder.toString().trim());
}

From source file:com.cryart.sabbathschool.viewmodel.SSLessonItemViewModel.java

License:Open Source License

public String getDate() {

    String startDateOut = DateTimeFormat.forPattern(SSConstants.SS_DATE_FORMAT_OUTPUT)
            .print(DateTimeFormat.forPattern(SSConstants.SS_DATE_FORMAT).parseDateTime(ssLesson.start_date));

    String endDateOut = DateTimeFormat.forPattern(SSConstants.SS_DATE_FORMAT_OUTPUT)
            .print(DateTimeFormat.forPattern(SSConstants.SS_DATE_FORMAT).parseDateTime(ssLesson.end_date));

    return StringUtils.capitalize(startDateOut) + " - " + StringUtils.capitalize(endDateOut);
}

From source file:com.cryart.sabbathschool.viewmodel.SSLessonsViewModel.java

License:Open Source License

public void onReadClick() {
    if (ssQuarterlyInfo != null && ssQuarterlyInfo.lessons.size() > 0) {
        DateTime today = DateTime.now().withTimeAtStartOfDay();
        String ssLessonIndex = ssQuarterlyInfo.lessons.get(0).index;

        for (SSLesson ssLesson : ssQuarterlyInfo.lessons) {
            DateTime startDate = DateTimeFormat.forPattern(SSConstants.SS_DATE_FORMAT)
                    .parseDateTime(ssLesson.start_date).withTimeAtStartOfDay();

            DateTime endDate = DateTimeFormat.forPattern(SSConstants.SS_DATE_FORMAT)
                    .parseDateTime(ssLesson.end_date).plusDays(1).withTimeAtStartOfDay();

            if (new Interval(startDate, endDate).contains(today)) {
                ssLessonIndex = ssLesson.index;
                break;
            }//from   ww w.j  a  v  a 2 s.co m
        }

        Intent ssReadingIntent = new Intent(context, SSReadingActivity.class);
        ssReadingIntent.putExtra(SSConstants.SS_LESSON_INDEX_EXTRA, ssLessonIndex);
        context.startActivity(ssReadingIntent);
    }
}