List of usage examples for org.apache.commons.lang.time DateUtils toCalendar
public static Calendar toCalendar(Date date)
From source file:net.audumla.astronomy.SeasonTest.java
@Test public void testSpringStart() throws Exception { SeasonalEvent event = new SeasonalEvent(SeasonalEvent.SPRINGSTART, Geolocation.newGeoLocation(30, 0, 0)); Date et = event.calculateEventFrom(new Date()); assert DateUtils.toCalendar(et).get(Calendar.MONTH) == Calendar.MARCH; event = new SeasonalEvent(SeasonalEvent.SPRINGSTART, Geolocation.newGeoLocation(-30, 0, 0)); et = event.calculateEventFrom(new Date()); assert DateUtils.toCalendar(et).get(Calendar.MONTH) == Calendar.SEPTEMBER; }
From source file:com.dp2345.controller.admin.StaticController.java
/** * ???//from w ww. j av a 2 s . c om */ @RequestMapping(value = "/build", method = RequestMethod.POST) public @ResponseBody Map<String, Object> build(BuildType buildType, Long articleCategoryId, Long productCategoryId, Date beginDate, Date endDate, Integer first, Integer count) { long startTime = System.currentTimeMillis(); if (beginDate != null) { Calendar calendar = DateUtils.toCalendar(beginDate); calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMinimum(Calendar.HOUR_OF_DAY)); calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE)); calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND)); beginDate = calendar.getTime(); } if (endDate != null) { Calendar calendar = DateUtils.toCalendar(endDate); calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY)); calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE)); calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND)); endDate = calendar.getTime(); } if (first == null || first < 0) { first = 0; } if (count == null || count <= 0) { count = 50; } int buildCount = 0; boolean isCompleted = true; if (buildType == BuildType.index) { buildCount = staticService.buildIndex(); } else if (buildType == BuildType.article) { ArticleCategory articleCategory = articleCategoryService.find(articleCategoryId); List<Article> articles = articleService.findList(articleCategory, beginDate, endDate, first, count); for (Article article : articles) { buildCount += staticService.build(article); } first += articles.size(); if (articles.size() == count) { isCompleted = false; } } else if (buildType == BuildType.product) { ProductCategory productCategory = productCategoryService.find(productCategoryId); List<Product> products = productService.findList(productCategory, beginDate, endDate, first, count); for (Product product : products) { buildCount += staticService.build(product); } first += products.size(); if (products.size() == count) { isCompleted = false; } } else if (buildType == BuildType.other) { buildCount = staticService.buildOther(); } long endTime = System.currentTimeMillis(); Map<String, Object> map = new HashMap<String, Object>(); map.put("first", first); map.put("buildCount", buildCount); map.put("buildTime", endTime - startTime); map.put("isCompleted", isCompleted); return map; }
From source file:com.haulmont.timesheets.web.calendar.TimeEntryCalendarEventAdapter.java
@Override public Date getEnd() { HoursAndMinutes hoursAndMinutes = HoursAndMinutes.fromTimeEntry(timeEntry); Calendar dateCal = DateUtils.toCalendar(getStart()); dateCal.set(Calendar.HOUR_OF_DAY, hoursAndMinutes.getHours()); dateCal.set(Calendar.MINUTE, hoursAndMinutes.getMinutes()); return dateCal.getTime(); }
From source file:com.haulmont.timesheets.gui.util.ComponentsHelper.java
public static String getColumnCaption(String columnId, Date date) { String caption = messages.getMessage(WeeklyReportEntry.class, "WeeklyReportEntry." + columnId); String format = COMMON_DAY_CAPTION_STYLE; if (workdaysTools.isHoliday(date) || workdaysTools.isWeekend(date)) { format = String.format(HOLIDAY_CAPTION_STYLE, format); }/*from w w w .j a v a2 s . c o m*/ if (DateUtils.isSameDay(timeSource.currentTimestamp(), date)) { format = String.format(TODAY_CAPTION_STYLE, format); } return String.format(format, caption, DateUtils.toCalendar(date).get(Calendar.DAY_OF_MONTH)); }
From source file:net.shopxx.dao.impl.StatisticDaoImpl.java
public List<Statistic> analyze(Statistic.Period period, Date beginDate, Date endDate) { Assert.notNull(period);/* ww w. j ava 2 s . com*/ CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Statistic> criteriaQuery = criteriaBuilder.createQuery(Statistic.class); Root<Statistic> root = criteriaQuery.from(Statistic.class); switch (period) { case year: criteriaQuery.select(criteriaBuilder.construct(Statistic.class, root.get("year"), criteriaBuilder.sum(root.<Long>get("registerMemberCount")), criteriaBuilder.sum(root.<Long>get("createOrderCount")), criteriaBuilder.sum(root.<Long>get("completeOrderCount")), criteriaBuilder.sum(root.<BigDecimal>get("createOrderAmount")), criteriaBuilder.sum(root.<BigDecimal>get("completeOrderAmount")))); criteriaQuery.groupBy(root.get("year")); break; case month: criteriaQuery.select(criteriaBuilder.construct(Statistic.class, root.get("year"), root.get("month"), criteriaBuilder.sum(root.<Long>get("registerMemberCount")), criteriaBuilder.sum(root.<Long>get("createOrderCount")), criteriaBuilder.sum(root.<Long>get("completeOrderCount")), criteriaBuilder.sum(root.<BigDecimal>get("createOrderAmount")), criteriaBuilder.sum(root.<BigDecimal>get("completeOrderAmount")))); criteriaQuery.groupBy(root.get("year"), root.get("month")); break; case day: criteriaQuery.select(criteriaBuilder.construct(Statistic.class, root.get("year"), root.get("month"), root.get("day"), root.<Long>get("registerMemberCount"), root.<Long>get("createOrderCount"), root.<Long>get("completeOrderCount"), root.<BigDecimal>get("createOrderAmount"), root.<BigDecimal>get("completeOrderAmount"))); break; } Predicate restrictions = criteriaBuilder.conjunction(); if (beginDate != null) { Calendar calendar = DateUtils.toCalendar(beginDate); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.greaterThan(root.<Integer>get("year"), year), criteriaBuilder.and(criteriaBuilder.equal(root.<Integer>get("year"), year), criteriaBuilder.greaterThan(root.<Integer>get("month"), month)), criteriaBuilder.and(criteriaBuilder.equal(root.<Integer>get("year"), year), criteriaBuilder.equal(root.<Integer>get("month"), month), criteriaBuilder.greaterThanOrEqualTo(root.<Integer>get("day"), day)))); } if (endDate != null) { Calendar calendar = DateUtils.toCalendar(endDate); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.lessThan(root.<Integer>get("year"), year), criteriaBuilder.and(criteriaBuilder.equal(root.<Integer>get("year"), year), criteriaBuilder.lessThan(root.<Integer>get("month"), month)), criteriaBuilder.and(criteriaBuilder.equal(root.<Integer>get("year"), year), criteriaBuilder.equal(root.<Integer>get("month"), month), criteriaBuilder.lessThanOrEqualTo(root.<Integer>get("day"), day)))); } criteriaQuery.where(restrictions); return entityManager.createQuery(criteriaQuery).getResultList(); }
From source file:com.dp2345.service.impl.ProductServiceImpl.java
/** * // w w w . j av a 2 s.com */ @SuppressWarnings("unchecked") private void updateHits() { Ehcache cache = cacheManager.getEhcache(Product.HITS_CACHE_NAME); List<Long> ids = cache.getKeys(); for (Long id : ids) { Product product = productDao.find(id); if (product != null) { productDao.lock(product, LockModeType.PESSIMISTIC_WRITE); Element element = cache.get(id); long hits = (Long) element.getObjectValue(); long increment = hits - product.getHits(); Calendar nowCalendar = Calendar.getInstance(); Calendar weekHitsCalendar = DateUtils.toCalendar(product.getWeekHitsDate()); Calendar monthHitsCalendar = DateUtils.toCalendar(product.getMonthHitsDate()); if (nowCalendar.get(Calendar.YEAR) != weekHitsCalendar.get(Calendar.YEAR) || nowCalendar.get(Calendar.WEEK_OF_YEAR) > weekHitsCalendar.get(Calendar.WEEK_OF_YEAR)) { product.setWeekHits(increment); } else { product.setWeekHits(product.getWeekHits() + increment); } if (nowCalendar.get(Calendar.YEAR) != monthHitsCalendar.get(Calendar.YEAR) || nowCalendar.get(Calendar.MONTH) > monthHitsCalendar.get(Calendar.MONTH)) { product.setMonthHits(increment); } else { product.setMonthHits(product.getMonthHits() + increment); } product.setHits(hits); product.setWeekHitsDate(new Date()); product.setMonthHitsDate(new Date()); productDao.merge(product); } } }
From source file:com.haulmont.timesheets.entity.WeeklyReportEntry.java
public void addTimeEntry(TimeEntry timeEntry) { int dayNumber = DateUtils.toCalendar(timeEntry.getDate()).get(Calendar.DAY_OF_WEEK); DayOfWeek day = DayOfWeek.fromCalendarDay(dayNumber); List<TimeEntry> timeEntries = getDayOfWeekTimeEntries(day); if (timeEntries == null) { List<TimeEntry> list = new ArrayList<>(); list.add(timeEntry);//from w ww .j av a 2 s. co m changeDayOfWeekTimeEntries(day, list); } else { timeEntries.add(timeEntry); } }
From source file:com.haulmont.timesheets.gui.approve.ApproveScreen.java
protected void initDaysColumns() { for (Date current = firstDayOfWeek; current.getTime() <= lastDayOfWeek.getTime(); current = DateUtils .addDays(current, 1)) {/*from w ww. j a v a 2 s . com*/ final DayOfWeek day = DayOfWeek .fromCalendarDay(DateUtils.toCalendar(current).get(Calendar.DAY_OF_WEEK)); final String columnId = day.getId() + COLUMN_SUFFIX; weeklyReportsTable.addGeneratedColumn(columnId, new Table.ColumnGenerator<WeeklyReportEntry>() { @Override public Component generateCell(final WeeklyReportEntry entity) { List<TimeEntry> timeEntries = entity.getDayOfWeekTimeEntries(day); if (CollectionUtils.isNotEmpty(timeEntries)) { if (timeEntries.size() == 1) { return createLinkToSingleTimeEntry(entity, timeEntries); } else { return createLinkToMultipleTimeEntries(entity, timeEntries.get(0).getDate()); } } return null; } private Component createLinkToMultipleTimeEntries(final WeeklyReportEntry reportEntry, final Date date) { final LinkButton linkButton = componentsFactory.createComponent(LinkButton.class); linkButton.setCaption(StringFormatHelper.getDayHoursString(reportEntry.getTotalForDay(day))); linkButton.setAction(new AbstractAction("edit") { @Override public void actionPerform(Component component) { User user = usersTable.getSingleSelected(); if (user != null) { openLookup("ts$TimeEntry.lookup", items -> { if (CollectionUtils.isNotEmpty(items)) { TimeEntry timeEntry = (TimeEntry) items.iterator().next(); openTimeEntryEditor(timeEntry); } }, WindowManager.OpenType.DIALOG, ParamsMap.of("date", date, "task", reportEntry.getTask(), "activityType", reportEntry.getActivityType(), "user", user.getId())); } } }); return linkButton; } private Component createLinkToSingleTimeEntry(WeeklyReportEntry reportEntry, List<TimeEntry> timeEntries) { final TimeEntry timeEntry = timeEntries.get(0); final LinkButton linkButton = componentsFactory.createComponent(LinkButton.class); linkButton.setCaption(StringFormatHelper.getDayHoursString(reportEntry.getTotalForDay(day))); linkButton.setAction(new AbstractAction("edit") { @Override public void actionPerform(Component component) { openTimeEntryEditor(timeEntry); } }); return linkButton; } }); weeklyReportsTable.setColumnWidth(columnId, 80); Table.Column column = weeklyReportsTable.getColumn(columnId); column.setAggregation(ComponentsHelper.createAggregationInfo( projectsService.getEntityMetaPropertyPath(WeeklyReportEntry.class, day.getId()), new WeeklyReportEntryAggregation())); } }
From source file:com.tesora.dve.db.mysql.common.MysqlAPIUtils.java
public static void putLengthCodedDate(ByteBuf cb, Date inDate) { byte length = 0; Calendar cal = null;//from ww w . j a v a 2 s . com if (inDate != null) { cal = DateUtils.toCalendar(inDate); if (cal.get(Calendar.MILLISECOND) > 0) { // indicates we need full 11 byte date length = 11; } else if (cal.get(Calendar.HOUR_OF_DAY) > 0 || cal.get(Calendar.MINUTE) > 0 || cal.get(Calendar.SECOND) > 0) { // this is the 7 byte format length = 7; } else if (cal.get(Calendar.YEAR) > 0 || cal.get(Calendar.MONTH) > 0 || cal.get(Calendar.DAY_OF_MONTH) > 0) { length = 4; } } cb.writeByte(length); if (length >= 4) { cb.writeShort(cal.get(Calendar.YEAR)); cb.writeByte(cal.get(Calendar.MONTH) + 1); // MONTH is zero based cb.writeByte(cal.get(Calendar.DAY_OF_MONTH)); } if (length >= 7) { cb.writeByte(cal.get(Calendar.HOUR_OF_DAY)); cb.writeByte(cal.get(Calendar.MINUTE)); cb.writeByte(cal.get(Calendar.SECOND)); } if (length == 11) { // 1 millisecond = 1000 microseconds, right? int microSeconds = cal.get(Calendar.MILLISECOND) * 1000; cb.writeInt(microSeconds); } }
From source file:com.tesora.dve.db.mysql.common.MysqlAPIUtils.java
public static void putLengthCodedTime(ByteBuf cb, Time inTime) { byte length = 0; Calendar cal = null;//from w ww . jav a 2s . c o m if (inTime != null) { cal = DateUtils.toCalendar(inTime); if (cal.get(Calendar.MILLISECOND) > 0) { // indicates we need full 12 byte date length = 12; } else if (cal.get(Calendar.HOUR_OF_DAY) > 0 || cal.get(Calendar.MINUTE) > 0 || cal.get(Calendar.SECOND) > 0) { // this is the 8 byte format length = 8; } } cb.writeByte(length); if (length >= 8) { cb.writeZero(5); // this is the sign and days - we are not supporting this cb.writeByte(cal.get(Calendar.HOUR_OF_DAY)); cb.writeByte(cal.get(Calendar.MINUTE)); cb.writeByte(cal.get(Calendar.SECOND)); } if (length == 12) { // 1 millisecond = 1000 microseconds, right? int microSeconds = cal.get(Calendar.MILLISECOND) * 1000; cb.writeInt(microSeconds); } }