The third-the week of the end of the 01 century

1. Title: Week at the end of the century

A cult once claimed that December 31, 1999 was the end of the world. Of course, the rumor is self-defeating. Some people say that December 31st at the end of a certain century in the future, if it is Monday, it will...
Interestingly, December 31st in any year at the end of the century cannot be Monday!! So, "rumor maker" Modified to Sunday again...
December 31st in 1999 is a Friday. May I ask: Which one of the nearest centuries in the future (ie xx99) will happen to be Sunday (ie Sunday) on December 31st?
Please answer the year (just write this 4-digit integer, do not write redundant information such as December 31)

Before doing the questions, first learn the Calendar class

The Calendar class is an abstract class. It provides some methods for the conversion between a specific moment and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, etc., and provides some methods for manipulating calendar fields (for example, obtaining the date of the next week) method.

Member methods:
> public static Calendar getlnstance()
public int get(int field)
public void add(int field, int amount): Operate the current calendar according to the given calendar field and the corresponding time.
public final void set(int year, int month, int date): Set the year, month and day of the current calendar

Focus:

  1. Example: The getInstance method of Calendar returns a Calendar object whose calendar field has been initialized with the current date and time:

    Calendar rightNow = Calendar.getInstance();
    public static Calendar getInstance()
    {
    return createCalendar(TimeZone.getDefault(), Locale.getDefault(Locale.Category.FORMAT));
    }

  2. The calendar field can be changed in three ways: set(), add(), poll()

  3. Year: Indicate the number of the year for get and set.

  4. Mouth: January is 0, so [0,11]

  5. DAY_OF_MONTH: Display the day of the month for get and set. The first day of the month is worth 1.

  6. DAY_OF_WEEK: SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,和 SATURDAY。分别对应:1,2,3,4,5,6,7

  7. HOUR: 12-hour clock (0-11). Noon and midnight represent 0 instead of 12

Insert picture description here

public static void main(String[] args) {
    
    
	Calendar calendar = Calendar.getInstance();
	for (int year = 1999;; year += 100) {
    
    
		calendar.set(Calendar.YEAR, year);// 要找year
		calendar.set(Calendar.MONTH, 11);// 12个月,第一个月从0开始因为我们要找的是12月
		calendar.set(Calendar.DAY_OF_MONTH, 31);// 月份第一天为1,我们要找的是31号
		if (calendar.get(Calendar.DAY_OF_WEEK) == 1) {
    
    // 如果该年的12月31日是星期天(星期天为1)
			System.out.println(year);
			break;
		}
	}

Answer: 2299

Supplementary questions:

1. Count how many days you have been in the world

Writing one:

public class MyYear {
    
    
	public static void main(String[] args) throws ParseException {
    
    
		// 键盘录入你的出生的年月日
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入你的出生年月日:");
		String line = sc.nextLine();

		// 把该字符串转换为一个日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date d = sdf.parse(line);

		// 通过该日期得到一个毫秒值
		long myTime = d.getTime();

		// 获取当前时间的毫秒值
		long nowTime = System.currentTimeMillis();

		// 得到一个差值
		long time = nowTime - myTime;

		// 把差值转换为年
		double day = time / 1000 / 60 / 60 / 24;
		System.out.println("你来到这个世界:" + day + "天");
	}
}

When inputting in the console, pay attention to: yyyy-mm-dd format, otherwise an error will occur!

Writing method two:

The method I wrote myself:
Avoid pits and lightning! ! ! Say silently three times: month starts at 0! month starts from 0! month starts from 0!

public static void main(String[] args) {
    
    
		Calendar calendar = Calendar.getInstance();
		// 键盘录入你的出生年月日
		Scanner input = new Scanner(System.in);
		System.out.println("请输入你的出生年月日:");
		int year = input.nextInt();
		int month = input.nextInt();
		int day = input.nextInt();
		// 得到一个毫秒值
		calendar.set(year, month - 1, day);
		Date date = calendar.getTime();
		long myTime = date.getTime();
		long nowTime = System.currentTimeMillis();
		long time = nowTime - myTime;
		double days = time / 1000 / 60 / 60 / 24;
		System.out.println("你来到这个世界:" + (long) days + "天");
	}

Insert picture description here
In addition, there is a pit. If we use the first method to write, the

SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);

It must be written like this. It cannot be written as yyyy-mm-d. The data will be wrong. The reason is that each letter, even if the case of the same letter, has a corresponding meaning. Can't write casually.
Insert picture description here

2. Get the number of days in February in any year

public static void main(String[] args) throws ParseException {
    
    
		// 输入任意年份
		// 设置日历对象的年月日
		// 把时间往前推一天,就是2月的最后一天
		// 输出这一天
		// 键盘录入你的出生年月日
		Scanner input = new Scanner(System.in);
		int year = input.nextInt();
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, 2, 1);
		calendar.add(Calendar.DATE, -1);
		System.out.println(calendar.get(Calendar.DATE));
	}

end.

Guess you like

Origin blog.csdn.net/weixin_44998686/article/details/108911264