List of usage examples for org.joda.time.format DateTimeFormat forPattern
public static DateTimeFormatter forPattern(String pattern)
From source file:com.garethahealy.elasticpostman.scraper.entities.EmailContent.java
License:Apache License
public Map<String, Object> toMap() { Map<String, Object> map = new HashMap<String, Object>(); map.put("from", from); map.put("subject", subject); map.put("content", content); map.put("contentIds", contentIds); map.put("sentDate", DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").print(sentDate)); map.put("headers", headers); return map;/*from ww w .j a va2 s .c o m*/ }
From source file:com.garethahealy.elastoplast.elasticloader.processors.JsonParserProcessor.java
License:Apache License
@Override public void process(Exchange exchange) throws Exception { ObjectMapper objectMapper = new ObjectMapper(); List<Map<String, String>> data = objectMapper.readValue(exchange.getIn().getBody(String.class), TYPE_REF); for (Map<String, String> line : data) { DateTime date = new DateTime(Long.parseLong(line.get("timeMillis"))); String value = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").print(date); line.put("@timestamp", value); }// ww w.java 2 s . co m exchange.getIn().setBody(data); }
From source file:com.github.dbourdette.otto.source.Event.java
License:Apache License
public Event parseValue(String key, String value) { if (StringUtils.startsWith(key, INTEGER_PREFIX)) { key = StringUtils.substringAfter(key, INTEGER_PREFIX); putInt(key, Integer.parseInt(value)); } else if (StringUtils.startsWith(key, BOOLEAN_PREFIX)) { key = StringUtils.substringAfter(key, BOOLEAN_PREFIX); putBoolean(key, Boolean.valueOf(value)); } else if (StringUtils.startsWith(key, DATE_PREFIX)) { key = StringUtils.substringAfter(key, DATE_PREFIX); putDate(key, DateTimeFormat.forPattern(DATE_PATTERN).parseDateTime(value)); } else {/* w ww. j av a 2 s.c o m*/ putString(key, value); } return this; }
From source file:com.github.fauu.natrank.service.MatchDataImportServiceImpl.java
License:Open Source License
@Override public ProcessedMatchData processMatchData(String rawMatchData) { ProcessedMatchData matchData = new ProcessedMatchData(); DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("dd/MM/yyyy"); int lineNo = 1; BufferedReader reader = new BufferedReader(new StringReader(rawMatchData)); try {/* w ww . j ava 2s . c om*/ String line; while ((line = reader.readLine()) != null) { String[] splitLine = line.split(";"); int numFields = splitLine.length; if (numFields == 6) { for (int i = 0; i < numFields; i++) { if (splitLine[i] == null || splitLine[i].trim().length() == 0) { MatchDataError error = new MatchDataError(lineNo, line, MatchDataError.Type.ERROR_MISSING_FIELD); matchData.getErrors().add(error); } } ParsedRawMatchDatum match = new ParsedRawMatchDatum(); LocalDate matchDate; try { matchDate = dateTimeFormatter.parseLocalDate(splitLine[0]); } catch (IllegalArgumentException e) { MatchDataError error = new MatchDataError(lineNo, line, MatchDataError.Type.ERROR_INCORRECT_DATE_FORMAT); matchData.getErrors().add(error); e.printStackTrace(); matchDate = null; } String matchType = splitLine[1]; String matchCity = splitLine[2]; String matchTeam1 = splitLine[3]; String matchTeam2 = splitLine[4]; String matchResult = splitLine[5]; Country team1Country = countryRepository.findByName(matchTeam1); Country team2Country = countryRepository.findByName(matchTeam2); if ((team1Country != null) && (team2Country != null)) { List<Match> duplicates = matchRepository.findByDateAndTeam1AndTeam2(matchDate, team1Country.getTeam(), team2Country.getTeam()); if (duplicates.size() > 0) { continue; } } match.setDate(matchDate); match.setType(matchType); match.setCity(matchCity); match.setTeam1(matchTeam1); match.setTeam2(matchTeam2); match.setResult(matchResult); matchData.getMatches().add(match); } else { MatchDataError error = new MatchDataError(lineNo, line, MatchDataError.Type.ERROR_INCORRECT_LINE_FORMAT); matchData.getErrors().add(error); } lineNo++; } } catch (IOException e) { e.printStackTrace(); } finally { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } if (matchData.getErrors().size() == 0) { List<String> existingCountryNames = countryRepository.findAllNames(); Set<String> processedNewCountryNames = new HashSet<>(); List<String> existingCityNames = cityRepository.findAllNames(); Set<String> processedNewCityNames = new HashSet<>(); List<String> existingMatchTypesFifaNames = matchTypeRepository.findAllFifaNames(); Set<String> processedNewMatchTypeFifaNames = new HashSet<>(); List<String> countryNamesTemp = new LinkedList<>(); for (ParsedRawMatchDatum parsedRawMatchDatum : matchData.getMatches()) { String matchTypeFifaName = parsedRawMatchDatum.getType(); if (!processedNewMatchTypeFifaNames.contains(matchTypeFifaName) && !existingMatchTypesFifaNames.contains(matchTypeFifaName)) { MatchType newType = new MatchType(); newType.setFifaName(matchTypeFifaName); processedNewMatchTypeFifaNames.add(matchTypeFifaName); matchData.getTypes().add(newType); } countryNamesTemp.add(parsedRawMatchDatum.getTeam1()); countryNamesTemp.add(parsedRawMatchDatum.getTeam2()); for (String countryName : countryNamesTemp) { if (!processedNewCountryNames.contains(countryName) && !existingCountryNames.contains(countryName)) { Country newCountry = new Country(); newCountry.setName(countryName); newCountry.setPeriod(new Period()); newCountry.getPeriod().setFromDate(parsedRawMatchDatum.getDate()); List<CountryCode> matchingCountryCodes = countryCodeRepository .findByCountryName(newCountry.getName()); String inferredCountryCode = ""; if (matchingCountryCodes.size() > 0) { inferredCountryCode = matchingCountryCodes.get(0).getCode(); } newCountry.setCode(inferredCountryCode); processedNewCountryNames.add(newCountry.getName()); matchData.getCountries().add(newCountry); } } countryNamesTemp.clear(); String cityName = parsedRawMatchDatum.getCity(); if (!processedNewCityNames.contains(cityName) && !existingCityNames.contains(cityName)) { City newCity = new City(); newCity.setName(cityName); CityCountryAssoc cityCountryAssoc = new CityCountryAssoc(); cityCountryAssoc.setCity(newCity); cityCountryAssoc.setPeriod(new Period()); cityCountryAssoc.getPeriod().setFromDate(parsedRawMatchDatum.getDate()); newCity.getCityCountryAssocs().add(cityCountryAssoc); processedNewCityNames.add(cityName); matchData.getCities().add(newCity); matchData.getCitiesInferredCountryNames().add(parsedRawMatchDatum.getTeam1()); } } } return matchData; }
From source file:com.github.fge.jsonschema.format.helpers.DateFormatAttribute.java
License:Open Source License
protected DateFormatAttribute(final String fmt, final String format) { super(fmt, NodeType.STRING); this.format = format; formatter = DateTimeFormat.forPattern(format); }
From source file:com.github.flawedbliss.dicebotr3.DicebotR3.java
License:Open Source License
private void updateLastlogin(String uid) { Connection connect = null;// w w w . ja va 2s. co m PreparedStatement preparedStatement = null; PreparedStatement preparedStatement2 = null; ResultSet resultSet = null; DateTime date = new DateTime(new Date()); DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"); try { connect = cp.getConnection(); preparedStatement = connect.prepareStatement("SELECT * FROM lastlogin WHERE uid=?"); preparedStatement.setString(1, uid); resultSet = preparedStatement.executeQuery(); if (resultSet.isBeforeFirst()) { preparedStatement2 = connect.prepareStatement("UPDATE lastlogin SET lastlogin=? WHERE " + "uid=?"); preparedStatement2.setString(1, dtf.print(date)); preparedStatement2.setString(2, uid); preparedStatement2.executeUpdate(); } else { preparedStatement2 = connect .prepareStatement("INSERT INTO lastlogin (uid, lastlogin)" + " VALUES (?, ?)"); preparedStatement2.setString(1, uid); preparedStatement2.setString(2, dtf.print(date)); preparedStatement2.executeUpdate(); } preparedStatement2.close(); resultSet.close(); preparedStatement.close(); connect.close(); } catch (SQLException ex) { log.warning("SQLException when trying to update lastlogin of " + uid); } }
From source file:com.github.flawedbliss.dicebotr3.DicebotR3.java
License:Open Source License
private boolean addNotice(Notice notice) { Connection connect = null;//ww w. j a va2 s . c om PreparedStatement preparedStatement = null; DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"); try { connect = cp.getConnection(); preparedStatement = connect.prepareStatement("INSERT INTO " + cfg.getMysqlNoticeTable() + "(message, expirationDate, special) VALUES (?, ?, ?)"); preparedStatement.setString(1, notice.getMessage()); preparedStatement.setString(2, dtf.print(notice.getExpirationDate())); preparedStatement.setBoolean(3, notice.isSpecial()); preparedStatement.executeUpdate(); preparedStatement.close(); connect.close(); return true; } catch (SQLException ex) { log.warning("SQLException while trying to add a new notice."); return false; } }
From source file:com.github.flawedbliss.dicebotr4.MySqlManager.java
License:Open Source License
/** * * @param uid Unique ID of the user whose lastlogin entry should be * added/updated//from w w w .j a va 2s . com * @throws DicebotException */ public void updateLastLogin(String uid) throws DicebotException { DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"); try (Connection connect = cp.getConnection()) { String sql; PreparedStatement statement; DateTime now = new DateTime(new Date()); DateTime lastlogin = getLastLogin(uid); if (lastlogin == null) { sql = "INSERT INTO lastlogin (uid, lastlogin) VALUES (?, ?)"; statement = connect.prepareStatement(sql); statement.setString(0, uid); statement.setString(1, dtf.print(now)); statement.executeUpdate(); } else { sql = "UPDATE lastlogin SET lastlogin=? WHERE uid=?"; statement = connect.prepareStatement(sql); statement.setString(0, dtf.print(now)); statement.setString(1, uid); statement.executeUpdate(); } statement.close(); } catch (SQLException ex) { throw new DicebotException("Could not insert/update lastlogin for " + uid + " . Ignoring."); } }
From source file:com.github.flawedbliss.dicebotr4.MySqlManager.java
License:Open Source License
/** * /*from w w w. ja va 2 s. co m*/ * @param code The primary key (code) where the used column used should be updated * @param uid Unique Id of the user who used the code * @throws DicebotException */ public void setCodeUsed(String code, String uid) throws DicebotException { String sql = "UPDATE premium SET used=1, used_by=?, date_used=? WHERE code=?"; DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"); try (Connection connect = cp.getConnection(); PreparedStatement statement = connect.prepareStatement(sql);) { statement.setString(0, uid); statement.setString(1, dtf.print(new DateTime(new Date()))); statement.setString(2, code); statement.executeUpdate(); } catch (SQLException ex) { if (ex.getMessage() != null) { throw new DicebotException("Unable to set code used for code " + code + "\n" + ex.getMessage()); } else { throw new DicebotException("Unable to set code used for " + code + ". No further information."); } } }
From source file:com.github.pockethub.android.util.TimeUtils.java
License:Apache License
/** * Convert string datetime in UTC to local datetime. * @param value The datetime in UTC to parse. * @return Local datetime.// ww w . j a va 2s . c om */ public static Date stringToDate(String value) { DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"); DateTime t = format.withZoneUTC().parseDateTime(value); return t.toDate(); }