欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java 计算两个日期之间相差天数

程序员文章站 2022-07-12 17:49:54
...

 

		{
			int y1 = 2016, m1 = 9, d1 = 29;
			int y2 = 2014, m2 = 11, d2 = 8;

			GregorianCalendar gcCalendar1 = new GregorianCalendar(y1, m1 - 1, d1);
			GregorianCalendar gcCalendar2 = new GregorianCalendar(y2, m2 - 1, d2);

			long timeMillis = gcCalendar1.getTimeInMillis() - gcCalendar2.getTimeInMillis();

			long days = timeMillis / (24 * 60 * 60 * 1000);
			System.out.println(days);

			SimpleDateFormat dftDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			System.out.println(dftDate.format(gcCalendar1.getTime()));
			System.out.println(dftDate.format(gcCalendar2.getTime()));
		}