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

The following examples show how to use cn.hutool.core.date.DateUtil#month() . 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: CountServiceImpl.java    From BigDataPlatform with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 无数据补0
 * @param data
 * @param year
 * @return
 */
public List<OrderChartData> getFullYearData(List<OrderChartData> data,int year){

    List<OrderChartData> fullData = new ArrayList<>();
    //起始月份
    Date everyMonth = TimeUtil.getBeginDayOfYear(year);
    int count = -1;
    for(int i=0;i<12;i++){
        boolean flag = true;
        for(OrderChartData chartData:data){
            if(DateUtil.month(chartData.getTime())==DateUtil.month(everyMonth)){
                //有数据
                flag = false;
                count++;
                break;
            }
        }
        if(!flag){
            fullData.add(data.get(count));
        }else{
            OrderChartData orderChartData = new OrderChartData();
            orderChartData.setTime(everyMonth);
            orderChartData.setMoney(new BigDecimal("0"));
            fullData.add(orderChartData);
        }

        //时间+1天
        Calendar cal = Calendar.getInstance();
        cal.setTime(everyMonth);
        cal.add(Calendar.MONTH, 1);
        everyMonth = cal.getTime();
    }
    return fullData;
}
 
Example 2
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 3
Source File: PostServiceImpl.java    From halo with GNU General Public License v3.0 4 votes vote down vote up
private String buildFullPath(Post post) {

        PostPermalinkType permalinkType = optionService.getPostPermalinkType();

        String pathSuffix = optionService.getPathSuffix();

        String archivesPrefix = optionService.getArchivesPrefix();

        int month = DateUtil.month(post.getCreateTime()) + 1;

        String monthString = month < 10 ? "0" + month : String.valueOf(month);

        int day = DateUtil.dayOfMonth(post.getCreateTime());

        String dayString = day < 10 ? "0" + day : String.valueOf(day);

        StringBuilder fullPath = new StringBuilder();

        if (optionService.isEnabledAbsolutePath()) {
            fullPath.append(optionService.getBlogBaseUrl());
        }

        fullPath.append(URL_SEPARATOR);

        if (permalinkType.equals(PostPermalinkType.DEFAULT)) {
            fullPath.append(archivesPrefix)
                .append(URL_SEPARATOR)
                .append(post.getSlug())
                .append(pathSuffix);
        } else if (permalinkType.equals(PostPermalinkType.ID)) {
            fullPath.append("?p=")
                .append(post.getId());
        } else if (permalinkType.equals(PostPermalinkType.DATE)) {
            fullPath.append(DateUtil.year(post.getCreateTime()))
                .append(URL_SEPARATOR)
                .append(monthString)
                .append(URL_SEPARATOR)
                .append(post.getSlug())
                .append(pathSuffix);
        } else if (permalinkType.equals(PostPermalinkType.DAY)) {
            fullPath.append(DateUtil.year(post.getCreateTime()))
                .append(URL_SEPARATOR)
                .append(monthString)
                .append(URL_SEPARATOR)
                .append(dayString)
                .append(URL_SEPARATOR)
                .append(post.getSlug())
                .append(pathSuffix);
        }
        return fullPath.toString();
    }
 
Example 4
Source File: PostCommentServiceImpl.java    From halo with GNU General Public License v3.0 4 votes vote down vote up
private BasePostMinimalDTO buildPostFullPath(BasePostMinimalDTO post) {
    PostPermalinkType permalinkType = optionService.getPostPermalinkType();

    String pathSuffix = optionService.getPathSuffix();

    String archivesPrefix = optionService.getArchivesPrefix();

    int month = DateUtil.month(post.getCreateTime()) + 1;

    String monthString = month < 10 ? "0" + month : String.valueOf(month);

    int day = DateUtil.dayOfMonth(post.getCreateTime());

    String dayString = day < 10 ? "0" + day : String.valueOf(day);

    StringBuilder fullPath = new StringBuilder();

    if (optionService.isEnabledAbsolutePath()) {
        fullPath.append(optionService.getBlogBaseUrl());
    }

    fullPath.append(URL_SEPARATOR);

    if (permalinkType.equals(PostPermalinkType.DEFAULT)) {
        fullPath.append(archivesPrefix)
            .append(URL_SEPARATOR)
            .append(post.getSlug())
            .append(pathSuffix);
    } else if (permalinkType.equals(PostPermalinkType.ID)) {
        fullPath.append("?p=")
            .append(post.getId());
    } else if (permalinkType.equals(PostPermalinkType.DATE)) {
        fullPath.append(DateUtil.year(post.getCreateTime()))
            .append(URL_SEPARATOR)
            .append(monthString)
            .append(URL_SEPARATOR)
            .append(post.getSlug())
            .append(pathSuffix);
    } else if (permalinkType.equals(PostPermalinkType.DAY)) {
        fullPath.append(DateUtil.year(post.getCreateTime()))
            .append(URL_SEPARATOR)
            .append(monthString)
            .append(URL_SEPARATOR)
            .append(dayString)
            .append(URL_SEPARATOR)
            .append(post.getSlug())
            .append(pathSuffix);
    }

    post.setFullPath(fullPath.toString());

    return post;
}