ITeye博客

Java 代码

shelllgd · 2011-01-30 11:39 · java综合 · 编程语言 · Markdown

/**
     * 去除字符串中的空格,回车,换行,制表符
     * @param before 字符串的值
     * @return 处理后的字符串
     */
    public static String replaceBlank(String before){
        Pattern p = Pattern.compile("\\s*|\t|\r|\n");
        Matcher m = p.matcher(before);
        String after = m.replaceAll("");
        return after;
    }

 

 //获得本周一的日期   
    public String getMondayOFWeek(){   
        
         int mondayPlus = this.getMondayPlus();   
         GregorianCalendar currentDate = new GregorianCalendar();   
         currentDate.add(GregorianCalendar.DATE, mondayPlus);   
         Date monday = currentDate.getTime();   
            
         DateFormat df = DateFormat.getDateInstance();   
         String preMonday = df.format(monday);   
         return preMonday;   
    } 
    
    // 获得当前日期与本周日相差的天数   
    private int getMondayPlus() {   
        Calendar cd = Calendar.getInstance();   
        // 获得今天是一周的第几天,星期日是第一天,星期二是第二天......   
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK)-1;         //因为按中国礼拜一作为第一天所以这里减1   
        if (dayOfWeek == 1) {   
            return 0;   
        } else {   
            return 1 - dayOfWeek;   
        }   
    }   

评论(1)

  1. #1 楼 · Garfield.Geng · 2011-02-07 23:13
    
    // Apache common都有现成解决方案的:
    
    String input = "\n";
    assertEquals("", org.apache.commons.lang3.StringUtils.strip(input, "\n\r\t"));
    
    for (Iterator<Calendar> it = DateUtils.iterator(new Date(), DateUtils.RANGE_WEEK_MONDAY); it.hasNext();) {
        print(DateFormatUtils.format(it.next(), DateFormatUtils.ISO_DATE_FORMAT.getPattern()));
    }
    打印出来:
    2011-02-07
    2011-02-08
    2011-02-09
    2011-02-10
    2011-02-11
    2011-02-12
    2011-02-13
    
    

评论功能维护中,当前仅展示历史评论。