Java獲取前一天時間和判斷某個時間段

java獲取前一天時間SimpleDateFormat

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

public class Test4 {
	public static void main(String[] args) {
		SimpleDateFormat predf = new SimpleDateFormat("yyyy-MM-dd");
		Date d = new Date();
		String predate = predf.format(new Date(d.getTime() - (long) 24 * 60	* 60 * 1000));//減去24小時,即前一天
		System.out.println(predate);
	}
}

java判斷某個時間段

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

public class Test4 {
	public static void main(String[] args) {
		SimpleDateFormat dfh = new SimpleDateFormat("HH");
		String h = dfh.format(new Date());

		if (Integer.parseInt(h) > 2 && Integer.parseInt(h) < 10) {
			System.out.println("時間10:00-2:00未到");
		}
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章