Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

1. Review

Xiao Ming is sorting out a number of historical documents. There are many dates in these historical documents. Ming knew that these dates were between January 1, 1960, and December 31, 2059. To Xiao Ming’s headache, the format of these dates is very different, some use year/month/day, some use month/day/year, and some use day/month/year. To make matters worse, the first two digits of the year are also omitted, leaving many possible dates corresponding to a single date in the literature. For example, 02/03/04 could be March 04, 2002, February 03, 2004, or March 02, 2004. Given a date in the literature, can you help Xiao Ming determine what possible dates correspond to it

Enter a date in the format “AA/BB/CC”. (0 <= A, B, and C <= 9) Prints A number of different dates, one line for each date, in the format YYYY-MM-DD. Multiple dates are arranged from morning to night. Example Input: 02/03/04 Example Output: 2002-03-04 2004-02-03 2004-03-02

Where is yyyY-MM-DD

  • If it is a leap year, the second month has 29 days. If it is an ordinary year, the second month has 28 days. “Leap year: (1) Ordinary year: divisible by 4 but not by 100.
  • 1,3,5,7,8,10,12 are big months, so dd can only <=31 days. 4,6,9, and 11 are small months, so dd can only <=30 days

(That’s the point of this problem, so we need to pay attention to these points, and basically we have the problem.)

import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.Scanner; public class Main { static HashSet<Integer> hs = new HashSet<Integer>(); static ArrayList<Integer> al = new ArrayList<Integer>(); public static void isdata(String s1,String s2,String s3){ //1. Int a = integer.parseint (s1); int b =Integer.parseInt(s2); int c =Integer.parseInt(s3); if(a>=0 && a<60){ a = Integer.parseInt("20"+s1); } if(a>=60 && a<100){ a= Integer.parseInt("19"+s1); } if(((a%4 == 0 && a%100 ! =0 ) || a%400 == 0) && b==2 && (c>0 && c<=29)) { int data1 =Integer.parseInt(a+s2+s3+""); hs.add(data1); } / / judgment leap year else if (b = = 2 && (c > 0 | | c < = 28)) {int data2 = Integer. The parseInt (a + s2 + s3 + ""); hs.add(data2); } / / judgment in general if ((b = = 1 | | 3 | b = = = = 5 | | b | b = = 7 | | = = 8 b | | b = = 10 | | b = = 12) && (> 0 c & c < = 31)) {int data3 =Integer.parseInt(a+s2+s3+""); hs.add(data3); } if((b==4 || b==6|| b==9|| b==11) && (c>0 && c<=30 )){ int data3 =Integer.parseInt(a+s2+s3+""); hs.add(data3); } // for(int i = 0; i<al.size(); i++) //System.out.println(al[i]); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); String[] abc = str.split("/"); String a1 = abc[0]; String a2 = abc[1]; String a3 = abc[2]; isdata(a1,a2,a3); isdata(a3,a1,a2); isdata(a3,a2,a1); al.addAll(hs); Collections.sort(al); // for (int I: al) { System.out.println( ("" + i).substring(0, 4) + "-" + ("" + i).substring(4, 6) + "-" + ("" + i).substring(6, 8)); }}}Copy the code

(From my own blog post)