Date class - Calendar class - quiz

	public static void main(String[] args) throws Exception {
		function();
		function_01();
	}

	/*
	 * Leap year calculation
	 * Advanced algorithm: set the calendar to March 1 of the specified year, add offset 1 day forward, get the number of days, 29 leap year
	 */
	private static void function_01() {
		Calendar c = Calendar.getInstance();
		c.set(2048, 2, 1);
		c.add(Calendar.DAY_OF_MONTH, -1);
		System.out.println(c.get(Calendar.DAY_OF_MONTH));

	}

	private static void function() throws Exception {
		System.out.println("Please enter the birthday date format yyyy-MM-dd");
		String birthdayStr = new Scanner(System.in).nextLine();
		Date birthday = new SimpleDateFormat("yyyy-MM-dd").parse(birthdayStr);
		Date today = new Date();
		long gap = today.getTime() - birthday.getTime();
		System.out.println("Alive" + gap / 1000 / 60 / 60 / 24 + "days");
	}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324892687&siteId=291194637