時間操作(Java版)—獲取給定日期N天后的日期


版權聲明:本文為博主原創文章。未經博主同意不得轉載。 https://blog.csdn.net/wangshuxuncom/article/details/34896777

        獲取給定日期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)// 應還書籍時間——日
		;
	}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM