List of usage examples for org.joda.time.format DateTimeFormatter parseDateTime
public DateTime parseDateTime(String text)
From source file:org.archive.bacon.ParseTimestamp.java
License:Apache License
public String exec(Tuple input) throws IOException { if (input == null || input.size() == 0) return null; try {/*from www . j av a2s .c o m*/ String date = (String) input.get(0); int len = date.length(); String format = null; if (len == 12) format = "YYYYMMddHHmm"; if (len == 14) format = "YYYYMMddHHmmss"; if (len == 20) format = "YYYY-MM-dd'T'HH:mm:ss'Z'"; if (format == null) return null; // Unknown format. // Set the time to default or the output is in UTC DateTimeZone.setDefault(DateTimeZone.UTC); // See http://joda-time.sourceforge.net/api-release/org/joda/time/format/DateTimeFormat.html DateTimeFormatter parser = DateTimeFormat.forPattern(format); DateTime result = parser.parseDateTime(date); return result.toString(); } catch (Exception e) { // If we have any problems parsing the date, just return null; return null; } }
From source file:org.arrow.util.TriggerUtils.java
License:Apache License
/** * Indicates if the given scheduler string is in ISO8601 format. * /*w w w . j av a 2s. c o m*/ * @param str the ISO 8601 string * @return boolean */ public static boolean isIso8601(String str) { try { DateTimeFormatter fmt = ISODateTimeFormat.dateTime(); fmt.parseDateTime(str); return true; } catch (IllegalArgumentException ex) { return false; } }
From source file:org.basepom.mojo.propertyhelper.DateField.java
License:Apache License
private DateTime getDateTime(final Optional<String> value, final DateTimeFormatter formatter, final DateTimeZone timeZone) { if (!value.isPresent()) { return null; }//from ww w .j a v a 2s . com if (formatter != null) { return formatter.parseDateTime(value.get()).withZone(timeZone); } try { return new DateTime(Long.parseLong(value.get()), timeZone); } catch (NumberFormatException nfe) { return new DateTime(value.get(), timeZone); } }
From source file:org.belio.service.producer.OutboxEntryParser.java
@Override public Outbox parseEntry(String... strings) { Outbox outbox = new Outbox(); try {//from ww w . jav a 2 s . c o m // String[] strings = string[0].split(","); // System.out.println(strings[6]); DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS"); outbox.setAlertsId(Long.valueOf(strings[0])); outbox.setShortCode(strings[1]); outbox.setMsisdn(Long.valueOf(strings[2])); outbox.setText(URLDecoder.decode(strings[3], "UTF-8")); outbox.setPriority(Integer.valueOf(strings[4])); outbox.setNetwork(strings[5]); outbox.setSdpId(strings[6]); outbox.setRefNo(Integer.valueOf(strings[7])); // switch (strings[2]) { // case "CONTENT": // outbox.setQueueType(QueueType.CONTENT); // break; // case "TEXT": // outbox.setQueueType(QueueType.TEXT); // break; // case "BULK": // outbox.setQueueType(QueueType.BULK); // break; // case "DATING": // outbox.setQueueType(QueueType.DATING); // break; // case "SERVICE": // outbox.setQueueType(QueueType.SERVICE); // break; // } outbox.setCreated(new Timestamp(formatter.parseDateTime(strings[8]).getMillis())); outbox.setDateCreated(new Timestamp(formatter.parseDateTime(strings[9]).getMillis())); outbox.setModified(new Timestamp(formatter.parseDateTime(strings[10]).getMillis())); // outbox.setServiceID(String.valueOf(alert.getServiceId())); } catch (UnsupportedEncodingException ex) { Logger.getLogger(OutboxEntryParser.class.getName()).log(Level.SEVERE, null, ex); } return outbox; }
From source file:org.brekka.stillingar.core.conversion.TemporalAdapter.java
License:Apache License
public Calendar toCalendar(Object obj, boolean supportsDate, boolean supportsTime, Class<?> expectedType) { Calendar value;//from w w w . j a v a2 s . c o m if (obj instanceof Calendar) { value = (Calendar) obj; } else if (obj instanceof Date) { value = Calendar.getInstance(); value.setTime((Date) obj); } else if (obj instanceof String) { String dateTimeStr = (String) obj; if (!jodaTimeAvailable) { throw new IllegalArgumentException(format( "Date/time conversion unavailable for value '%s' to '%s'." + " Add JodaTime library to enable default ISO conversion.", dateTimeStr, expectedType.getName())); } if (dateTimeStr.length() < 14 && dateTimeStr.endsWith("Z")) { dateTimeStr = dateTimeStr.substring(0, dateTimeStr.length() - 1); } org.joda.time.format.DateTimeFormatter dateTimeParser; if (dateTimeStr.charAt(2) == ':') { dateTimeParser = org.joda.time.format.ISODateTimeFormat.timeParser(); } else { dateTimeParser = org.joda.time.format.ISODateTimeFormat.dateTimeParser(); } org.joda.time.DateTime dateTime = dateTimeParser.parseDateTime(dateTimeStr); value = dateTime.toCalendar(null); if (!supportsDate) { value.clear(Calendar.YEAR); value.clear(Calendar.MONTH); value.clear(Calendar.DAY_OF_MONTH); } if (!supportsTime) { value.clear(Calendar.HOUR_OF_DAY); value.clear(Calendar.MINUTE); value.clear(Calendar.SECOND); value.clear(Calendar.MILLISECOND); } } else { throw new IllegalArgumentException( format("No temporal conversion available for value of type '%s' to '%s'.", obj.getClass().getName(), expectedType.getName())); } return value; }
From source file:org.classbooker.presentation.controller.GetUserReservationsAction.java
private void initializeParametersFromForm() { nif = form.nif.getText();// ww w. ja va 2 s. com capacity = 0; if (!form.capacity.getText().isEmpty()) { capacity = Integer.parseInt(form.capacity.getText()); } buildingName = form.buildingName.getText(); if (form.buildingName.getText().isEmpty()) { buildingName = null; } roomNb = form.roomNb.getText(); if (form.roomNb.getText().isEmpty()) { roomNb = null; } dateIni = null; if (!form.dateIni.getText().isEmpty()) { DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MM-yyyy HH:mm"); dateIni = formatter.parseDateTime(form.dateIni.getText()); } type = form.type.getText(); if (form.type.getText().isEmpty()) { type = null; } }
From source file:org.classbooker.presentation.controller.SubmitMakeReservationByTypeAction.java
public void actionPerformed(ActionEvent e) { if ("".equals(reservationByTypeInsertionForm.nif.getText())) { JOptionPane.showMessageDialog(null, "Please, Can you put user nif?", "Warning", JOptionPane.WARNING_MESSAGE); }/*from ww w . j a v a2 s . co m*/ String nif = reservationByTypeInsertionForm.nif.getText(); if ("".equals(reservationByTypeInsertionForm.type.getText())) { JOptionPane.showMessageDialog(null, "Please, Can you put the type of room?", "Warning", JOptionPane.WARNING_MESSAGE); } String type = reservationByTypeInsertionForm.type.getText(); if ("".equals(reservationByTypeInsertionForm.buildingName.getText())) { JOptionPane.showMessageDialog(null, "Please, Can you put building name?", "Warning", JOptionPane.WARNING_MESSAGE); } String buildingName = reservationByTypeInsertionForm.buildingName.getText(); if ("".equals(reservationByTypeInsertionForm.capacity.getText())) { JOptionPane.showMessageDialog(null, "Please, Can you put a capacity?", "Warning", JOptionPane.WARNING_MESSAGE); } int capacity = Integer.parseInt(reservationByTypeInsertionForm.capacity.getText()); DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MM-yyyy HH:mm"); if ("".equals(reservationByTypeInsertionForm.dateIni.getText())) { JOptionPane.showMessageDialog(null, "Please, Can you put a Date?", "Warning", JOptionPane.WARNING_MESSAGE); } DateTime dateIni = formatter.parseDateTime(reservationByTypeInsertionForm.dateIni.getText()); reservationByTypeInsertionForm.parent.getContentPane().removeAll(); try { // Reservation makeReservationByType(String nif, String type, String buildingName, int capacity, DateTime date) Reservation reservation = services.makeReservationByType(nif, type, buildingName, capacity, dateIni); // ConfirmationForm confirm = new ConfirmationForm("Add Reservation"); // reservationByTypeInsertionForm.parent.getContentPane().add(confirm,BorderLayout.CENTER); reservationByTypeInsertionForm.parent.getContentPane().removeAll(); AcceptReservationByTypeForm form = new AcceptReservationByTypeForm(parent, reservation, services); reservationByTypeInsertionForm.parent.getContentPane().add(form, BorderLayout.CENTER); reservationByTypeInsertionForm.parent.revalidate(); } catch (IncorrectBuildingException exc) { JOptionPane.showMessageDialog(null, "The Name of Building is incorrect. Please, write again", "Warning", JOptionPane.WARNING_MESSAGE); } catch (IncorrectCapacityException exc) { JOptionPane.showMessageDialog(null, "The Capacity is incorrect. Please, write again", "Warning", JOptionPane.WARNING_MESSAGE); } catch (IncorrectTypeException exc) { JOptionPane.showMessageDialog(null, "The Type of the room is incorrect. Please, write again", "Warning", JOptionPane.WARNING_MESSAGE); } catch (DAOException exc) { //AlreadyExistingBuildingException exc){ exc.printStackTrace(); ExceptionInfo exception = new ExceptionInfo("Something is going wrong! Ups!"); reservationByTypeInsertionForm.parent.getContentPane().add(exception, BorderLayout.CENTER); } finally { reservationByTypeInsertionForm.parent.revalidate(); } }
From source file:org.codehaus.httpcache4j.HeaderUtils.java
License:Open Source License
public static DateTime fromHttpDate(Header header) { if (header == null) { return null; }//from w w w .j a v a 2 s . c o m if ("0".equals(header.getValue().trim())) { return new DateTime(1970, 1, 1, 0, 0, 0, 0).withZone(DateTimeZone.forID("UTC")); } DateTimeFormatter formatter = DateTimeFormat.forPattern(PATTERN_RFC1123).withZone(DateTimeZone.forID("UTC")) .withLocale(Locale.US); DateTime formattedDate = null; try { formattedDate = formatter.parseDateTime(header.getValue()); } catch (IllegalArgumentException ignore) { } return formattedDate; }
From source file:org.conqat.engine.bugzilla.lib.Bug.java
License:Apache License
/** Get milliseconds of an enumeration field that is holding a date. */ public long getMilliSeconds(EBugzillaField field) { // TODO (BH): Why variable here? long milliSeconds = 0; // TODO (BH): I would invert the condition and return/throw here to // reduce the nesting. if (fields.get(field) != null) { // TODO (BH): Why store value and overwrite in next line? You could // also move this outside of the if and use the variable in the if // expression. String bugzillaDate = StringUtils.EMPTY_STRING; bugzillaDate = fields.get(field); // TODO (BH): Make constants from these pattern Pattern todayPattern = Pattern.compile("[0-9]{2}:[0-9]{2}:[0-9]{2}"); Pattern lastWeekPattern = Pattern.compile("[A-Z][a-z][a-z] [0-9]{2}:[0-9]{2}"); Pattern anyDatePattern = Pattern.compile("[0-9]{4}-[0-9]{2}-[0-9]{2}"); // TODO (BH): Variables only used once. Inline? Matcher todayMatcher = todayPattern.matcher(bugzillaDate); Matcher lastWeekMatcher = lastWeekPattern.matcher(bugzillaDate); Matcher anyDateMatcher = anyDatePattern.matcher(bugzillaDate); if (anyDateMatcher.matches()) { // TODO (BH): Make this a constant? DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd"); // TODO (BH): Directly return? milliSeconds = dateTimeFormatter.parseDateTime(bugzillaDate).getMillis(); } else if (lastWeekMatcher.matches()) { DateTime lastWeekDate = new DateTime(Chronic.parse(bugzillaDate).getBeginCalendar().getTime()); // Since jchronic parses the Bugzilla format exactly seven days // to late, we need to subtract those 7 days. // TODO (BH): Directly return? milliSeconds = lastWeekDate.minusDays(7).getMillis(); } else if (todayMatcher.matches()) { DateTime todayDate = new DateTime(); // TODO (BH): Make this a constant? DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("HH:mm:ss"); DateTime fieldDate = dateTimeFormatter.parseDateTime(bugzillaDate); // TODO (BH): Directly return? milliSeconds = new DateTime(todayDate.getYear(), todayDate.getMonthOfYear(), todayDate.getDayOfMonth(), fieldDate.getHourOfDay(), fieldDate.getMinuteOfHour(), fieldDate.getSecondOfMinute()).getMillis(); } else {/*from ww w. j a v a 2 s . co m*/ // TODO (BH): I think this is not a good way of handling this // error as the argument might be valid, but the data is just // not good. Better use a checked exception, such as // ConQATException. throw new IllegalArgumentException("Field is not a Bugzilla date."); } } else { // TODO (BH): I think this is not a good way of handling this error // as the argument might be valid, but the data is just not present. // Better use a checked exception, such as ConQATException. throw new IllegalArgumentException("Argument is not a Bugzilla field."); } return milliSeconds; }
From source file:org.craftercms.search.service.impl.DateTimeConverter.java
License:Open Source License
@Override public Object convert(String name, String value) { DateTimeFormatter incomingFormatter = DateTimeFormat.forPattern(dateTimeFieldPattern).withZoneUTC(); DateTimeFormatter outgoingFormatter = ISODateTimeFormat.dateTime(); return outgoingFormatter.print(incomingFormatter.parseDateTime(value)); }