List of usage examples for org.joda.time DateTime year
public Property year()
From source file:com.addthis.hydra.task.source.AbstractPersistentStreamSource.java
License:Apache License
/** */ private String replaceDateElements(DateTime time, String template) { template = template.replace("{YY}", time.year().getAsString()); template = template.replace("{Y}", getTwoDigit(time.year().get())); template = template.replace("{M}", getTwoDigit(time.monthOfYear().get())); template = template.replace("{D}", getTwoDigit(time.dayOfMonth().get())); template = template.replace("{H}", getTwoDigit(time.hourOfDay().get())); if (log.isDebugEnabled()) { log.debug("template=" + template); }//from w ww . j ava2 s . c o m return template; }
From source file:com.certus.actions.getChartExpencesAction.java
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String requestString = request.getParameter("expenses"); //String requestString = "year"; DateTime today = new DateTime(); GsonBuilder gsonBuilder = new GsonBuilder(); Gson gson = gsonBuilder.registerTypeAdapter(ChartExpenses.class, new ChartExpenseAdapter()).create(); switch (requestString) { case "week": DateTime startOfWeek = today.weekOfWeekyear().roundFloorCopy(); DateTime endOfWeek = today.weekOfWeekyear().roundCeilingCopy(); Map<String, Double> mapWeekSale = new ReportsController().getSalesByWeek(startOfWeek.toDate(), endOfWeek.toDate());/*from ww w . j a v a 2 s .co m*/ Map<String, Double> sortedMapWeekSale = new TreeMap<>(ReportsController.DAYS_OF_WEEK_COMPARATOR); sortedMapWeekSale.putAll(mapWeekSale); Map<String, Double> mapWeekExpense = new ReportsController().getExpenesByWeek(startOfWeek.toDate(), endOfWeek.toDate()); Map<String, Double> sortedMapWeekExpense = new TreeMap<>(ReportsController.DAYS_OF_WEEK_COMPARATOR); sortedMapWeekExpense.putAll(mapWeekExpense); if (mapWeekExpense.size() != mapWeekSale.size()) { try { throw new Exception("Invalid size exception"); } catch (Exception ex) { Logger.getLogger(getChartExpencesAction.class.getName()).log(Level.SEVERE, null, ex); } } Iterator itSale = mapWeekSale.entrySet().iterator(); Iterator itExp = mapWeekExpense.entrySet().iterator(); List<ChartExpenses> expList = new ArrayList<>(); while (itSale.hasNext() || itExp.hasNext()) { Map.Entry<String, Double> sale = null; Map.Entry<String, Double> exp = null; if (itSale.hasNext()) { sale = (Map.Entry<String, Double>) itSale.next(); } if (itExp.hasNext()) { exp = (Map.Entry<String, Double>) itExp.next(); } if (sale != null && exp != null) { if (sale.getKey().equals(exp.getKey())) { expList.add(new ChartExpenses(sale.getKey(), sale.getValue(), exp.getValue())); } } } expList.sort(new DaysComparator()); String elementWeek = gson.toJson(expList); response.setContentType("application/json"); response.getWriter().write(elementWeek); break; case "month": DateTime startOfMonth = today.monthOfYear().roundFloorCopy(); DateTime endOfMonth = today.monthOfYear().roundCeilingCopy(); Map<String, Double> mapMonthSale = new ReportsController().getSalesByMonth(startOfMonth.toDate(), endOfMonth.toDate()); Map<String, Double> sortedMapMonthSale = new TreeMap<>(ReportsController.DAYS_OF_MONTH_COMPARATOR); sortedMapMonthSale.putAll(mapMonthSale); Map<String, Double> mapMonthExp = new ReportsController().getExpenesByMonth(startOfMonth.toDate(), endOfMonth.toDate()); Map<String, Double> sortedMapMonthExp = new TreeMap<>(ReportsController.DAYS_OF_MONTH_COMPARATOR); sortedMapMonthExp.putAll(mapMonthExp); if (mapMonthSale.size() != mapMonthExp.size()) { try { throw new Exception("Invalid size exception"); } catch (Exception ex) { Logger.getLogger(getChartExpencesAction.class.getName()).log(Level.SEVERE, null, ex); } } Iterator itSaleMonth = mapMonthSale.entrySet().iterator(); Iterator itExpMonth = mapMonthExp.entrySet().iterator(); List<ChartExpenses> expMonthList = new ArrayList<>(); while (itSaleMonth.hasNext() || itExpMonth.hasNext()) { Map.Entry<String, Double> sale = null; Map.Entry<String, Double> exp = null; if (itSaleMonth.hasNext()) { sale = (Map.Entry<String, Double>) itSaleMonth.next(); } if (itExpMonth.hasNext()) { exp = (Map.Entry<String, Double>) itExpMonth.next(); } if (sale != null && exp != null) { if (sale.getKey().equals(exp.getKey())) { expMonthList.add(new ChartExpenses(sale.getKey(), sale.getValue(), exp.getValue())); } } } expMonthList.sort(new MonthDaysComparator()); String elementMonth = gson.toJson(expMonthList); response.setContentType("application/json"); response.getWriter().write(elementMonth); break; case "year": DateTime startOfYear = today.year().roundFloorCopy(); DateTime endOfYear = today.year().roundCeilingCopy(); Map<String, Double> mapYearSale = new ReportsController().getSalesByYear(startOfYear.toDate(), endOfYear.toDate()); Map<String, Double> sortedMapYear = new TreeMap<>(ReportsController.MONTH_OF_YEAR_COMPARATOR); sortedMapYear.putAll(mapYearSale); Map<String, Double> mapYearExp = new ReportsController().getExpenesByYear(startOfYear.toDate(), endOfYear.toDate()); Map<String, Double> sortedMapYearExp = new TreeMap<>(ReportsController.MONTH_OF_YEAR_COMPARATOR); sortedMapYearExp.putAll(mapYearExp); if (mapYearExp.size() != mapYearSale.size()) { try { throw new Exception("Invalid size exception"); } catch (Exception ex) { Logger.getLogger(getChartExpencesAction.class.getName()).log(Level.SEVERE, null, ex); } } Iterator itSaleYear = mapYearSale.entrySet().iterator(); Iterator itExpYear = mapYearExp.entrySet().iterator(); List<ChartExpenses> expYearList = new ArrayList<>(); while (itSaleYear.hasNext() || itExpYear.hasNext()) { Map.Entry<String, Double> sale = null; Map.Entry<String, Double> exp = null; if (itSaleYear.hasNext()) { sale = (Map.Entry<String, Double>) itSaleYear.next(); } if (itExpYear.hasNext()) { exp = (Map.Entry<String, Double>) itExpYear.next(); } if (sale != null && exp != null) { if (sale.getKey().equals(exp.getKey())) { expYearList.add(new ChartExpenses(sale.getKey(), sale.getValue(), exp.getValue())); } } } expYearList.sort(new YearMonthsComparator()); String elementYear = gson.toJson(expYearList); response.setContentType("application/json"); response.getWriter().write(elementYear); break; default: break; } }
From source file:com.certus.actions.getChartOrdersAction.java
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String requestString = request.getParameter("orders"); DateTime today = new DateTime(); GsonBuilder gsonBuilder = new GsonBuilder(); Gson gson = gsonBuilder.registerTypeAdapter(ChartsCriteria.class, new ChartsCriteriaAdapter()).create(); switch (requestString) { case "week": DateTime startOfWeek = today.weekOfWeekyear().roundFloorCopy(); DateTime endOfWeek = today.weekOfWeekyear().roundCeilingCopy(); Map<String, Double> mapWeek = new ReportsController().getOrdersByWeek(startOfWeek.toDate(), endOfWeek.toDate());//from w w w. j a va 2 s.c om Map<String, Double> sortedMapWeek = new TreeMap<>(ReportsController.DAYS_OF_WEEK_COMPARATOR); sortedMapWeek.putAll(mapWeek); List<ChartsCriteria> orderListWeek = new ArrayList<>(); for (Map.Entry<String, Double> m : sortedMapWeek.entrySet()) { orderListWeek.add(new ChartsCriteria(m.getKey(), m.getValue())); } String elementWeek = gson.toJson(orderListWeek); response.setContentType("application/json"); response.getWriter().write(elementWeek); break; case "month": DateTime startOfMonth = today.monthOfYear().roundFloorCopy(); DateTime endOfMonth = today.monthOfYear().roundCeilingCopy(); Map<String, Double> mapMonth = new ReportsController().getOrdersByMonth(startOfMonth.toDate(), endOfMonth.toDate()); Map<String, Double> sortedMapMonth = new TreeMap<>(ReportsController.DAYS_OF_MONTH_COMPARATOR); sortedMapMonth.putAll(mapMonth); List<ChartsCriteria> orderListMonth = new ArrayList<>(); for (Map.Entry<String, Double> m : sortedMapMonth.entrySet()) { orderListMonth.add(new ChartsCriteria(m.getKey(), m.getValue())); } String elementMonth = gson.toJson(orderListMonth); response.setContentType("application/json"); response.getWriter().write(elementMonth); break; case "year": DateTime startOfYear = today.year().roundFloorCopy(); DateTime endOfYear = today.year().roundCeilingCopy(); Map<String, Double> mapYear = new ReportsController().getOrdersByYear(startOfYear.toDate(), endOfYear.toDate()); Map<String, Double> sortedMapYear = new TreeMap<>(ReportsController.MONTH_OF_YEAR_COMPARATOR); sortedMapYear.putAll(mapYear); List<ChartsCriteria> orderListYear = new ArrayList<>(); for (Map.Entry<String, Double> m : sortedMapYear.entrySet()) { orderListYear.add(new ChartsCriteria(m.getKey(), m.getValue())); } String elementYear = gson.toJson(orderListYear); response.setContentType("application/json"); response.getWriter().write(elementYear); break; default: break; } }
From source file:com.certus.actions.getChartSalesAction.java
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String requestString = request.getParameter("sales"); DateTime today = new DateTime(); GsonBuilder gsonBuilder = new GsonBuilder(); Gson gson = gsonBuilder.registerTypeAdapter(ChartsCriteria.class, new ChartsCriteriaAdapter()).create(); switch (requestString) { case "week": DateTime startOfWeek = today.weekOfWeekyear().roundFloorCopy(); DateTime endOfWeek = today.weekOfWeekyear().roundCeilingCopy(); Map<String, Double> mapWeek = new ReportsController().getSalesByWeek(startOfWeek.toDate(), endOfWeek.toDate());//from w ww.ja v a 2s.c o m Map<String, Double> sortedMapWeek = new TreeMap<>(ReportsController.DAYS_OF_WEEK_COMPARATOR); sortedMapWeek.putAll(mapWeek); List<ChartsCriteria> saleListWeek = new ArrayList<>(); for (Map.Entry<String, Double> m : sortedMapWeek.entrySet()) { saleListWeek.add(new ChartsCriteria(m.getKey(), m.getValue())); } String elementWeek = gson.toJson(saleListWeek); response.setContentType("application/json"); response.getWriter().write(elementWeek); break; case "month": DateTime startOfMonth = today.monthOfYear().roundFloorCopy(); DateTime endOfMonth = today.monthOfYear().roundCeilingCopy(); Map<String, Double> mapMonth = new ReportsController().getSalesByMonth(startOfMonth.toDate(), endOfMonth.toDate()); Map<String, Double> sortedMapMonth = new TreeMap<>(ReportsController.DAYS_OF_MONTH_COMPARATOR); sortedMapMonth.putAll(mapMonth); List<ChartsCriteria> saleListMonth = new ArrayList<>(); for (Map.Entry<String, Double> m : sortedMapMonth.entrySet()) { saleListMonth.add(new ChartsCriteria(m.getKey(), m.getValue())); } String elementMonth = gson.toJson(saleListMonth); response.setContentType("application/json"); response.getWriter().write(elementMonth); break; case "year": DateTime startOfYear = today.year().roundFloorCopy(); DateTime endOfYear = today.year().roundCeilingCopy(); Map<String, Double> mapYear = new ReportsController().getSalesByYear(startOfYear.toDate(), endOfYear.toDate()); Map<String, Double> sortedMapYear = new TreeMap<>(ReportsController.MONTH_OF_YEAR_COMPARATOR); sortedMapYear.putAll(mapYear); List<ChartsCriteria> saleListYear = new ArrayList<>(); for (Map.Entry<String, Double> m : sortedMapYear.entrySet()) { saleListYear.add(new ChartsCriteria(m.getKey(), m.getValue())); } String elementYear = gson.toJson(saleListYear); response.setContentType("application/json"); response.getWriter().write(elementYear); break; default: break; } }
From source file:com.cfelde.cron4joda.SchedulingPattern.java
License:Open Source License
/** * This methods returns true if the given timestamp (expressed as a UNIX-era * millis value) matches the pattern, according to the given time zone. * * @param timezone A time zone./*from w ww .ja va 2 s. co m*/ * @param millis The timestamp, as a UNIX-era millis value. * @return true if the given timestamp matches the pattern. */ public boolean match(DateTime dt) { int minute = dt.getMinuteOfHour(); int hour = dt.getHourOfDay(); int dayOfMonth = dt.getDayOfMonth(); int month = dt.getMonthOfYear(); //gc.get(Calendar.MONTH) + 1; int dayOfWeek = dt.getDayOfWeek(); //gc.get(Calendar.DAY_OF_WEEK) - 1; for (int i = 0; i < matcherSize; i++) { ValueMatcher minuteMatcher = (ValueMatcher) minuteMatchers.get(i); ValueMatcher hourMatcher = (ValueMatcher) hourMatchers.get(i); ValueMatcher dayOfMonthMatcher = (ValueMatcher) dayOfMonthMatchers.get(i); ValueMatcher monthMatcher = (ValueMatcher) monthMatchers.get(i); ValueMatcher dayOfWeekMatcher = (ValueMatcher) dayOfWeekMatchers.get(i); boolean eval = minuteMatcher.match(minute) && hourMatcher.match(hour) && ((dayOfMonthMatcher instanceof DayOfMonthValueMatcher) ? ((DayOfMonthValueMatcher) dayOfMonthMatcher).match(dayOfMonth, month, dt.year().getField().isLeap(dt.getMillis())) /*gc.isLeapYear(year))*/ : dayOfMonthMatcher.match(dayOfMonth)) && monthMatcher.match(month) && dayOfWeekMatcher.match(dayOfWeek); if (eval) { return true; } } return false; }
From source file:com.chiorichan.plugin.lua.api.OSAPI.java
License:Mozilla Public License
@Override void initialize() { // Push a couple of functions that override original Lua API functions or // that add new functionality to it. lua.getGlobal("os"); // Custom os.clock() implementation returning the time the computer has // been actively running, instead of the native library... lua.pushJavaFunction(new JavaFunction() { @Override//from w w w .java 2 s . c om public int invoke(LuaState lua) { lua.pushNumber(System.currentTimeMillis()); return 1; } }); lua.setField(-2, "clock"); lua.pushJavaFunction(new JavaFunction() { @Override public int invoke(LuaState lua) { String format = lua.getTop() > 0 && lua.isString(1) ? lua.toString(1) : "%d/%m/%y %H:%M:%S"; long time = (long) (lua.getTop() > 1 && lua.isNumber(2) ? lua.toNumber(2) * 1000 / 60 / 60 : System.currentTimeMillis()); DateTime dt = new DateTime(time); if (format == "*t") { lua.newTable(0, 8); lua.pushInteger(dt.year().get()); lua.setField(-2, "year"); lua.pushInteger(dt.monthOfYear().get()); lua.setField(-2, "month"); lua.pushInteger(dt.dayOfMonth().get()); lua.setField(-2, "day"); lua.pushInteger(dt.hourOfDay().get()); lua.setField(-2, "hour"); lua.pushInteger(dt.minuteOfHour().get()); lua.setField(-2, "min"); lua.pushInteger(dt.secondOfMinute().get()); lua.setField(-2, "sec"); lua.pushInteger(dt.dayOfWeek().get()); lua.setField(-2, "wday"); lua.pushInteger(dt.dayOfYear().get()); lua.setField(-2, "yday"); } else lua.pushString(dt.toString(DateTimeFormat.forPattern(format))); return 1; } }); lua.setField(-2, "date"); /* * // Date formatting function. * lua.pushScalaFunction(lua => { * val format = * if (lua.getTop > 0 && lua.isString(1)) lua.toString(1) * else "%d/%m/%y %H:%M:%S" * val time = * if (lua.getTop > 1 && lua.isNumber(2)) lua.toNumber(2) * 1000 / 60 / 60 * else machine.worldTime + 5000 * * val dt = GameTimeFormatter.parse(time) * def fmt(format: String) { * if (format == "*t") { * lua.newTable(0, 8) * lua.pushInteger(dt.year) * lua.setField(-2, "year") * lua.pushInteger(dt.month) * lua.setField(-2, "month") * lua.pushInteger(dt.day) * lua.setField(-2, "day") * lua.pushInteger(dt.hour) * lua.setField(-2, "hour") * lua.pushInteger(dt.minute) * lua.setField(-2, "min") * lua.pushInteger(dt.second) * lua.setField(-2, "sec") * lua.pushInteger(dt.weekDay) * lua.setField(-2, "wday") * lua.pushInteger(dt.yearDay) * lua.setField(-2, "yday") * } * else { * lua.pushString(GameTimeFormatter.format(format, dt)) * } * } * * // Just ignore the allowed leading '!', Minecraft has no time zones... * if (format.startsWith("!")) * fmt(format.substring(1)) * else * fmt(format) * 1 * }) * lua.setField(-2, "date") * * // Return ingame time for os.time(). * lua.pushScalaFunction(lua => { * if (lua.isNoneOrNil(1)) { * // Game time is in ticks, so that each day has 24000 ticks, meaning * // one hour is game time divided by one thousand. Also, Minecraft * // starts days at 6 o'clock, versus the 1 o'clock of timestamps so we * // add those five hours. Thus: * // timestamp = (time + 5000) * 60[kh] * 60[km] / 1000[s] * lua.pushNumber((machine.worldTime + 5000) * 60 * 60 / 1000) * } * else { * def getField(key: String, d: Int) = { * lua.getField(-1, key) * val res = lua.toIntegerX(-1) * lua.pop(1) * if (res == null) * if (d < 0) throw new Exception("field '" + key + "' missing in date table") * else d * else res: Int * } * * lua.checkType(1, LuaType.TABLE) * lua.setTop(1) * * val sec = getField("sec", 0) * val min = getField("min", 0) * val hour = getField("hour", 12) * val mday = getField("day", -1) * val mon = getField("month", -1) * val year = getField("year", -1) * * GameTimeFormatter.mktime(year, mon, mday, hour, min, sec) match { * case Some(time) => lua.pushNumber(time) * case _ => lua.pushNil() * } * } * 1 * }) * lua.setField(-2, "time") */ // Pop the os table. lua.pop(1); }
From source file:com.clustercontrol.hub.util.CollectStringDataParser.java
License:Open Source License
/** * ????????????/*from www . j ava2 s . com*/ * * @param data * @return */ public CollectStringData parse(CollectStringData data) { Map<String, CollectDataTag> tagMap = new HashMap<>(); for (CollectDataTag tag : data.getTagList()) { tagMap.put(tag.getKey(), tag); } if (isNullOrZeroLength(format.getTimestampRegex()) && isNullOrZeroLength(format.getTimestampFormat())) { // do nothing, use currentTimeMillis as timestamp } else { Matcher m = timestampPattern.matcher(data.getValue()); if (m.find() && m.groupCount() > 0) { String timestampStr = m.group(1); try { DateTime datetime = timestampFormatter.parseDateTime(timestampStr); if (datetime.year().get() == 0) { // for messages without year, like syslog DateTime now = new DateTime(); DateTimeFormatter timestampFormatterWithCurrentYear = timestampFormatter .withDefaultYear(now.year().get()); DateTimeFormatter timestampFormatterWithLastYear = timestampFormatter .withDefaultYear(now.year().get() - 1); datetime = timestampFormatterWithCurrentYear.parseDateTime(timestampStr); if (datetime.getMillis() - now.getMillis() > 1000 * 60 * 60 * 24 * 7) { // treat messages as end of year (threshold : 1 week) datetime = timestampFormatterWithLastYear.parseDateTime(timestampStr); } } tagMap.put(CollectStringTag.TIMESTAMP_IN_LOG.name(), new CollectDataTag( new CollectDataTagPK(data.getCollectId(), data.getDataId(), CollectStringTag.TIMESTAMP_IN_LOG.name()), CollectStringTag.TIMESTAMP_IN_LOG.valueType(), Long.toString(datetime.getMillis()))); } catch (IllegalArgumentException e) { log.warn(String.format("invalid timestamp string : format = %s, string = %s", format.getTimestampRegex(), timestampStr)); } } } for (LogFormatKey keyword : format.getKeyPatternList()) { Pattern p = keywordPatternMap.get(keyword.getKey()); if (null == p) { log.debug(String.format("Pattern is null keyword : pattern=%s", keyword.getPattern())); continue; } Matcher m = p.matcher(data.getValue()); String matchedStr = null; switch (keyword.getKeyType()) { case parsing: if (m.find() && m.groupCount() > 0) { matchedStr = m.group(1); } break; case fixed: if (m.find()) { matchedStr = keyword.getValue(); } break; } if (matchedStr != null && keyword.getValueType() == ValueType.string) { tagMap.put(keyword.getKey(), new CollectDataTag( new CollectDataTagPK(data.getCollectId(), data.getDataId(), keyword.getKey()), keyword.getValueType(), matchedStr)); } else if (matchedStr != null && keyword.getValueType() != ValueType.string) { tagMap.put(keyword.getKey(), new CollectDataTag( new CollectDataTagPK(data.getCollectId(), data.getDataId(), keyword.getKey()), keyword.getValueType(), matchedStr)); switch (keyword.getValueType()) { case number: try { new BigDecimal(matchedStr); } catch (NumberFormatException e) { log.warn(String.format("not match number format : value=%s, source=%s, pattern=%s", matchedStr, data.getValue(), p.pattern())); } break; case bool: if (!"true".equalsIgnoreCase(matchedStr) || !"false".equalsIgnoreCase(matchedStr)) { log.warn(String.format("not match boolean type : value=%s, source=%s, pattern=%s", matchedStr, data.getValue(), p.pattern())); } break; default: log.warn(String.format("unexpected value type : type=%s, value=source=%s, pattern=%s", keyword.getValueType().name(), data.getValue(), p.pattern())); break; } } } data.setTagList(new ArrayList<>(tagMap.values())); return data; }
From source file:com.excilys.ebi.bank.web.tld.Functions.java
License:Apache License
public static int year(DateTime dateTime) { return dateTime.year().get(); }
From source file:com.idylwood.yahoo.Date.java
License:Open Source License
private Date(final DateTime dt) { this(dt.year().get(), dt.monthOfYear().get(), dt.dayOfMonth().get()); }
From source file:com.linkedin.cubert.utils.DateTimeUtilities.java
License:Open Source License
public static int asInt(DateTime dt) { return dt.year().get() * 100 * 100 + dt.monthOfYear().get() * 100 + dt.dayOfMonth().get(); }