首页 > 代码库 > 关于时间的操作(Java版)

关于时间的操作(Java版)

        本博文收录在编程过程中使用过的关于时间操作的Java代码:

        1、获取给定日期N天后的日期

import java.util.Calendar;

public class Test {
	public static void main(String[] args) {
		System.out.println(new Test().getDateAfterNDays("2012-05-10", 7));//输出2012-5-17
	}

	/**
	 * 获取给定日期N天后的日期
	 * 
	 * @author GaoHuanjie
	 */
	public String getDateAfterNDays(String dateTime, int days) {
		Calendar calendar = Calendar.getInstance();
		String[] dateTimeArray = dateTime.split("-");
		int year = Integer.parseInt(dateTimeArray[0]);
		int month = Integer.parseInt(dateTimeArray[1]);
		int day = Integer.parseInt(dateTimeArray[2]);
		calendar.set(year, month - 1, day);
		long time = calendar.getTimeInMillis();// 给定时间与1970 年 1 月 1 日的00:00:00.000的差,以毫秒显示
		calendar.setTimeInMillis(time + days * 1000 * 60 * 60 * 24);// 用给定的 long值设置此Calendar的当前时间值
		return calendar.get(Calendar.YEAR)// 应还书籍时间——年
				+ "-" + (calendar.get(Calendar.MONTH) + 1)// 应还书籍时间——月
				+ "-" + calendar.get(Calendar.DAY_OF_MONTH)// 应还书籍时间——日
		;
	}
}

        2、获取给定时间与当前系统时间的差值(以毫秒为单位)

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {

	/**
	 * 获取给定时间与当前系统时间的差值(以毫秒为单位)
	 * 
	 * @author GaoHuanjie
	 */
	public long getTimeDifference(String paramTime) {
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String systemTime = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss").format(new Date().getTime());// 获取系统时间
		long difference = 0;
		try {
			Date systemDate = dateFormat.parse(systemTime);
			Date lockDate = dateFormat.parse(paramTime);
			difference = systemDate.getTime() - lockDate.getTime();
			System.out.println("系统时间:" + systemTime + ",给定时间:" + paramTime 	+ ",给定时间与当前系统时间的差值(以毫秒为单位):" + difference + "毫秒");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return difference;
	}

	public static void main(String[] args) {
		new Test().getTimeDifference("2014-06-04 14:48:47");
	}
}

持续更新中,敬请期待!!!