List of usage examples for org.joda.time.format DateTimeFormatterBuilder appendTimeZoneName
public DateTimeFormatterBuilder appendTimeZoneName()
From source file:com.clevercloud.bianca.lib.date.DateModule.java
License:Open Source License
/** * Returns the parsed date./* w w w . ja v a 2 s . com*/ */ 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; }
From source file:com.miraisolutions.xlconnect.utils.RPOSIXDateTimeFormatter.java
License:Open Source License
private org.joda.time.format.DateTimeFormatter getFormatter(String format) { if (cache.containsKey(format)) return cache.get(format); DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder(); for (int i = 0; i < format.length(); ++i) { if (format.charAt(i) == '%' && i + 1 < format.length()) { char specifier = format.charAt(++i); switch (specifier) { case '%': builder.appendLiteral("%"); break; case 'a': // Abbreviated weekday name in the current locale. (Also matches // full name on input.) builder.appendDayOfWeekShortText(); break; case 'A': // Full weekday name in the current locale. (Also matches // abbreviated name on input.) builder.appendDayOfWeekText(); break; case 'b': // Abbreviated month name in the current locale. (Also matches // full name on input.) builder.appendMonthOfYearShortText(); break; case 'B': // Full month name in the current locale. (Also matches // abbreviated name on input.) builder.appendMonthOfYearText(); break; case 'c': // Date and time. Locale-specific on output, "%a %b %e // %H:%M:%S %Y" on input. throw new UnsupportedOperationException("%c not yet implemented"); case 'd': // Day of the month as decimal number (01-31). builder.appendDayOfMonth(2); break; case 'H': // Hours as decimal number (00-23). builder.appendHourOfDay(2); break; case 'I': // Hours as decimal number (01-12). builder.appendHourOfHalfday(2); break; case 'j': // Day of year as decimal number (001-366). builder.appendDayOfYear(3); break; case 'm': // Month as decimal number (01-12). builder.appendMonthOfYear(2); break; case 'M': // Minute as decimal number (00-59). builder.appendMinuteOfHour(2); break; case 'p': // AM/PM indicator in the locale. Used in conjunction with %I // and *not* with %H. An empty string in some locales. builder.appendHalfdayOfDayText(); break; case 'O': if (i + 1 >= format.length()) { builder.appendLiteral("%O"); } else { switch (format.charAt(++i)) { case 'S': // Specific to R is %OSn, which for output gives the seconds to 0 // <= n <= 6 decimal places (and if %OS is not followed by a // digit, it uses the setting of getOption("digits.secs"), or if // that is unset, n = 3). Further, for strptime %OS will input // seconds including fractional seconds. Note that %S ignores (and // not rounds) fractional parts on output. // TODO: not sure how to handle fractional seconds here builder.appendSecondOfMinute(2); break; default: throw new UnsupportedOperationException("%O[dHImMUVwWy] not yet implemented"); }/*from ww w.j a v a2 s .c o m*/ } break; case 'S': // Second as decimal number (00-61), allowing for up to two // leap-seconds (but POSIX-compliant implementations will ignore // leap seconds). // TODO: I have no idea what the docs are talking about in relation // to leap seconds builder.appendSecondOfMinute(2); break; // case 'U': // Week of the year as decimal number (00-53) using Sunday as // the first day 1 of the week (and typically with the first // Sunday of the year as day 1 of week 1). The US convention. // case 'w': // Weekday as decimal number (0-6, Sunday is 0). // case 'W': // Week of the year as decimal number (00-53) using Monday as // the first day of week (and typically with the first Monday of // the year as day 1 of week 1). The UK convention. // %x Date. Locale-specific on output, "%y/%m/%d" on input. //%X Time. Locale-specific on output, "%H:%M:%S" on input. case 'y': // Year without century (00-99). Values 00 to 68 are prefixed by // 20 and 69 to 99 by 19 - that is the behaviour specified by // the 2004 POSIX standard, but it does also say it is expected // that in a future version the default century inferred from a // 2-digit year will change. builder.appendTwoDigitYear(1968, true); break; case 'Y': // Year with century builder.appendYear(1, 4); break; case 'z': // Signed offset in hours and minutes from UTC, so -0800 is 8 // hours behind UTC. builder.appendTimeZoneOffset(null /* always show offset, even when zero */, true /* show seperators */, 1 /* min fields (hour, minute, etc) */, 2 /* max fields */ ); break; case 'Z': // (output only.) Time zone as a character string (empty if not // available). builder.appendTimeZoneName(); break; default: throw new UnsupportedOperationException("%" + specifier + " not yet implemented"); } } else { builder.appendLiteral(format.substring(i, i + 1)); } } org.joda.time.format.DateTimeFormatter formatter = builder.toFormatter(); cache.put(format, formatter); return formatter; }