【Java】日期时间API

本文深入探讨了Java中关于日期时间的三个核心类:Date、SimpleDateFormat和Calendar,包括它们的创建、常用方法、格式化及时间计算等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<pre name="code" class="java">//==========================日期时间相关API==================
public class DateDemo {
	private static final long LEVEL = 20150701;//本码讲版本
	/**=========================Date========================
	 * Date用于存储时间 如果用其获取或计算时间将不保证精确(均已过时) 
	 * 因此官方建议将Date用于表示一个固定的时间点
	 ==========================================================================*/
	public void testDate() {
		/*
		 * Date的创建和常用方法
		 */
		Date date = new Date();
		System.out.println("当前时间:" + date);
		//getTime() 返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
		long l = date.getTime();
		System.out.println("当前时间的Long值:" + l);
		// setTime() 设置此 Date 对象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的时间点。
		l += 1000 * 60 * 60 * 24;
		date.setTime(l);
		System.out.println("明天:" + date);
	}

	/**=====================SimpleDateFormat=====================
	 * SimpleDateFormat类继承DateFormat用来格式化日期时间
	 ================================================================================*/
	
	public void testSimpleDateFormat() {
		String pattern = "yyyy年MM月dd日 HH点mm分ss秒";
		/*
		 * Date--->String
		 */
		// 直接使用SimpleDateFormat
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		System.out.println("1.直接使用SimpleDateFormat   "
				+ format.format(new Date()));
		// 直接使用DateFormat
		DateFormat df = DateFormat.getDateInstance(DateFormat.FULL,
				Locale.ENGLISH);
		System.out.println("2.直接使用DateFormat   " + df.format(new Date()));
		//兼容性最好的创建方法 使用DateFormat的工厂方法实例化,可控使用哪个地区的时间表示
		SimpleDateFormat simple = (SimpleDateFormat) DateFormat
				.getDateInstance(DateFormat.FULL, Locale.CHINA);
		simple.applyPattern(pattern);
		System.out.println("3.使用DateFormat工厂方法实例化SimpleDateFormat   "
				+ simple.format(new Date()));
		/*
		 * String--->Date
		 */
		String str = "2008年08月08日 20点08分00秒";
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		try {
			Date date = sdf.parse(str);
			System.out.println("4.字符串2008年08月08日 20点08分00秒转换为时间:" + date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	/**====================Calendar=====================
	 * Calendar用于获取和计算时间
	 ====================================================================*/
	Calendar calendar = Calendar.getInstance();
	int year;
	int month;// 月从0开始
	int day;
	int week;
	@Test
	public void testCalendar() {
		/*
		 * Calendar--->date
		 */
		System.out.println("Calendar--->Date:  " + calendar.getTime());
		/*
		 * Date--->Calendar
		 */
		Date date = new Date();
		calendar.setTime(date);
		/*
		 * 操作Calendar设置时间
		 */
		//为当前Calendar设置时间 2008-08-08 20:08:00
		//方式一:直接赋值时间分量
		// cal.set(Calendar.YEAR, 2008);
		// cal.set(Calendar.MONTH, 8);
		// cal.set(Calendar.DAY_OF_MONTH, 8);//等效DATE
		//方式二:一次设置6种时间分量
		calendar.set(2008, Calendar.AUGUST, 8, 20, 8, 0);// 月从0开使 设置值超过限制会向上进位
		//对Calendar中时间的格式化
		SimpleDateFormat sdf = new SimpleDateFormat(
				"G w yyyy年MM月dd日 HH点mm分ss秒 E F W a z");
		System.out.println("3.手动设置Calendar时间:  "
				+ sdf.format(calendar.getTime()));
		
		/*
		 * 获取Calendar时间分量
		 */
		getCalendar();//见方法
		System.out.println("分别获取Calendar时间分量:" + year + "年" + month + '月' + day
				+ "日 星期" + (week == 1 ? 7 : week - 1));
		int dayOfYear = calendar.getActualMaximum(Calendar.DAY_OF_YEAR);
		System.out.println("计算当前年度的最大天数值: "+year + "共" + dayOfYear + "天");
		/*
		 * 使用Calendar进行时间计算
		 */
		calendar.add(Calendar.MONTH, 3);// 计算3个月后那天
		calendar.add(Calendar.DAY_OF_YEAR, 25);// 计算25天后那天
		getCalendar();// 星期从周日开始
		System.out.println("计算当前时间3月25天后的时间:  " + year + "年"
				+ month + '月' + day + "日" + "星期" + (week == 1 ? 7 : week - 1));
		calendar.add(Calendar.YEAR, -1);// 一年前的那天
		getCalendar();
		System.out.println("计算一年前的今天:  " + year + "年" + month
				+ '月' + day + "日" + "星期" + (week == 1 ? 7 : week - 1));
	}
	/**
	 * 分别获取Calendar时间分量
	 */
	private void getCalendar() {
		year = calendar.get(Calendar.YEAR);
		month = calendar.get(Calendar.MONTH) + 1;// 月从0开始
		day = calendar.get(Calendar.DATE);
		week = calendar.get(Calendar.DAY_OF_WEEK);
	}

}




                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xerophyte000

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值