Some common uses of Calendar, such as determining whether the same day

Source: Internet
Author: User
Tags date1 diff

1, Calendar to determine whether the same day code is as follows :

Idea: The time format of two date types into the calendar type is compared to the date of two events

	public static Boolean issameday (date date1, date date2) {
		Calendar Caldatea = Calendar.getinstance ();
		Caldatea.settime (date1);

		Calendar caldateb = Calendar.getinstance ();
		Caldateb.settime (date2);

		Return Caldatea.get (calendar.year) = = Caldateb.get (calendar.year)
				&& caldatea.get (calendar.month) = = Caldateb.get (calendar.month)
				&& caldatea.get (calendar.day_of_month) = = Caldateb
						. Get (Calendar.day _of_month);
	}

2, Calendar calculation current time known current date

	public static String getyearandmonthday (int dayofmonth) {
		Calendar rightnow = Calendar.getinstance ();
		Date today = new Date (System.currenttimemillis ());
		Rightnow.settime (today);
		Rightnow.add (Calendar.month, 1); Righthow.add (calendar.month,1) return
			(Rightnow.get (calendar.year) + "year" +rightnow.get (calendar.month)) + "Month" + DayOfMonth + "Day";
	}
	
3, calendar calculation One months after the Today

	public static String getnextdate (int dayofmonth) {
		Calendar rightnow = Calendar.getinstance ();
		Date today = new Date (System.currenttimemillis ());
		Rightnow.settime (today);
		int day = Rightnow.get (calendar.day_of_month+1);
			Return Rightnow.get (calendar.year) + "-" + (Rightnow.get (calendar.month) + 2) + "-" + DayOfMonth;
	}
4, two time difference how much

	public static final int Getdayfromtimediff (date d1, date D2) {
		if (D1 = = NULL | | d2 = NULL) {return
			0;
		}
		long L1 = D1.gettime ();
		Long L2 = D2.gettime ();
		Long diff = l2-l1;
		int day = (int) (diff/(1000 * 3600 *));
		return day;
	}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.