Date练习题

输入你的生日,计算你来到世界多少天了?

 public static void main(String[] args) throws ParseException {
        Scanner s = new Scanner(System.in);
        System.out.println("请输入你的出生日期");
        String birthday = s.nextLine();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd");
        Date date = simpleDateFormat.parse(birthday);
        long btime = date.getTime();
        Date date1 = new Date();
        long nowtime = date1.getTime();
        long day = nowtime - btime;
        System.out.println("您已经来到世界" + day / 100 / 60 / 60 / 24 + "天了");

    }

计算某年的二月有几天

 public static void main(String[] args) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        Scanner scanner = new Scanner(System.in);

        System.out.println("输入年份");
        int year = scanner.nextInt();

        calendar.set(year, 2, 1);
        calendar.add(Calendar.DATE, -1);
      
        System.out.println(year + "年的二月有" + calendar.get(Calendar.DATE) + "天");

    }

给定一个年月日,打印出该月的日历

  public static void main(String[] args) throws ParseException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您要查日历的具体日期(格式:年/月/日)");
        String insert = scanner.nextLine();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        Date date = simpleDateFormat.parse(insert);
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        int insertday = calendar.get(Calendar.DATE);
        calendar.set(Calendar.DATE, 1);
        int fastdayofweek = calendar.get(Calendar.DAY_OF_WEEK);
        //  System.out.println(fastdayofweek);
        int lastday = calendar.getActualMaximum(Calendar.DATE);

        int[] calen = new int[42];
        for (int i = 1; i <= lastday; i++) {
            calen[fastdayofweek - 1] = i;
            fastdayofweek++;
        }
        System.out.println("日\t一\t二\t三\t四\t五\t六");
        for (int i = 0; i < fastdayofweek; i++) {
            if (calen[i] == 0) {
                System.out.print("\t");
            } else {
                System.out.print(calen[i] + "\t");
            }

            if ((i + 1) % 7 == 0) {
                System.out.println();
            }
        }


    }

你可能感兴趣的:(Date练习题)