关于java:代码-特殊日期特殊日期

8次阅读

共计 1402 个字符,预计需要花费 4 分钟才能阅读完成。

问题形容

一个日期由年、月、日组成, 年份为四位数, 月不超过两位, 日期为不超过两位, 小明喜爱把年月日连起来写, 当月或日期的长度为一位时在后面补 0, 这样造成一个八位数。例如, 2018 年 1 月 3 日写成 20180103 , 而 2018 年 11 月 15 日写成 20181115 小明发现, 这样写好, 有些日期中呈现了 3 位间断的数字, 小明称之为非凡日期。例如,20181115 就是这样一个数, 两头呈现了间断的 3 个 1, 当然, 2011 年 11 月 11 日也是这样一个日期。给定一个起始日期和一个完结日期, 请计算这两个日期之间 (蕴含这两个日期) 有多少个非凡日期。

代码
package Ring1270.pra.java01;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;
/**
 * Question description: *      One date contain year,month and day, year is four number, month is two number and day is two number *      if there has three and more numbers are similar with each other be called special date like 20211105, *      Statistics special date when setting a start date and a end date * */
public class E_SpecialDate {public static void main(String[] args) throws ParseException {Scanner scanner = new Scanner(System.in);
        System.out.println("Input start date:");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String start = scanner.nextLine();
        System.out.println("Input end date:");
        String end = scanner.nextLine();
        Calendar st = Calendar.getInstance();
        Calendar ed = Calendar.getInstance();
        st.setTime(simpleDateFormat.parse(start));
        ed.setTime(simpleDateFormat.parse(end));
        char[] D;
        int count = 0;
        System.out.println("The special date:");
        while (!st.after(ed)) {st.add(Calendar.DAY_OF_YEAR, 1);
            D = simpleDateFormat.format(st.getTime()).toCharArray();
            for (int i = 0; i <= 5; i++) {if (D[i] == D[i + 1] && D[i] == D[i + 2]) {System.out.println(D);
                    count++;
                    break;
                }
            }
        }
        System.out.println(count);
    }
}
运行截图

正文完
 0