9.日期工具

9.日期工具

/**
 * 返回日期年月日时分秒字符串格式
 * 
 * @param date
 *            日期
 * 
 * @return String 格式字符串
 * 
 */
public static String getStringOfYMD(Date date) {
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	return date == null ? "" : sdf.format(date);
}
/**
 * 返回字符串格式日期
 * 
 * @param date
 *            日期
 * 
 * @return String 格式字符串
 * 
 */
public static String getStringOfYMDEx(Date date) {
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
	return date == null ? "" : sdf.format(date);
}
/**
 * 时间戳转换成字符串
 */
public static String getDateToString(long time) {
	Date d = new Date(time);
	SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	return sf.format(d);
}
/**
 * 返回日期相差天数
 * 
 * @param bDate
 *            开始日期
 *            
 * @param aDate
 *            结束日期
 * 
 * @return String 格式字符串
 * 
 */
public static Integer getDaysBetweenDate(String bDate, String aDate) {
	Integer ret = 0;
	try {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (StringUtils.isNotEmpty(bDate) && StringUtils.isNotEmpty(aDate)) {
			Date dBDate = sdf.parse(bDate);
			Date dADate = sdf.parse(aDate);
			ret = Integer.parseInt(String.valueOf((dADate.getTime() - dBDate.getTime()) / (24 * 60 * 60 * 1000)));
		}
	} catch (Exception e) {
		ret = 0;
	}

	return ret;
}
/**
 * 返回当前月第一天
 * @return
 */
public static String getMonthFirstDate(){
	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00"); 
	Calendar   cal_1=Calendar.getInstance();//获取当前日期 
	cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
	String firstDay = format.format(cal_1.getTime());
	return firstDay;
}
/**
 * 获取当天是周几
 * @return
 */
public static int getWeek() {
	int week = 0;
	Date today = new Date();
	Calendar c = Calendar.getInstance();
	c.setTime(today);
	int weekday = c.get(Calendar.DAY_OF_WEEK);
	if (weekday == 1) {
		week = 7;
	} else if (weekday == 2) {
		week = 1;
	} else if (weekday == 3) {
		week = 2;
	} else if (weekday == 4) {
		week = 3;
	} else if (weekday == 5) {
		week = 4;
	} else if (weekday == 6) {
		week = 5;
	} else if (weekday == 7) {
		week = 6;
	}
	return week;
}

public static int dateToWeek(Date datet) {
    int[] weekDays = { 7,1,2,3,4,5,6 };
    Calendar cal = Calendar.getInstance(); // 获得一个日历
    cal.setTime(datet);
    int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
    if (w < 0)
        w = 0;
    return weekDays[w];
}


/**
  * 根据日期返回当天为周几
  * 
  * @param datetime
  * @return
*/
public static int dateToWeek(String datetime){
    try {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        return dateToWeek((Date) f.parse(datetime));
    } catch (Exception e) {
        e.printStackTrace();
        return 0;
    }
}
/**
 * 得到指定日期和天数和格式时间相加
 *
 * @param time
 *            时间
 * @param format
 *            格式 yyyy-MM-dd HH:mm:ss
 * @param day
 *            天数
 * @return String
 */
public static String specifiedTime(String time, String format, long day) {
	if (StringUtils.isEmpty(time)) {
		return null;
	} else {
		SimpleDateFormat dateFormat = new SimpleDateFormat(format); // 日期格式
		Date date = null;
		try {
			date = dateFormat.parse(time);// 指定日期
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Date newDate = specifiedDate(date, day); // 指定日期加上天数
		return dateFormat.format(newDate); // 将毫秒数转换成日期
	}
}

/**
  * 指定日期加上天数
  *
  * @param date
  *            时间
  * @param day
  *            天数
  * @return Date
  */
public static Date specifiedDate(Date date, long day) {
    long time = date.getTime(); // 得到指定日期的毫秒数
    day = day * 24 * 60 * 60 * 1000; // 要加上的天数转换成毫秒数
    time += day; // 相加得到新的毫秒数
    return new Date(time); // 将毫秒数转换成日期
}

/**
  * 指定日期减去天数
  *
  * @param date
  *            时间
  * @param day
  *            天数
  * @return Date
  */
public static Date subtractDate(Date date, long day) {
    long time = date.getTime(); // 得到指定日期的毫秒数
    day = day * 24 * 60 * 60 * 1000; // 要加上的天数转换成毫秒数
    time = time - day; // 相加得到新的毫秒数
    return new Date(time); // 将毫秒数转换成日期
}
	
/**
 * date转string类型 自定义格式类型
 *
 * @param currentTime
 *            date时间类型
 * @param type
 *            类型 yyyy-MM-dd HH:mm:ss
 * @return String
 */
public static String dateToStringType(Date currentTime, String type) {
	SimpleDateFormat formatter = new SimpleDateFormat(type);
	String dateString = formatter.format(currentTime);

	return dateString;
}
/**
 * 返回日期相差天数
 * 
 * @param bDate
 *            开始日期
 *            
 * @param aDate
 *            结束日期
 * 
 * @return String 格式字符串
 * 
 */
public static Integer getDaysBetweenDateByHMS(String bDate, String aDate) {
	Integer ret = 0;

	if(!StringUtils.isNotEmpty(bDate) || !StringUtils.isNotEmpty(aDate))
	{
		return 0;
	}
	if(bDate.length() == 10){
		bDate += " 00:00:00";
	}
	if(aDate.length() == 10){
		aDate += " 00:00:00";
	}
	if(bDate.length() == 16){ 
		bDate += ":00";
	}
	if(aDate.length() == 16){
		aDate += ":00";
	}

	try {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (StringUtils.isNotEmpty(bDate) && StringUtils.isNotEmpty(aDate)) {
			Date dBDate = sdf.parse(bDate);
			Date dADate = sdf.parse(aDate);
			ret = Integer.parseInt(String.valueOf((dADate.getTime() - dBDate.getTime()) / (24 * 60 * 60 * 1000)));
		}
	} catch (Exception e) {
		e.printStackTrace();
		ret = 0;
	}

	return ret;
}
/**
 * 比较时间大小
 *
 * @param DATE1
 *            当前时间
 * @param DATE2
 *            计划时间
 * @return
 */
public static Integer compareTime(String DATE1, String DATE2) {
	if (StringUtils.isEmpty(DATE2) || StringUtils.isEmpty(DATE1)) {
		return 0;
	} else {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date dt1 = null;
		Date dt2 = null;
		try {
			dt1 = df.parse(DATE1);
			dt2 = df.parse(DATE2);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		if (dt1.getTime() > dt2.getTime()) {
			// logger.info("dt1 大");
			return 1;
		} else if (dt1.getTime() < dt2.getTime()) {
			// logger.info("dt2 大");
			return 2;
		} else {
			return 2;
		}
	}
}
/**
 * 判断字符串是否是一个有效的日期
 *
 * @param theStr
 * @return boolean true 是,false否
 */
public static boolean isDate(String theStr) {
	return theStr.matches("\\d{4}\\-\\d{2}\\-\\d{2}");
}
/**
 * Java实现将阿拉伯数字转为汉字
 *
 * @param string
 * @return String
 */
public static String toChinese(String string) {
	String[] s1 = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
	String[] s2 = { "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千" };

	String result = "";

	int n = string.length();
	for (int i = 0; i < n; i++) {

		int num = string.charAt(i) - '0';

		if (i != n - 1 && num != 0) {
			result += s1[num] + s2[n - 2 - i];
		} else {
			result += s1[num];
		}
	}
	return result;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值