zoukankan      html  css  js  c++  java
  • java

      设计函数实现输入日期显示星期几。

      下面为程序的代码:

    /************************************************************   
    Copyright (C), 1988-1999, Huawei Tech. Co., Ltd.   
    FileName: main.cpp
      
    Author: Light  
    Version :   1.0       
    Date:   2018/4/17
    Description: 输入日期,返回星期几     // 模块描述         
    Version:  仅用于软件测试,完成作业// 版本信息 
      
    Function List:     // 主要函数及其功能     
    1.
     History: 
      // 历史修改记录 
          
    	<author>  <time>   <version >   <desc>       
        Light    18/4/17     1.0        建立函数
    
    ***********************************************************/
    
    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;//年份 String month = null;//月份 String day = null;//日 //输入年月日 @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("请输入年份:"); year=scanner1.next(); System.out.println("请输入月份:"); month=scanner2.next(); System.out.println("请输入日:"); day=scanner3.next(); //检测输入是否响应 System.out.println( "输入的时间为:" + year + "年" + month + "月" + day + "日"); //检测输入的内容是否为数字,或为空 if(!isNumber(year) || !isNumber(month) || !isNumber(day) ) { System.out.println("检测到您的输入不合法,请输入数字!"); } else { int year_rual=Integer.parseInt(year); int month_rual=Integer.parseInt(month); int day_rual=Integer.parseInt(day); //判断输入的日期是否合法 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("检测到您的输入不合法,请输入合法日期!"); } } } /** * 判断输入的年月日是否为数字,或为空 * @param number //输入内容 * @return boolean//返回值为true符合规则,返回值为false不符合规则 */ 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; } } /** * 判断输入的年月日是否符合规则 * @param year //年份 * @param month // 月份 * @param day //天 * @return boolean//返回值为true符合规则,返回值为false不符合规则 */ public static boolean judge (int year,int month,int day) {  //当输入的数字小于零时,返回false   if (year <= 0)   {    return false;    }   if (month <= 0 || month > 12)
          {     return false;   } if (day<=0 || day > 31){ return false; } //年份能被4整除并且不能被100整除,或者能被400整除,则为闰年 if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { if (month == 2) { //闰年的2月 if (day > 29) { return false; } } }else{ //不是闰年的2月 if (month == 2) { if (day > 28) { return false; } } } //1、3、5、7、8、10、12月份为31天 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月份为30天 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; } /** * 根据年月日返回星期几 * @param year //年份 * @param month //月份 * @param day //天 * @return String //返回值直接返回星期几 */ 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; } }

      

  • 相关阅读:
    畅通project续
    mysql两列合成一列
    stl 之set图解
    Random Forest 与 GBDT 的异同
    手机游戏加密那点事儿_前言_0
    onfocus事件,onblur事件;Focus()方法,Blur()方法
    Oracle数据库导出导入
    数据文件限制大小
    oracle级联操作
    Material UI:很强大的CSS框架
  • 原文地址:https://www.cnblogs.com/suifengye/p/8862476.html
Copyright © 2011-2022 走看看