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

public class CalendarTest {

	public static void main(String[] args) {
		test();
		System.out.println("--------------");
		test1();
		System.out.println("--------------");
		test2();
		System.out.println("--------------");
		test3();
		System.out.println("--------------");
		test4();
		System.out.println("--------------");
		System.out.println(test5(new Date(), -1));
	}

	// 3s后
	public static void test() {
		long start = System.currentTimeMillis() + 3000;
		Date date = new Date(start);
		System.out.println(new Date());
		System.out.println(date);
	}

	// 基本用法,获取年月日时分秒星期
	public static void test1() {
		Calendar calendar = Calendar.getInstance();
		// 获取年
		int year = calendar.get(Calendar.YEAR);

		// 获取月,这里需要需要月份的范围为0~11,因此获取月份的时候需要+1才是当前月份值
		int month = calendar.get(Calendar.MONTH) + 1;

		// 获取日
		int day = calendar.get(Calendar.DAY_OF_MONTH);

		// 获取时
		int hour2 = calendar.get(Calendar.HOUR_OF_DAY); // 24小时表示
		int hour = calendar.get(Calendar.HOUR);

		// 获取分
		int minute = calendar.get(Calendar.MINUTE);

		// 获取秒
		int second = calendar.get(Calendar.SECOND);

		// 获取毫秒
		int milliSecond = calendar.get(Calendar.MILLISECOND);

		// 星期,英语国家星期从星期日开始计算
		int weekday = calendar.get(Calendar.DAY_OF_WEEK);

		System.out.println("现在是" + year + "年" + month + "月" + day + "日" + hour2 + "(" + hour + ")" + "时" + minute + "分"
				+ second + "秒" + milliSecond + "毫秒" + "星期" + weekday);
	}

	// 一年后的今天
	public static void test2() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.YEAR, 1);

		Date time = calendar.getTime();
		System.out.println("一年后的今天:" + time);
	}

	// 这个月的第一天
	public static void test3() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		// calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1);
		System.out.println("这个月的第一天:" + calendar.getTime());
	}

	// 这个月的最后一天
	public static void test4() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, 1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.SECOND, -1);
		System.out.println("这个月的最后一天:" + calendar.getTime());
	}
	
	 // 在特定日期的基础上增加或者减少天数
	public static Date test5(Date date, Integer days) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		if (days != null) {
			calendar.add(Calendar.DAY_OF_MONTH, days);
		}
		return calendar.getTime();
	}

}

Fri Dec 07 21:38:04 CST 2018
Fri Dec 07 21:38:07 CST 2018
--------------
现在是2018年12月7日21(9)时38分4秒405毫秒星期6
--------------
一年后的今天:Sat Dec 07 21:38:04 CST 2019
--------------
这个月的第一天:Sat Dec 01 00:00:00 CST 2018
--------------
这个月的最后一天:Mon Dec 31 23:59:59 CST 2018
--------------
Thu Dec 06 21:38:04 CST 2018

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐