日期工具类数学工具类字符串工具类

package com.jude.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 日期工具类
 * @author 
 *
 */
public class DateUtil {

	/**
	 * 把日期对象根据生成指定格式的字符串
	 * @param date
	 * @param format
	 * @return
	 */
	public static String formatDate(Date date,String format){
		String result="";
		SimpleDateFormat sdf=new SimpleDateFormat(format);
		if(date!=null){
			result=sdf.format(date);
		}
		return result;
	}
	
	/**
	 * 把日期字符串生成指定格式的日期对象
	 * @param str
	 * @param format
	 * @return
	 * @throws Exception
	 */
	public static Date formatString(String str,String format) throws Exception{
		if(StringUtil.isEmpty(str)){
			return null;
		}
		SimpleDateFormat sdf=new SimpleDateFormat(format);
		return sdf.parse(str);
	}
	
	/**
	 * 生成当前年月日字符串
	 * @return
	 * @throws Exception
	 */
	public static String getCurrentDateStr()throws Exception{
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
		return sdf.format(date);
	}
	
	/**
	 * 获取指定范围内的日期集合
	 * @param before
	 * @param end
	 * @return
	 * @throws Exception
	 */
	public static List<String> getRangeDates(String before,String end)throws Exception{
		List<String> datas=new ArrayList<String>();
		Calendar cb = Calendar.getInstance();
		Calendar ce = Calendar.getInstance();
		cb.setTime(formatString(before,"yyyy-MM-dd"));
		ce.setTime(formatString(end,"yyyy-MM-dd"));
		datas.add(formatDate(cb.getTime(),"yyyy-MM-dd"));
		while(cb.before(ce)){
			cb.add(Calendar.DAY_OF_MONTH, 1);
			datas.add(formatDate(cb.getTime(),"yyyy-MM-dd"));
		}
		return datas;
	}
	
	/**
	 * 获取指定范围内的月份集合
	 * @param before
	 * @param end
	 * @return
	 * @throws Exception
	 */
	public static List<String> getRangeMonth(String before,String end)throws Exception{
		List<String> months=new ArrayList<String>();
		Calendar cb = Calendar.getInstance();
		Calendar ce = Calendar.getInstance();
		cb.setTime(formatString(before,"yyyy-MM"));
		ce.setTime(formatString(end,"yyyy-MM"));
		months.add(formatDate(cb.getTime(),"yyyy-MM"));
		while(cb.before(ce)){
			cb.add(Calendar.MONTH, 1);
			months.add(formatDate(cb.getTime(),"yyyy-MM"));
		}
		return months;
	}
	
	
	public static void main(String[] args) throws Exception{
		/*List<String> datas=getRangeDatas("2017-10-28","2017-11-02");
		for(String data:datas){
			System.out.println(data);
		}*/
		List<String> months=getRangeMonth("2017-09","2018-12");
		for(String month:months){
			System.out.println(month);
		}
	}
}
package com.jude.util;

/**
 * 数学工具类
 * @author Administrator
 *
 */
public class MathUtil {

	/**
	 * 格式化数字 保留两位
	 * @param n
	 * @return
	 */
	public static float format2Bit(float n){
		return (float)(Math.round(n*100))/100;
	}
}
package com.jude.util;

/**
 * 字符串工具类
 * @author jude
 *
 */
public class StringUtil {

	/**
	 * 判断是否是空
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str){
		if(str==null||"".equals(str.trim())){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 判断是否不是空
	 * @param str
	 * @return
	 */
	public static boolean isNotEmpty(String str){
		if((str!=null)&&!"".equals(str.trim())){
			return true;
		}else{
			return false;
		}
	}
	

	/**
	 * 生成四位编号
	 * @param code
	 * @return
	 */
	public static String formatCode(String code){
		try {
			int length = code.length();
			Integer num = Integer.valueOf(code.substring(length-4,length))+1;
			String codenum = num.toString();
			int codelength = codenum.length();
			for (int i = 4; i > codelength; i--) {
				codenum = "0"+codenum;
			}
			return codenum;
		} catch (NumberFormatException e) {
			return "0100";
		}
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值