A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

来凑凑热闹喽,
回复 使用道具 举报
看看会不会做啊
回复 使用道具 举报
嗯,我想试试
回复 使用道具 举报
我来看看!
回复 使用道具 举报
  1. package test;

  2. import java.text.*;
  3. import java.util.*;

  4. public class Test
  5. {
  6.         /**
  7.          * @param args
  8.          * @throws ParseException
  9.          */
  10.         public static void main(String[] args) throws ParseException
  11.         {
  12.                 System.out.println("请输入一个日期,格式为:yyyy-mm-dd,如2000-01-01");
  13.                 Scanner c=new Scanner(System.in);//输入
  14.                 String strdate=c.next();
  15.                 String pat = "yyyy-MM-dd" ;
  16.                 SimpleDateFormat sdf = new SimpleDateFormat(pat) ;// 实例化模板对象
  17.                 Date d=sdf.parse(strdate);
  18.                 String str="1898-01-01";
  19.                 Date d2=sdf.parse(str);
  20.                 long day=(d.getTime()-d2.getTime())/(1000*24*60*60)+1;//相减
  21.                 System.out.println("相差"+day+"天");
  22.         }

  23. }
复制代码

评分

参与人数 1技术分 +2 收起 理由
神之梦 + 2 不错

查看全部评分

回复 使用道具 举报
import java.io.*;
public class demo {

        /**
         *
         *  输入某年某月,判断这一年与这一月分别距离1898年1月1日多少天?
         *  程序分析:以2013年7月为例,使用循环计算1898年1月1日距离2013年1月1日的总天数(考虑闰年与平年的情况)
         *   再计算2013年1月1日距离7月的总天数
         *
         * 思路:首先算是此年中的第几天,用switch累计一下得出;
         *       再算此年的第一天离1898.1.1过了多少天,然后与前一步累加即可。
         */
        //此方法用于算这是一年中的第几天
        public static int date(int year,int month,int day){
                int sum=0;
                switch(month){
                case 12:sum+=30;
                case 11:sum+=31;
                case 10:sum+=30;
                case 9:sum+=31;
                case 8:sum+=31;
                case 7:sum+=30;
                case 6:sum+=31;
                case 5:sum+=30;
                case 4:sum+=31;
                case 3:sum+=((year%4==0)&(year%100!=0))|(year%400==0)?29:28;
                case 2:sum+=31;
                case 1:sum+=day;
                }
                return sum;
        }
        //此方法使用循环计算1898年1月1日距离2013年1月1日的总天数,然后累加计算天数
        public static int days(int year,int num){
          int sum=0,a=year-1898;
          if(a>0){
                  //这是输入的年份在1898之后
                 for (int i = 1898; i < year; i++) {
                         sum+=((i%4==0)&(i%100!=0))|(i%400==0)?366:365;
                }
                 num+=sum;
                 return num;
          }
          else if (a<0){
                  //这是输入的年份在1898之前
                  for (int i =year ; i <1898 ; i++) {
                                 sum+=((i%4==0)&(i%100!=0))|(i%400==0)?366:365;
                        }
                  num=sum-num;
                  return num;
          }
          //这是输入年份为1898年的情况
          num+=sum;
          return num;
        }
        public static void main(String[] args) {
                System.out.println("请按yyyy-mm-dd(年-月-日,月日均为2位)格式输入日期:");
                // 获取输入的日期
        BufferedReader bf =new BufferedReader(new InputStreamReader(System.in));
            String s=null;
            try{s=bf.readLine();}
            catch(Exception e){e.printStackTrace();};
            //截取年,月,日,变成int类型
            int year=0,month=0,day=0;
                   year = Integer.valueOf(s.substring(0,4));  
            month = Integer.valueOf(s.substring(5,7));
            day = Integer.valueOf(s.substring(8,10));   
            //计算天数
            int num;
            num=date(year, month, day);
            //由于num是第几天,算距离的时候应该减一,传入num-1能算出相隔的天数
            System.out.println(year+"年"+month+"月"+day+"日距离1898年1月1日一共有"+days(year,num-1)+"天");
        }

}

评分

参与人数 1技术分 +2 收起 理由
神之梦 + 2 赞一个!

查看全部评分

回复 使用道具 举报
做完了题上交了~望老师能给点指导意见~
回复 使用道具 举报
牛牛 发表于 2013-8-1 21:01
/**
* 题目:
*  输入某年某月,判断这一年与这一月分别距离1898年1月1日多少天?

希望老师能给点指导
回复 使用道具 举报
瞅瞅。。。
回复 使用道具 举报

:dizzy:时间到了,可是还有好多bug。。
回复 使用道具 举报
抢一个看看
回复 使用道具 举报
  1. package com.itheima;

  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;

  4. public class DaysTest {
  5.         public static void main(String[] args) {
  6.                 try {
  7.                         InputStreamReader isr = new InputStreamReader(System.in);
  8.                         BufferedReader br = new BufferedReader(isr);
  9.                         System.out.println("请输入年月(格式:yyyymm):");//按格式输入年月
  10.                         String s = br.readLine();
  11.                         int y = Integer.parseInt(s.substring(0, 4));//截取字符串中的年
  12.                         int m = Integer.parseInt(s.substring(4, 6));//截取字符串中的月
  13.                         int num = count(y, m);
  14.                         System.out.println("距离1898年1月有" + num + "天");
  15.                 } catch (Exception e) {
  16.                         e.printStackTrace();
  17.                 }
  18.         }

  19.         public static int count(int year, int month) {
  20.                 int num = 0;
  21.                 int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };//平年的月份
  22.                 int days2[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };//闰年的月份
  23.                 if (year < 1898) {    //计算在1898年之前的日期
  24.                         for (int i = year; i < 1898; i++) {
  25.                                 if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {//计算年的距离时间
  26.                                         num = num + 366;
  27.                                 } else {
  28.                                         num = num + 365;
  29.                                 }
  30.                         }
  31.                         for (int j = 0; j < month - 1; j++) {//减去多余的月的距离时间
  32.                                 if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
  33.                                         num = num - days2[j];
  34.                                 else
  35.                                         num = num - days[j];
  36.                         }
  37.                 } else {     //计算在1898年之后的日期
  38.                         for (int i = year; i >= 1898; i--) {
  39.                                 if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
  40.                                         num = num + 366;
  41.                                 } else {
  42.                                         num = num + 365;
  43.                                 }
  44.                         }
  45.                         for (int j = month-1; j < days.length; j++) {
  46.                                 if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
  47.                                         num = num - days2[j];
  48.                                 else
  49.                                         num = num - days[j];
  50.                         }
  51.                 }
  52.                 return num;
  53.         }

  54. }
复制代码
运行结果
请输入年月(格式:yyyymm):
190501
距离1898年1月有2556天

评分

参与人数 1技术分 +1 收起 理由
神之梦 + 1 赞一个!

查看全部评分

回复 使用道具 举报
看题目呢
回复 使用道具 举报
  1. <pre id="recommend-content-887653677" accuse="aContent" class="recommend-text mb-10" style="margin-bottom: 10px; font-family: arial, 'courier new', courier, 宋体, monospace; white-space: pre-wrap; color: rgb(51, 51, 51); line-height: 24px; background-color: rgb(241, 254, 221); ">import java.util.Scanner;

  2. public class test2 {
  3.         public static void main(String[] args) {
  4.                 Scanner s = new Scanner(System.in);

  5.                 System.out.print("请选择年份:");
  6.                 int year = s.nextInt();// 输入的年
  7.                 System.out.print("请选择月份:");
  8.                 int month = s.nextInt();// 输入的月

  9.                 s.close();

  10.                 int days = 0;// 统计天数
  11.                 for (int i = 1898; i < year; i++) {
  12.                         days += getDaysByYear(i);
  13.                 }

  14.                 int[] days_each_month = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  15.                 if (isLeepYear(year)) {
  16.                         days_each_month[1] = 29;
  17.                 }
  18.                 System.out.println("输入年份距离1898年1月1日的天数:" + days);

  19.                 for (int i = 0; i < month - 1; i++) {
  20.                         days += days_each_month[i];
  21.                 }
  22.                 System.out.println("输入月份距离1898年1月1日的天数:" + days);
  23.                 System.out.println("当前月份的天数:" + days_each_month[month]);
  24.         }

  25.         // 获得某年的天数
  26.         private static int getDaysByYear(int year) {
  27.                 if (isLeepYear(year)) {
  28.                         return 366;
  29.                 } else {
  30.                         return 365;
  31.                 }
  32.         }

  33.         // 判断是否是闰年
  34.         private static boolean isLeepYear(int year) {
  35.                 return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
  36.         }
  37. }</pre>
复制代码

评分

参与人数 1技术分 +1 收起 理由
神之梦 + 1 赞一个!

查看全部评分

回复 使用道具 举报
牛牛 发表于 2013-8-2 12:07
希望老师能给点指导

那版主多出点题~让我们好得技术分呀。
回复 使用道具 举报
都有些什么题呢?瞧瞧
回复 使用道具 举报
试试看,顶顶
回复 使用道具 举报
晚上看题  如果3小时就是要白天了啊
回复 使用道具 举报
  1. import java.util.Scanner;

  2. public class Waq{
  3.         public static void main(String[] args) {
  4.                 Scanner str = new Scanner(System.in);
  5.                 // 输入的年
  6.                 System.out.print("请输入年份:");
  7.                 int year = str.nextInt();
  8.                 // 输入的月
  9.                 System.out.print("请输入月份:");
  10.                 int month = str.nextInt();

  11.                 str.close();
  12.                 // 统计天数
  13.                 int days = 0;
  14.                 for (int i = 1989; i < year; i++) {
  15.                         days += getBY(i);
  16.                 }

  17.                 int[] daysEM = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  18.                 if (isLY(year)) {
  19.                         daysEM[1] = 29;
  20.                 }
  21.                 System.out.println("输入年份距离1989年1月1日的天数:" + days);

  22.                 for (int i = 0; i < month - 1; i++) {
  23.                         days += daysEM[i];
  24.                 }
  25.                 System.out.println("输入月份距离1989年1月1日的天数:" + days);
  26.         }
  27.         // 判断是否是闰年
  28.                 private static boolean isLY(int year) {
  29.                         return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
  30.                 }
  31.         // 获得某年的天数
  32.         private static int getBY(int year) {
  33.                 if (isLY(year)) {
  34.                         return 366;
  35.                 } else {
  36.                         return 365;
  37.                 }
  38.         }       
  39. }
复制代码
这题以前做过 就直接把以前做的复制过来吧,以前是按照数组的方式做的,因为那时候老师布置的,按照别人的思路说数组,就用的数组去实现的!;P

评分

参与人数 1技术分 +1 收起 理由
神之梦 + 1 汗、、、、、、、

查看全部评分

回复 使用道具 举报
本帖最后由 浪无痕-陈文坤 于 2013-8-3 00:21 编辑

  1. //按全部月份计算
  2. import javax.swing.JOptionPane;
  3. class Time11
  4. {
  5. public int year;
  6. public int month;
  7. public int alldays;//所有的天数;
  8. public int weekDay;//一月当中的第一天是星期几;
  9. public int monthDays;//每月的天数
  10. public boolean isRn;//判断是否闰年
  11. public Time11()
  12. {         //添加了年份弹出框输入功能:加了提示信息防止出错
  13.         while(year<1898)
  14.         {
  15.                 String Year=JOptionPane.showInputDialog("请输入年份,必须大于1898年! ");
  16.                 year=Integer.parseInt(Year);
  17.         }
  18.         //添加了月份弹出框输入功能:加了提示信息防止出错
  19.         while(!(month>=1&&month<=12))
  20.         
  21.         {
  22.                 String Month=JOptionPane.showInputDialog("请输入月份 ,必须在1~12之间!");
  23.                 month=Integer.parseInt(Month);
  24.         }
  25.         //判断是否是闰年还是平年
  26.         //isRn=year%4==0&&year%100!=0||year%400==0;
  27. }
  28.         public int getMonths(int month)
  29.         {
  30.                 for (int i=1;i<month;i++)
  31.                 {
  32.                         switch(i)
  33.                         {
  34.                                 case 4:
  35.                                 case 6:
  36.                                 case 9:
  37.                                 case 11:
  38.                                                 alldays +=30;
  39.                                                 break;
  40.                                 case 2:
  41.                                         if(isRn)
  42.                                         {
  43.                                                 alldays +=29;
  44.                                         }
  45.                                         else
  46.                                         {
  47.                                                 alldays +=28;
  48.                                         }
  49.                                         break;
  50.                                 default:
  51.                                                 alldays +=31;
  52.                                        
  53.                                         }
  54.                         }
  55.                 return alldays;
  56.         }
  57.         public int getDays()
  58.         {        //获取从1898到(year-1)之间的总天数
  59.                 for(int i=1898;i<year;i++)
  60.                 {
  61.                         getMonths(12);
  62.                 //月份相对应的天数和year年的总天数
  63.                 }
  64.                         
  65.                 return getMonths(month);
  66.                         
  67.         }
  68. }
  69. public class TimeDemo2
  70. {
  71.         public static void main(String[] args)
  72.         {
  73.                 System.out.println(new Time().getDays());
  74. }
  75. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马