Java Code Examples for cn.hutool.core.date.DateUtil#parse()

The following examples show how to use cn.hutool.core.date.DateUtil#parse() . 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: BaseService.java    From SENS with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 根据查询条件分页获取
 *
 * @param page
 * @param condition
 * @return
 */
default Page<E> findAll(Page<E> page, QueryCondition<E> condition) {
    E e = condition.getData();
    SearchVo searchVo = condition.getSearchVo();

    //对指定字段查询
    QueryWrapper<E> queryWrapper = getQueryWrapper(e);

    //查询日期范围
    if (searchVo != null) {
        String startDate = searchVo.getStartDate();
        String endDate = searchVo.getEndDate();
        if (StrUtil.isNotBlank(startDate) && StrUtil.isNotBlank(endDate)) {
            Date start = DateUtil.parse(startDate);
            Date end = DateUtil.parse(endDate);
            queryWrapper.between("create_time", start, end);
        }
    }
    return (Page<E>) getRepository().selectPage(page, queryWrapper);
}
 
Example 2
Source File: BaseServerController.java    From Jpom with MIT License 6 votes vote down vote up
/**
 * 处理分页的时间字段
 *
 * @param page    分页
 * @param entity  条件
 * @param colName 字段名称
 */
protected void doPage(Page page, Entity entity, String colName) {
    String time = getParameter("time");
    colName = colName.toUpperCase();
    page.addOrder(new Order(colName, Direction.DESC));
    // 时间
    if (StrUtil.isNotEmpty(time)) {
        String[] val = StrUtil.split(time, "~");
        if (val.length == 2) {
            DateTime startDateTime = DateUtil.parse(val[0], DatePattern.NORM_DATETIME_FORMAT);
            entity.set(colName, ">= " + startDateTime.getTime());

            DateTime endDateTime = DateUtil.parse(val[1], DatePattern.NORM_DATETIME_FORMAT);
            if (startDateTime.equals(endDateTime)) {
                endDateTime = DateUtil.endOfDay(endDateTime);
            }
            // 防止字段重复
            entity.set(colName + " ", "<= " + endDateTime.getTime());
        }
    }
}
 
Example 3
Source File: AdminController.java    From mayday with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 后台首页
 * 
 * @return
 */
@RequestMapping(value = { "", "index" })
public String index(Model model) {
	// 查询已发布文章数
	Integer countPublish = articleService.countByStatus(null, PostType.POST_TYPE_POST.getValue());
	model.addAttribute("countPublish", countPublish);
	// 友链总数
	List<Link> lists = linksService.findLinks();
	model.addAttribute("countLinks", lists.size());
	// 附件总数
	int countAttachment = attachmentService.countAttachment().size();
	model.addAttribute("countAttachment", countAttachment);
	// 成立天数
	Date blogStart=DateUtil.parse(MaydayConst.OPTIONS.get("blog_start").toString());
	model.addAttribute("establishDate", DateUtil.between(blogStart, DateUtil.date(), DateUnit.DAY));
	// 查询最新的文章
	ArticleCustom articleCustom = new ArticleCustom();
	articleCustom.setArticlePost(PostType.POST_TYPE_POST.getValue());
	PageInfo<ArticleCustom> pageInfo = articleService.findPageArticle(1, 5, articleCustom);
	model.addAttribute("articles", pageInfo.getList());
	// 查询最新的日志
	PageInfo<Log> info = logService.findLogs(1, 5);
	model.addAttribute("logs", info.getList());
	return "admin/admin_index";
}
 
Example 4
Source File: UseHutool.java    From java-tutorial with MIT License 6 votes vote down vote up
/**
 * hutool 时间格式化
 */
public static void dateFormat() {

    String dateStr = "2017-03-01";
    Date date = DateUtil.parse(dateStr);
    //结果 2017/03/01
    String format = DateUtil.format(date, "yyyy/MM/dd");
    //常用格式的格式化,结果:2017-03-01
    String formatDate = DateUtil.formatDate(date);
    //结果:2017-03-01 00:00:00
    String formatDateTime = DateUtil.formatDateTime(date);
    //结果:00:00:00
    String formatTime = DateUtil.formatTime(date);
    System.out.println(format);
    System.out.println(formatDate);
    System.out.println(formatDateTime);
    System.out.println(formatTime);
}
 
Example 5
Source File: SysConfigServiceImpl.java    From OneBlog with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 获取网站详情
 */
@Override
public Map<String, Object> getSiteInfo() {
    Map<String, Object> siteInfo = sysConfigMapper.getSiteInfo();
    if (!CollectionUtils.isEmpty(siteInfo)) {
        Date installdate = null;
        SysConfig config = this.getByKey(ConfigKeyEnum.INSTALLDATE.getKey());
        if (null == config || StringUtils.isEmpty(config.getConfigValue())) {
            // 默认建站日期为2019-01-01
            installdate = Date.from(LocalDate.of(2019, 1, 1).atStartOfDay(ZoneId.systemDefault()).toInstant());
        } else {
            installdate = DateUtil.parse(config.getConfigValue(), DatePattern.NORM_DATETIME_PATTERN);
        }
        long between = 1;
        if (!installdate.after(new Date())) {
            between = DateUtil.between(installdate, new Date(), DateUnit.DAY);
        }
        siteInfo.put("installdate", between < 1 ? 1 : between);
    }
    return siteInfo;
}
 
Example 6
Source File: BlockIndexService.java    From WeBASE-Collect-Bee with Apache License 2.0 5 votes vote down vote up
public long getStartBlockIndex() throws ParseException, IOException, InterruptedException {
    if (systemEnvironmentConfig.getStartBlockHeight() > 0) {
        return systemEnvironmentConfig.getStartBlockHeight();
    }
    if (systemEnvironmentConfig.getStartDate() != null && systemEnvironmentConfig.getStartDate().length() > 0) {
        log.info("startDate : {}", systemEnvironmentConfig.getStartDate());
        Date startDate = DateUtil.parse(systemEnvironmentConfig.getStartDate());
        long blockIndex = -1;
        while ((blockIndex = getBlockIndexByStartDate(startDate)) < 0) {
            Thread.sleep(systemEnvironmentConfig.getFrequency() * 1000);
        }
        return blockIndex;
    }
    return 0;
}
 
Example 7
Source File: AdminController.java    From stone with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 请求后台页面
 *
 * @param model   model
 * @param session session
 *
 * @return 模板路径admin/admin_index
 */
@GetMapping(value = {"", "/index"})
public String index(Model model) {

    //查询评论的条数
    final Long commentCount = commentService.getCount();
    model.addAttribute("commentCount", commentCount);

    //查询最新的文章
    final List<Post> postsLatest = postService.findPostLatest();
    model.addAttribute("postTopFive", postsLatest);

    //查询最新的日志
    final List<Logs> logsLatest = logsService.findLogsLatest();
    model.addAttribute("logs", logsLatest);

    //查询最新的评论
    final List<Comment> comments = commentService.findCommentsLatest();
    model.addAttribute("comments", comments);

    //附件数量
    model.addAttribute("mediaCount", attachmentService.getCount());

    //文章阅读总数
    final Long postViewsSum = postService.getPostViews();
    model.addAttribute("postViewsSum", postViewsSum);

    //成立天数
    final Date blogStart = DateUtil.parse(HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_START.getProp()));
    final long hadDays = DateUtil.between(blogStart, DateUtil.date(), DateUnit.DAY);
    model.addAttribute("hadDays", hadDays);
    return "admin/admin_index";
}
 
Example 8
Source File: HutoolController.java    From mall-learning with Apache License 2.0 5 votes vote down vote up
@ApiOperation("DateUtil使用:日期时间工具")
@GetMapping(value = "/dateUtil")
public CommonResult dateUtil() {
    //Date、long、Calendar之间的相互转换
    //当前时间
    Date date = DateUtil.date();
    //Calendar转Date
    date = DateUtil.date(Calendar.getInstance());
    //时间戳转Date
    date = DateUtil.date(System.currentTimeMillis());
    //自动识别格式转换
    String dateStr = "2017-03-01";
    date = DateUtil.parse(dateStr);
    //自定义格式化转换
    date = DateUtil.parse(dateStr, "yyyy-MM-dd");
    //格式化输出日期
    String format = DateUtil.format(date, "yyyy-MM-dd");
    //获得年的部分
    int year = DateUtil.year(date);
    //获得月份,从0开始计数
    int month = DateUtil.month(date);
    //获取某天的开始、结束时间
    Date beginOfDay = DateUtil.beginOfDay(date);
    Date endOfDay = DateUtil.endOfDay(date);
    //计算偏移后的日期时间
    Date newDate = DateUtil.offset(date, DateField.DAY_OF_MONTH, 2);
    //计算日期时间之间的偏移量
    long betweenDay = DateUtil.between(date, newDate, DateUnit.DAY);
    return CommonResult.success(null, "操作成功");
}
 
Example 9
Source File: ArticleServiceImpl.java    From mayday with GNU General Public License v3.0 5 votes vote down vote up
@Override
@Cacheable(value = ARTICLES_CACHE_NAME, key = ARTICLES_CACHE_KEY)
public List<ArchiveBo> archives() {
	// 查询文章表各个时间段的文章数量 分别为DATE->时间段 count->文章数量
	List<ArchiveBo> listforArchiveBo = articleMapperCustom.findDateAndCount();
	if (listforArchiveBo != null) {
		for (ArchiveBo archiveBo : listforArchiveBo) {
			ArticleExample example = new ArticleExample();
			// 在EXAMPLE对象中存放article_status和article_post
			ArticleExample.Criteria criteria = example.createCriteria().andArticleStatusEqualTo(0)
					.andArticlePostEqualTo("post");
			example.setOrderByClause("article_newstime desc");
			String date = archiveBo.getDate();
			Date sd = DateUtil.parse(date, "yyyy年MM月");
			// 在criteria对象中放入article_newstime大于或者等于的值
			criteria.andArticleNewstimeGreaterThanOrEqualTo(sd);
			Calendar cal = Calendar.getInstance();
			// 判断获取的时间的月份是否小于12
			if (sd.getMonth() < 12) {
				cal.setTime(sd);
				// 月份 +1
				cal.add(Calendar.MONTH, +1);
			} else {
				cal.setTime(sd);
				// 年 +1
				cal.add(Calendar.YEAR, +1);
			}
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
			String date2 = sdf.format(cal.getTime());
			Date date3 = DateUtil.parse(date2, "yyyy年MM月");
			// 在criteria对象中放入article_newstime小于的值
			criteria.andArticleNewstimeLessThan(date3);
			List<Article> articles = articleMapper.selectByExample(example);
			archiveBo.setArticles(articles);
		}
	}
	return listforArchiveBo;
}
 
Example 10
Source File: AdminController.java    From blog-sharon with Apache License 2.0 5 votes vote down vote up
/**
 * 请求后台页面
 *
 * @param model   model
 * @param session session
 * @return 模板路径admin/admin_index
 */
@GetMapping(value = {"", "/index"})
public String index(Model model) {

    //查询评论的条数
    Long commentCount = commentService.getCount();
    model.addAttribute("commentCount", commentCount);

    //查询最新的文章
    List<Post> postsLatest = postService.findPostLatest();
    model.addAttribute("postTopFive", postsLatest);

    //查询最新的日志
    List<Logs> logsLatest = logsService.findLogsLatest();
    model.addAttribute("logs", logsLatest);

    //查询最新的评论
    List<Comment> comments = commentService.findCommentsLatest();
    model.addAttribute("comments", comments);

    //附件数量
    model.addAttribute("mediaCount", attachmentService.getCount());

    //文章阅读总数
    Long postViewsSum = postService.getPostViews();
    model.addAttribute("postViewsSum", postViewsSum);

    //成立天数
    Date blogStart = DateUtil.parse(HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_START.getProp()));
    long hadDays = DateUtil.between(blogStart, DateUtil.date(), DateUnit.DAY);
    model.addAttribute("hadDays",hadDays);
    return "admin/admin_index";
}
 
Example 11
Source File: String2DateConfig.java    From Guns with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Date convert(String dateString) {
    if (ToolUtil.isEmpty(dateString)) {
        return null;
    } else {
        return DateUtil.parse(dateString);
    }
}
 
Example 12
Source File: CommonUtil.java    From spring-security with Apache License 2.0 2 votes vote down vote up
/**
 * 将String类型的时间转为java.util.Date
 * @param time
 * @return
 */
public static Date parseDate(String time){
    return DateUtil.parse(time, "yyyy-MM-dd HH:mm:ss");
}