自己定義的常用工具類之一calendar日曆類


import java.util.Calendar;
import java.util.Date;

public class DateUtils {
	
	/**
	 * 獲得當前時間距離給定天數零點的毫秒時間
	 * @param amount
	 * @return
	 */
	public static Long getDelayTime(int amount){
		//1 設置當前時間
		Calendar calendar = Calendar.getInstance();
		Date newDate = new Date();
		calendar.setTime(newDate);
		//2 將時分秒設置成0
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		//3 設置指定天數
		calendar.add(Calendar.DATE, amount);
		//4 計算當前時間距離設置日期零點的延遲時間
		return calendar.getTimeInMillis() - newDate.getTime();
	}
	
	
	/**
	 * 當前時間具體明天零點的毫秒時間
	 * @return
	 */
	public static Long getDelayTime(){
		return getDelayTime(1);
	}
	
	
	/**
	 * 獲得一天的毫秒值
	 * @return
	 */
	public static Long getOneDay(){
		return 24 * 60 * 60 * 1000L;
	}
	
	/**
	 * 獲得幾月(兩位)
	 * @return
	 */
	public static String getCurrentMonth(){
		//1 設置當前時間
		Calendar calendar = Calendar.getInstance();
		Date newDate = new Date();
		calendar.setTime(newDate);
		
		int m = calendar.get(Calendar.MONTH) + 1;
		if(m < 10){
			return "0" + m;
		}
		return "" + m;
	}
	/**
	 * 獲得幾號(兩位)
	 * @return
	 */
	public static String getCurrentDay(){
		//1 設置當前時間
		Calendar calendar = Calendar.getInstance();
		Date newDate = new Date();
		calendar.setTime(newDate);
		
		int d = calendar.get(Calendar.DATE);
		if(d < 10){
			return "0" + d;
		}
		return "" + d;
	}
	
	
	public static void main(String[] args) {
		System.out.println(getCurrentMonth());
		System.out.println(getCurrentDay());
	}

}




發佈了41 篇原創文章 · 獲贊 10 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章