java - return the day of the week based on the input date

/************************************************************   
Copyright (C), 1988-1999, Huawei Tech. Co., Ltd.   
FileName: main.cpp
  
Author: Light  
Version :   1.0       
Date:   2018/4/17
Description: Enter the date, return the day of the week // Module description         
Version: only for software testing, complete job // version info 
  
Function List: // Main functions and their functions     
1.
 History: 
  // history modification record 
      
	<author>  <time>   <version >   <desc>       
    Light 18/4/17 1.0 Build function

***********************************************************/

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class main {
public static void main(String[] args) { // TODO Auto-generated method stub String year = null;//year String month = null;//month String day = null;//Day //Enter the year, month and day @SuppressWarnings("resource") Scanner scanner1 = new Scanner(System.in); @SuppressWarnings("resource") Scanner scanner2 = new Scanner(System.in); @SuppressWarnings("resource") Scanner scanner3 = new Scanner(System.in); System.out.println("Please enter the year: "); year=scanner1.next(); System.out.println("Please enter the month: "); month=scanner2.next(); System.out.println("Please enter the date: "); day=scanner3.next(); //Check if input is responsive System.out.println( "The input time is: " + year + "year" + month + "month" + day + "day"); //Check if the input is a number or empty if(!isNumber(year) || !isNumber(month) || !isNumber(day) ) { System.out.println("It is detected that your input is invalid, please enter a number!"); } else { int year_rual=Integer.parseInt(year); int month_rual=Integer.parseInt(month); int day_rual=Integer.parseInt(day); //Check if the entered date is valid if (judge(year_rual,month_rual,day_rual)) {   String weekday=weekByDate(year_rual,month_rual,day_rual);   System.out.println( year + " 年" + month + " 月" + day + " 日是 " + weekday); } else {   System.out.println("It is detected that your input is invalid, please enter a valid date!"); } } } /** * Determine whether the entered year, month, and day are numbers or empty * @param number //input content * @return boolean//The return value is true to meet the rules, the return value of false does not meet the rules */ public static boolean isNumber(String number) { if (number == null || "".equals(number.trim())) { return false; } Pattern pattern = Pattern.compile("[0-9]*"); Matcher isNum = pattern.matcher (number.trim ()); if (isNum.matches()) { return true; } else { return false; } } /** * Determine whether the entered year, month, and date conform to the rules * @param year //year * @param month // month * @param day //day * @return boolean//The return value is true to meet the rules, the return value of false does not meet the rules */ public static boolean judge (int year,int month,int day) {  //When the input number is less than zero, return false   if (year <= 0)   {    return false;    }   if (month <= 0 || month > 12)
      {     return false;   } if (day<=0 || day > 31){ return false; } //The year is divisible by 4 and not divisible by 100, or if it is divisible by 400, it is a leap year if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { if (month == 2) { //February in leap year if (day > 29) { return false; } } }else{ // February is not a leap year if (month == 2) { if (day > 28) { return false; } } } //1, 3, 5, 7, 8, 10, and December are 31 days int[] m1 = {1,3,5,7,8,10,12}; for (int i = 0; i < m1.length; i++){ if (month == m1[i]) { if (day>31) { return false; } } } //4,6,9,12 months are 30 days int[] m2 = {4,6,9,11}; for (int j = 0; j < m2.length; j++) { if (month == m2[j]) { if (day > 30) { return false; } } } return true; } /** * Return the day of the week according to the year, month and day * @param year //year * @param month //month * @param day //day * @return String //The return value directly returns the day of the week */ public static String weekByDate (int year,int month,int day) { String str=""; SimpleDateFormat fmt = new SimpleDateFormat("dd MM yyyy"); Date d = null; try { d = fmt.parse(day+" "+month+" "+year); }
     catch (ParseException e)
{   // TODO Auto-generated catch block   e.printStackTrace (); } Calendar cal = Calendar.getInstance(); cal.setTime(d); int weekDay = cal.get(Calendar.DAY_OF_WEEK); switch(weekDay) { case 1 : str="SUNDAY"; break; case 2 : str="MONDAY"; break; case 3 : str="TUESDAY"; break; case 4 : str="WEDNESDAY"; break; case 5 : str="THURSDAY"; break; case 6 : str="FRIDAY"; break; case 7 : str="SATURDAY"; break; default: break; } return str; } }

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324513253&siteId=291194637