java 时间处理

import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeUtil {

	private TimeUtil() {
	}

	public static Date strToDate(String sStr) {
		if (sStr == null)
			return null;
		SimpleDateFormat formatter;
		if (sStr.length() == 19)
			formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		else if (sStr.length() == 10)
			formatter = new SimpleDateFormat("yyyy-MM-dd");
		else if (sStr.length() == 8)
			formatter = new SimpleDateFormat("yyyyMMdd");
		else if (sStr.length() == 14)
			formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		else
			formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		ParsePosition pos = new ParsePosition(0);
		return formatter.parse(sStr, pos);
	}

	public static Timestamp strToDatetime(String s) {
		return new Timestamp(strToDate(s).getTime());
	}

	public static String datetimeToChinese(Date dtSource) {
		SimpleDateFormat formatter = new SimpleDateFormat(
				"yyyy年MM月dd日HH时mm分ss秒");
		return formatter.format(dtSource);
	}

	public static String dateToStr(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		return format.format(date);
	}

	public static String datetimeToStr(Date date) {
		if (date == null) {
			return "";
		} else {
			SimpleDateFormat formatter = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			return formatter.format(date);
		}
	}

	public static String today() {
		return dateToStr(new Date());
	}

	public static String now() {
		return datetimeToStr(new Date());
	}

	public static String calcTime(String time, int diffYear, int diffMonth,
			int diffDate, int diffHour, int diffMinute, int diffSecond) {
		Timestamp timestamp = strToDatetime(time);
		timestamp.setYear(timestamp.getYear() + diffYear);
		timestamp.setMonth(timestamp.getMonth() + diffMonth);
		timestamp.setDate(timestamp.getDate() + diffDate);
		timestamp.setHours(timestamp.getHours() + diffHour);
		timestamp.setMinutes(timestamp.getMinutes() + diffMinute);
		timestamp.setSeconds(timestamp.getSeconds() + diffSecond);
		return datetimeToStr(timestamp);
	}

	public static String getCurrentTime() {
		String s = (new Time(System.currentTimeMillis())).toString();
		return s;
	}

	public static String getCurrentDate() {
		String s = (new java.sql.Date(System.currentTimeMillis())).toString();
		return s;
	}

	public static String getCurrentNow() {
		String s = getCurrentDate() + " " + getCurrentTime();
		return s;
	}

	public static String dateFromStr(String date) {
		if (date == null || date.length() < 10)
			return getCurrentDate();
		else
			return date.substring(0, 10);
	}

	public static String timeFromStr(String date) {
		if (date == null || date.length() < 19)
			return "00:00:00";
		else
			return date.substring(11, 19);
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值