HRBUST-1555 correct date format (java version)

Topic Link

https://vjudge.net/problem/HRBUST-1555

Thinking

First need to determine the date in line accounted for four years, accounting for two month, accounting for two days
and then use SimpleDateFormat define the format of the input transformation, catch the exception, if it is not correspond to the corresponding output format on abnormal

Code

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			String str = sc.nextLine();
			// 先需要判断年月日符合年份占四位,月份占两位,日占两位

			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Date date;
			try {
				date = dateFormat.parse(str);
				String[] split = str.split("-");
				int year = Integer.parseInt(split[0]);
				int mon = Integer.parseInt(split[1]);
				int day = Integer.parseInt(split[2]);
				if (year <= 999 || year >= 10000 || mon > 12 || mon <= 0
						|| day <= 0 || day > 31) {
					System.out.println("No");
				}else {
					System.out.println("Yes");
				}
			} catch (ParseException e) {
				System.out.println("No");
			}
		}

		sc.close();

	}

}

Explanation

This code has a problem when submitted, will be reported Memory Limit Exceeded, input and output is correct, do not know what went wrong, can only be put here.

Published 60 original articles · won praise 4 · Views 1255

Guess you like

Origin blog.csdn.net/qq_43966129/article/details/105356613