Java Code Examples for org.apache.commons.lang3.time.DateUtils#truncate()

The following examples show how to use org.apache.commons.lang3.time.DateUtils#truncate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TimeSpan.java    From dsworkbench with Apache License 2.0 5 votes vote down vote up
/**
 * @return the date
 */
public Date getDate() {
  if(isValidAtSpecificDay()) {
    return DateUtils.truncate(new Date(exactSpan.getMinimum()), Calendar.DATE);
  }
  return null;
}
 
Example 2
Source File: DecoratedChatChannel.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public void setStartDate(Date startDate) {
    if (startDate != null) {
        // fix up the date to shift to be beginning or end of the day (drop any time component)
        startDate = DateUtils.truncate(startDate, Calendar.DATE);
    }
    this.startDate = startDate;
}
 
Example 3
Source File: TestRemoteDownloadSource.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test
public void testMDTM() throws Exception {
  Assume.assumeTrue(scheme == Scheme.ftp || scheme == Scheme.ftps);
  path = "remote-download-source/parseSameTimestamp";
  File dir = new File(currentThread().getContextClassLoader().getResource(path).getPath());
  File[] files = dir.listFiles();
  Assert.assertEquals(3, files.length);
  Date now = DateUtils.truncate(new Date(), Calendar.SECOND);
  for (File f : files) {
    if (f.getName().equals("panda.txt")) {
      Assert.assertTrue(f.setLastModified(now.getTime()));
    } else if (f.getName().equals("polarbear.txt")) {
      Assert.assertTrue(f.setLastModified(18000000000L));
    } else if (f.getName().equals("sloth.txt")) {
      Assert.assertTrue(f.setLastModified(17000000000L));
    }
  }
  setupServer(dir.getAbsolutePath(), true);

  testMDTMHelper(17000000000L, 18000000000L, now.getTime());

  // Now, we'll configure the FTP Server to pretend like it doesn't support MDTM, which should lower the accuracy and
  // correctness of the timestamps
  supportMDTM = false;

  testMDTMHelper(
      // Older dates are truncated to month
      DateUtils.truncate(new Date(17000000000L), Calendar.DAY_OF_MONTH).getTime(),
      DateUtils.truncate(new Date(18000000000L), Calendar.DAY_OF_MONTH).getTime(),
      // Recent dates are truncated to minute
      DateUtils.truncate(now, Calendar.MINUTE).getTime());
}
 
Example 4
Source File: TruncateTime.java    From levelup-java-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void truncate_time_in_java_with_apache_commons () {
	
	Calendar cal = Calendar.getInstance();
	Date someRandomTruncatedDate = DateUtils.truncate(cal.getTime(), Calendar.DATE);
	
	// format object
	SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss z");
	String randomTruncatedDateFormatted = dateFormatter.format(someRandomTruncatedDate);
	
	logger.info("Truncated date: " + randomTruncatedDateFormatted);
	
	assertTrue(randomTruncatedDateFormatted.contains("12:00:00"));
}
 
Example 5
Source File: DatePicker.java    From dsworkbench with Apache License 2.0 5 votes vote down vote up
/**
 * Creates new form DatePicker
 */

public DatePicker() {
    initComponents();
    originalDate = DateUtils.truncate(new Date(), Calendar.DATE);
    selectedDate = originalDate;
    init();
}
 
Example 6
Source File: ResourceChartDialogEx.java    From logbook with MIT License 5 votes vote down vote up
/**
 * DateTimeで選択されている日付からCalendarインスタンスを作成します
 *
 * @param dateTime
 * @return
 */
private static Calendar getCalendar(DateTime dateTime) {
    Calendar cal = DateUtils.truncate(Calendar.getInstance(TimeZone.getDefault()), Calendar.DAY_OF_MONTH);
    cal.set(Calendar.YEAR, dateTime.getYear());
    cal.set(Calendar.MONTH, dateTime.getMonth());
    cal.set(Calendar.DAY_OF_MONTH, dateTime.getDay());
    return cal;
}
 
Example 7
Source File: DecoratedChatChannel.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public Date getEndDate() {
    if (endDate != null) {
        // fix up the date to drop any time component
        endDate = DateUtils.truncate(endDate, Calendar.DATE);
    }
    return endDate;
}
 
Example 8
Source File: SearchUtils.java    From jvue-admin with MIT License 5 votes vote down vote up
/**
 * 查询开始日期
 * @param date 日期("yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss")
 * @return 日期(0时0分)
 * @since  1.0
 */
public static Date dayFrom(String date) {
    if (StringUtils.isEmpty(date)) {
        return null;
    }
    try {
        Date d = DateUtils.parseDate(date, PARSE_PATTERNS);
        return DateUtils.truncate(d, Calendar.DATE);
    } catch (ParseException e) {
        LOGGER.warn("日期转换错误{},{}", date, e.getMessage());
    }
    return null;
}
 
Example 9
Source File: HubDAOImpl.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
private Calendar snappedMinutes(Calendar time, int snapTo) {
   int minutes = time.get(Calendar.MINUTE);
   int mod = minutes % snapTo;
   int mid = (int) Math.floor(snapTo / 2);
   time = DateUtils.truncate(time, Calendar.MINUTE);
   time.add(Calendar.MINUTE, mod < mid ? -mod : snapTo - mod);
   return time;
}
 
Example 10
Source File: DateUtil.java    From vjtools with Apache License 2.0 4 votes vote down vote up
/**
 * 2017-1-20 07:33:23, 则返回2017-1-16 00:00:00
 */
public static Date beginOfWeek(@NotNull final Date date) {
	return DateUtils.truncate(DateUtil.subDays(date, DateUtil.getDayOfWeek(date) - 1), Calendar.DATE);
}
 
Example 11
Source File: DateUtil.java    From j360-dubbo-app-all with Apache License 2.0 4 votes vote down vote up
/**
 * 2016-11-10 07:33:23, 则返回2016-1-1 00:00:00
 */
public static Date beginOfYear(@NotNull final Date date) {
	return DateUtils.truncate(date, Calendar.YEAR);
}
 
Example 12
Source File: DateUtil.java    From vjtools with Apache License 2.0 4 votes vote down vote up
/**
 * 2016-12-10 07:33:23, 则返回2016-12-10 07:33:00
 */
public static Date beginOfMinute(@NotNull final Date date) {
	return DateUtils.truncate(date, Calendar.MINUTE);
}
 
Example 13
Source File: DateUtil.java    From vjtools with Apache License 2.0 4 votes vote down vote up
/**
 * 2016-11-10 07:33:23, 则返回2016-1-1 00:00:00
 */
public static Date beginOfYear(@NotNull final Date date) {
	return DateUtils.truncate(date, Calendar.YEAR);
}
 
Example 14
Source File: DateUtil.java    From vjtools with Apache License 2.0 4 votes vote down vote up
/**
 * 2016-11-10 07:33:23, 则返回2016-1-1 00:00:00
 */
public static Date beginOfYear(@NotNull final Date date) {
	return DateUtils.truncate(date, Calendar.YEAR);
}
 
Example 15
Source File: DateUtil.java    From vjtools with Apache License 2.0 4 votes vote down vote up
/**
 * 2017-1-20 07:33:23, 则返回2017-1-16 00:00:00
 */
public static Date beginOfWeek(@NotNull final Date date) {
	return DateUtils.truncate(DateUtil.subDays(date, DateUtil.getDayOfWeek(date) - 1), Calendar.DATE);
}
 
Example 16
Source File: DateUtil.java    From vjtools with Apache License 2.0 4 votes vote down vote up
/**
 * 2017-1-23 07:33:23, 则返回2017-1-22 00:00:00
 */
public static Date nextWeek(@NotNull final Date date) {
	return DateUtils.truncate(DateUtil.addDays(date, 8 - DateUtil.getDayOfWeek(date)), Calendar.DATE);
}
 
Example 17
Source File: DateUtil.java    From j360-dubbo-app-all with Apache License 2.0 4 votes vote down vote up
/**
 * 2017-1-20 07:33:23, 则返回2017-1-16 00:00:00
 */
public static Date beginOfWeek(@NotNull final Date date) {
	return DateUtils.truncate(DateUtil.subDays(date, DateUtil.getDayOfWeek(date) - 1), Calendar.DATE);
}
 
Example 18
Source File: DateUtil.java    From vjtools with Apache License 2.0 4 votes vote down vote up
/**
 * 2016-12-10 07:33:23, 则返回2016-12-10 07:00:00
 */
public static Date beginOfHour(@NotNull final Date date) {
	return DateUtils.truncate(date, Calendar.HOUR_OF_DAY);
}
 
Example 19
Source File: DateUtil.java    From vjtools with Apache License 2.0 4 votes vote down vote up
/**
 * 2016-12-10 07:33:23, 则返回2016-12-10 07:33:00
 */
public static Date beginOfMinute(@NotNull final Date date) {
	return DateUtils.truncate(date, Calendar.MINUTE);
}
 
Example 20
Source File: MediaScannerService.java    From airsonic with GNU General Public License v3.0 4 votes vote down vote up
private void doScanLibrary() {
    LOG.info("Starting to scan media library.");
    MediaLibraryStatistics statistics = new MediaLibraryStatistics(
            DateUtils.truncate(new Date(), Calendar.SECOND));
    LOG.debug("New last scan date is " + statistics.getScanDate());

    try {

        // Maps from artist name to album count.
        Map<String, Integer> albumCount = new HashMap<String, Integer>();
        Genres genres = new Genres();

        scanCount = 0;

        mediaFileService.setMemoryCacheEnabled(false);
        indexManager.startIndexing();

        mediaFileService.clearMemoryCache();

        // Recurse through all files on disk.
        for (MusicFolder musicFolder : settingsService.getAllMusicFolders()) {
            MediaFile root = mediaFileService.getMediaFile(musicFolder.getPath(), false);
            scanFile(root, musicFolder, statistics, albumCount, genres, false);
        }

        // Scan podcast folder.
        File podcastFolder = new File(settingsService.getPodcastFolder());
        if (podcastFolder.exists()) {
            scanFile(mediaFileService.getMediaFile(podcastFolder), new MusicFolder(podcastFolder, null, true, null),
                     statistics, albumCount, genres, true);
        }

        LOG.info("Scanned media library with " + scanCount + " entries.");

        LOG.info("Marking non-present files.");
        mediaFileDao.markNonPresent(statistics.getScanDate());
        LOG.info("Marking non-present artists.");
        artistDao.markNonPresent(statistics.getScanDate());
        LOG.info("Marking non-present albums.");
        albumDao.markNonPresent(statistics.getScanDate());

        // Update statistics
        statistics.incrementArtists(albumCount.size());
        for (Integer albums : albumCount.values()) {
            statistics.incrementAlbums(albums);
        }

        // Update genres
        mediaFileDao.updateGenres(genres.getGenres());

        LOG.info("Completed media library scan.");

    } catch (Throwable x) {
        LOG.error("Failed to scan media library.", x);
    } finally {
        mediaFileService.setMemoryCacheEnabled(true);
        indexManager.stopIndexing(statistics);
        scanning = false;
    }
}