Example usage for org.joda.time.format DateTimeFormatterBuilder appendLiteral

List of usage examples for org.joda.time.format DateTimeFormatterBuilder appendLiteral

Introduction

In this page you can find the example usage for org.joda.time.format DateTimeFormatterBuilder appendLiteral.

Prototype

public DateTimeFormatterBuilder appendLiteral(String text) 

Source Link

Document

Instructs the printer to emit specific text, and the parser to expect it.

Usage

From source file:JodaDT.java

License:Open Source License

/**
 * Creates a DateTime object from a String with this format
 * DD/MM/YYYY-hh:mm:ss.// w w w  . ja v a2  s .c o  m
 *
 * @param date with format DD/MM/YYYY-hh:mm:ss
 * @return a DateTime object or null
 */
public static DateTime parseDDMMYYYYhhmmss(String date) {
    if (date != null) {
        DateTimeFormatterBuilder dtfb = new DateTimeFormatterBuilder();
        dtfb.appendDayOfMonth(2);
        dtfb.appendLiteral('/');
        dtfb.appendMonthOfYear(2);
        dtfb.appendLiteral('/');
        dtfb.appendYear(4, 4);
        dtfb.appendLiteral('-');
        dtfb.appendHourOfDay(2);
        dtfb.appendLiteral(':');
        dtfb.appendMinuteOfHour(2);
        dtfb.appendLiteral(':');
        dtfb.appendSecondOfMinute(2);
        DateTimeFormatter dtf = dtfb.toFormatter();
        DateTime dt = dtf.parseDateTime(date);
        return dt;
    } else {
        return null;
    }
}

From source file:JodaDT.java

License:Open Source License

/**
 * Creates a DateTime object from a String with this format
 * DD/MM/YYYY-hh:mm./*from  w w  w .  java  2  s  . c o m*/
 *
 * @param date with format DD/MM/YYYY-hh:mm
 * @return a DateTime object or null
 */
public static DateTime parseDDMMYYYYhhmm(String date) {
    if (date != null) {
        DateTimeFormatterBuilder dtfb = new DateTimeFormatterBuilder();
        dtfb.appendDayOfMonth(2);
        dtfb.appendLiteral('/');
        dtfb.appendMonthOfYear(2);
        dtfb.appendLiteral('/');
        dtfb.appendYear(4, 4);
        dtfb.appendLiteral('-');
        dtfb.appendHourOfDay(2);
        dtfb.appendLiteral(':');
        dtfb.appendMinuteOfHour(2);
        DateTimeFormatter dtf = dtfb.toFormatter();
        DateTime dt = dtf.parseDateTime(date);
        return dt;
    } else {
        return null;
    }
}

From source file:JodaDT.java

License:Open Source License

/**
 * Creates a DateTime object from a String with this format DD/MM/YYYY-hh
 *
 * @param date with format DD/MM/YYYY-hh
 * @return a DateTime object or null//from  ww  w.  ja va2 s  . c o  m
 */
public static DateTime parseDDMMYYYYhh(String data) {
    if (data != null) {
        DateTimeFormatterBuilder dtfb = new DateTimeFormatterBuilder();
        dtfb.appendDayOfMonth(2);
        dtfb.appendLiteral('/');
        dtfb.appendMonthOfYear(2);
        dtfb.appendLiteral('/');
        dtfb.appendYear(4, 4);
        dtfb.appendLiteral('-');
        dtfb.appendHourOfDay(2);
        DateTimeFormatter dtf = dtfb.toFormatter();
        DateTime dt = dtf.parseDateTime(data);
        return dt;
    } else {
        return null;
    }
}

From source file:JodaDT.java

License:Open Source License

/**
 * Creates a DateTime object from a String with this format DD/MM/YYYY
 *
 * @param date with format DD/MM/YYYY//from  w w  w.j  a  v a2s  .com
 * @return a DateTime object or null
 */
public static DateTime parseDDMMYYYY(String data) {
    if (data != null) {
        DateTimeFormatterBuilder dtfb = new DateTimeFormatterBuilder();
        dtfb.appendDayOfMonth(2);
        dtfb.appendLiteral('/');
        dtfb.appendMonthOfYear(2);
        dtfb.appendLiteral('/');
        dtfb.appendYear(4, 4);
        DateTimeFormatter dtf = dtfb.toFormatter();
        DateTime dt = dtf.parseDateTime(data);
        return dt;
    } else {
        return null;
    }
}

From source file:JodaDT.java

License:Open Source License

/**
 * Converts a DateTimeObject into a string with format DD/MM/YYYY-hh:mm:ss.
 *
 * @param dt a DateTime object/*ww  w. j a  v a 2 s  .co  m*/
 * @return the formatted String or null
 */
public static String formatDDMMYYYYhhmmss(DateTime dt) {
    if (dt != null) {
        DateTimeFormatterBuilder dtfb = new DateTimeFormatterBuilder();
        dtfb.appendDayOfMonth(2);
        dtfb.appendLiteral('/');
        dtfb.appendMonthOfYear(2);
        dtfb.appendLiteral('/');
        dtfb.appendYear(4, 4);
        dtfb.appendLiteral('-');
        dtfb.appendHourOfDay(2);
        dtfb.appendLiteral(':');
        dtfb.appendMinuteOfHour(2);
        dtfb.appendLiteral(':');
        dtfb.appendSecondOfMinute(2);
        DateTimeFormatter dtf = dtfb.toFormatter();
        String str = dt.toString(dtf);
        return str;
    } else {
        return null;
    }
}

From source file:JodaDT.java

License:Open Source License

/**
 * Converts a DateTimeObject into a string with format DD/MM/YYYY-hh:mm.
 *
 * @param dt a DateTime object/*from  ww  w .  j a v a2  s  .  c  om*/
 * @return the formatted String or null
 */
public static String formatDDMMYYYYhhmm(DateTime dt) {
    if (dt != null) {
        DateTimeFormatterBuilder dtfb = new DateTimeFormatterBuilder();
        dtfb.appendDayOfMonth(2);
        dtfb.appendLiteral('/');
        dtfb.appendMonthOfYear(2);
        dtfb.appendLiteral('/');
        dtfb.appendYear(4, 4);
        dtfb.appendLiteral('-');
        dtfb.appendHourOfDay(2);
        dtfb.appendLiteral(':');
        dtfb.appendMinuteOfHour(2);
        DateTimeFormatter dtf = dtfb.toFormatter();
        String str = dt.toString(dtf);
        return str;
    } else {
        return null;
    }
}

From source file:JodaDT.java

License:Open Source License

/**
 * Converts a DateTimeObject into a string with format DD/MM/YYYY-hh.
 *
 * @param dt a DateTime object//from w w w .  ja v a2s .co  m
 * @return the formatted String or null
 */
public static String formatDDMMYYYYhh(DateTime dt) {
    if (dt != null) {
        DateTimeFormatterBuilder dtfb = new DateTimeFormatterBuilder();
        dtfb.appendDayOfMonth(2);
        dtfb.appendLiteral('/');
        dtfb.appendMonthOfYear(2);
        dtfb.appendLiteral('/');
        dtfb.appendYear(4, 4);
        dtfb.appendLiteral('-');
        dtfb.appendHourOfDay(2);
        DateTimeFormatter dtf = dtfb.toFormatter();
        String str = dt.toString(dtf);
        return str;
    } else {
        return null;
    }
}

From source file:JodaDT.java

License:Open Source License

/**
 * Converts a DateTimeObject into a string with format DD/MM/YYYY
 *
 * @param dt a DateTime object//  w ww  . j av a2 s.c o m
 * @return the formatted String or null
 */
public static String formatDDMMYYYY(DateTime dt) {
    if (dt != null) {
        DateTimeFormatterBuilder dtfb = new DateTimeFormatterBuilder();
        dtfb.appendDayOfMonth(2);
        dtfb.appendLiteral('/');
        dtfb.appendMonthOfYear(2);
        dtfb.appendLiteral('/');
        dtfb.appendYear(4, 4);
        DateTimeFormatter dtf = dtfb.toFormatter();
        String str = dt.toString(dtf);
        return str;
    } else {
        return null;
    }
}

From source file:ch.eitchnet.android.util.JodaHelper.java

License:Open Source License

/**
 * @param timestamp/*ww w  .  ja  va 2 s . c  o  m*/
 * @return
 */
public static String toDateHourMinute(ReadablePartial timestamp) {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    builder.appendYear(4, 4).appendLiteral("-").appendMonthOfYear(2).appendLiteral("-").appendDayOfMonth(2);
    builder.appendLiteral(" ").appendHourOfDay(2).appendLiteral(":").appendMinuteOfHour(2);
    return builder.toFormatter().print(timestamp);
}

From source file:com.clevercloud.bianca.lib.date.DateModule.java

License:Open Source License

/**
 * Returns the parsed date.// w w w.  j av a  2 s  . co m
 */
public Value strptime(Env env, String date, String format) {
    ArrayValueImpl array = new ArrayValueImpl();
    DateTimeFormatterBuilder fb = new DateTimeFormatterBuilder();

    int length = format.length();

    for (int i = 0; i < length; i++) {
        char ch = format.charAt(i);
        if (ch != '%') {
            fb.appendLiteral(ch);
            continue;
        }

        switch (format.charAt(++i)) {
        case 'a':
            fb.appendDayOfWeekShortText();
            break;

        case 'A':
            fb.appendDayOfWeekText();
            break;

        case 'h':
        case 'b':
            fb.appendMonthOfYearShortText();
            ;
            break;

        case 'B':
            fb.appendMonthOfYearText();
            break;

        // TODO: case 'c'

        case 'C':
            fb.appendCenturyOfEra(2, 2);
            break;

        case 'd':
            fb.appendDayOfMonth(2);
            break;

        case 'D':
            fb.appendMonthOfYear(2);
            fb.appendLiteral('/');
            fb.appendDayOfMonth(2);
            fb.appendLiteral('/');
            fb.appendYear(2, 2);
            break;

        // TODO: case 'e'

        case 'F':
            fb.appendYear(4, 4);
            fb.appendLiteral('-');
            fb.appendMonthOfYear(2);
            fb.appendLiteral('-');
            fb.appendDayOfMonth(2);
            break;

        // TODO: case 'g'
        // TODO: case 'G'

        case 'H':
            fb.appendHourOfDay(2);
            break;

        case 'I':
            fb.appendHourOfHalfday(2);
            break;

        case 'j':
            fb.appendDayOfYear(3);
            break;

        // TODO: case 'l'

        case 'm':
            fb.appendMonthOfYear(2);
            break;

        case 'M':
            fb.appendMinuteOfHour(2);
            break;

        case 'n':
            fb.appendLiteral("\n");
            break;

        case 'p':
        case 'P':
            fb.appendHalfdayOfDayText();
            break;

        case 'r':
            fb.appendHourOfHalfday(2);
            fb.appendLiteral(':');
            fb.appendMinuteOfHour(2);
            fb.appendLiteral(':');
            fb.appendSecondOfMinute(2);
            fb.appendLiteral(' ');
            fb.appendHalfdayOfDayText();
            break;

        case 'R':
            fb.appendHourOfDay(2);
            fb.appendLiteral(':');
            fb.appendMinuteOfHour(2);
            break;

        // TODO: case 's'

        case 'S':
            fb.appendSecondOfMinute(2);
            break;

        case 't':
            fb.appendLiteral("\t");
            break;

        case 'T':
            fb.appendHourOfDay(2);
            fb.appendLiteral(':');
            fb.appendMinuteOfHour(2);
            fb.appendLiteral(':');
            fb.appendSecondOfMinute(2);
            break;

        // TODO: case 'u'
        // TODO: case 'U'
        // TODO: case 'V'
        // TODO: case 'w'
        // TODO: case 'W'
        // TODO: case 'x'
        // TODO: case 'X'

        case 'y':
            fb.appendYear(2, 2);
            break;

        case 'Y':
            fb.appendYear(4, 4);
            break;

        case 'z':
            fb.appendTimeZoneOffset(null, true, 2, 2);
            break;

        case 'Z':
            fb.appendTimeZoneName();
            break;

        case '%':
            fb.appendLiteral('%');
            break;

        default:
            fb.appendLiteral(ch);
        }
    }

    DateTimeFormatter dtf = fb.toFormatter().withLocale(Locale.getDefault()).withOffsetParsed();

    org.joda.time.DateTime dt = new org.joda.time.DateTime();

    String unparsed = "";

    try {
        dt = dtf.parseDateTime(date);
    } catch (IllegalArgumentException e) {
        String delims = "[\"]+";

        String[] splits = e.getMessage().split(delims);

        unparsed = unparsed.concat(splits[3]);
    }

    // According to manual strptime(3)
    if (dt.getCenturyOfEra() == 0) {
        if (dt.getYear() > 68) {
            dt = dt.withCenturyOfEra(19);
        } else {
            dt = dt.withCenturyOfEra(20);
        }
    }

    array.put("tm_sec", dt.getSecondOfMinute());
    array.put("tm_min", dt.getMinuteOfHour());
    array.put("tm_hour", dt.getHourOfDay());
    array.put("tm_mday", dt.getDayOfMonth());
    array.put("tm_mon", dt.getMonthOfYear() - 1);
    array.put("tm_year", dt.getYearOfCentury() + ((dt.getCenturyOfEra() - 19) * 100)); // Years since 1900
    array.put("tm_wday", dt.getDayOfWeek() % 7);
    array.put("tm_yday", dt.getDayOfYear() - 1);
    array.put("unparsed", unparsed);

    return array;
}