「java输入年份输出日期」java 输出日期

博主:adminadmin 2022-11-22 18:17:09 50

本篇文章给大家谈谈java输入年份输出日期,以及java 输出日期对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

(JAVA)输入年月日,计算日期是今年的第几天?

import java.util.Scanner;

/**

* Created by xpf on 2018/6/22 :)

* GitHub:xinpengfei520

* Function:

*/

public class CalculateUtils {

/*平年二月28天*/

private static final int DAYS_28 = 28;

/*闰年二月29天*/

private static final int DAYS_29 = 29;

/*除了31天的月份其他均为30天*/

private static final int DAYS_30 = 30;

/*1、3、5、7、8、10、12月份31天*/

private static final int DAYS_31 = 31;

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("Please input year:");

int year = input.nextInt();

System.out.println("Please input month:");

int month = input.nextInt();

System.out.println("Please input day:");

int day = input.nextInt();

int daysInYear = getDaysInYear(year, month, day);

System.out.println("daysInYear:" + daysInYear);

}

/**

* get days in this year

*

* @param year

* @param month

* @param day

* @return

*/

public static int getDaysInYear(int year, int month, int day) {

int totalDays = 0;

switch (month) {

// 12 月份加的是11月份的天数,依次类推

case 12:

totalDays += DAYS_30;

case 11:

totalDays += DAYS_31;

case 10:

totalDays += DAYS_30;

case 9:

totalDays += DAYS_31;

case 8:

totalDays += DAYS_31;

case 7:

totalDays += DAYS_30;

case 6:

totalDays += DAYS_31;

case 5:

totalDays += DAYS_30;

case 4:

totalDays += DAYS_31;

case 3:

// 判断是否是闰年

if (((year / 4 == 0) (year / 100 != 0)) || (year / 400 == 0)) {

totalDays += DAYS_29;

} else {

totalDays += DAYS_28;

}

case 2:

totalDays += DAYS_31;

case 1: // 如果是1月份就加上输入的天数

totalDays += day;

}

return totalDays;

}

}

【解题思路】

1、通过年份区分是闰年还是平年,平年 2 月 28 年,闰年 2 月 29 天。

2、1、3、5、7、8、10、12 月份为 31 天,其余月份为 30 天。

3、将每个月的天数相加即可,如果输入的是 12 月,则从 11 月往前累加到1月。

扩展资料

其他java计算日期的方式

package study01;

import java.util.Scanner;

public class TestDay {

/*

* 输入2017年的月和日:month=?,day=? 输出输入的日期是2017年的第几天,使用switch完成

*/

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("month=");

int month = sc.nextInt();

System.out.print("day=");

int day = sc.nextInt();

int days = 0;

switch (month) {

case 12:

days += 30;

case 11:

days += 31;     

case 10:

days += 30;

case 9:

days += 31;

case 8:

days += 31;

case 7:

days += 30;

case 6:

days += 31; 

case 5:

days += 30;       

case 4:

days += 31;

case 3:

days += 28;

case 2:

days += 31;

case 1:

days += day;               

}

if(days365){

System.out.println("你输入的已经超过了365天了");

}else{

System.out.println("第" + days + "天");

}

}

}

输出的结果如下:

month=12

day=31

第365天

参考资料:百度百科-Scanner

参考资料:百度百科-java

怎样利用java程序输出一个系统日期

//1.将字符串用split切割得到年月日组成的数组 String s2="2011-11-11 11:11:11";//2.Calendar对象的获得,abstract并且构造函数是protected//本地时区和本地的习惯,系统日期Calendar calendar=Calendar.getInstance(); //3.将Calendar转换成输入的日期用calendar对象的set(Calendar.对应常量(如YEAR等),输入的对应值)方法设值 //4.获得判断用的值//获得年份int year=calendar.get(Calendar.YEAR);//获得这个月最多的天数int maxDay=today.getActualMaximum(Calendar.DATE);//获得当前日期是一周中的第几天,注意这个数不代表星期几而是你电脑上日历的第几列int weekDay=calendar.get(Calendar.DAY_OF_WEEK);} 好了就这么多吧,有这些差不多了,还有什么继续问哈

Java编写程序,输入年份,输出本年度各月份日历

写了个简明的,

import java.util.Calendar;

import java.util.Scanner;

public class Test {

static public void main(String 参数[]){

Calendar c = Calendar.getInstance();

Scanner sc = new Scanner(System.in);

System.out.println("请输入年份:");

int year= sc.nextInt();

c.set(Calendar.YEAR, year);

c.set(Calendar.MONTH, Calendar.JANUARY);

c.set(Calendar.DAY_OF_MONTH, 1);

while(c.get(Calendar.YEAR)==year){

int wday=c.get(Calendar.DAY_OF_WEEK);

int mday=c.get(Calendar.DAY_OF_MONTH);

if(mday==1){

System.out.println("\n日\t一\t二\t三\t四\t五\t六\t第"+(c.get(Calendar.MONTH)+1)+"月");

System.out.println("---------------------------------------------------");

for(int i=0;iwday-1;i++) System.out.print(" \t");

}

System.out.print(mday+"\t");

if(wday==7) System.out.println();

c.add(Calendar.DAY_OF_YEAR, 1);

}

}

}

=======

请输入年份:

2012

日 一 二 三 四 五 六 第1月

---------------------------------------------------

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

日 一 二 三 四 五 六 第2月

---------------------------------------------------

1 2 3 4

5 6 7 8 9 10 11

12 13 14 15 16 17 18

19 20 21 22 23 24 25

26 27 28 29

日 一 二 三 四 五 六 第3月

---------------------------------------------------

1 2 3

4 5 6 7 8 9 10

11 12 13 14 15 16 17

18 19 20 21 22 23 24

25 26 27 28 29 30 31

日 一 二 三 四 五 六 第4月

---------------------------------------------------

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

日 一 二 三 四 五 六 第5月

---------------------------------------------------

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

日 一 二 三 四 五 六 第6月

---------------------------------------------------

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

日 一 二 三 四 五 六 第7月

---------------------------------------------------

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

日 一 二 三 四 五 六 第8月

---------------------------------------------------

1 2 3 4

5 6 7 8 9 10 11

12 13 14 15 16 17 18

19 20 21 22 23 24 25

26 27 28 29 30 31

日 一 二 三 四 五 六 第9月

---------------------------------------------------

1

2 3 4 5 6 7 8

9 10 11 12 13 14 15

16 17 18 19 20 21 22

23 24 25 26 27 28 29

30

日 一 二 三 四 五 六 第10月

---------------------------------------------------

1 2 3 4 5 6

7 8 9 10 11 12 13

14 15 16 17 18 19 20

21 22 23 24 25 26 27

28 29 30 31

日 一 二 三 四 五 六 第11月

---------------------------------------------------

1 2 3

4 5 6 7 8 9 10

11 12 13 14 15 16 17

18 19 20 21 22 23 24

25 26 27 28 29 30

日 一 二 三 四 五 六 第12月

---------------------------------------------------

1

2 3 4 5 6 7 8

9 10 11 12 13 14 15

16 17 18 19 20 21 22

23 24 25 26 27 28 29

30 31

输入年份月份,打印输出当月日历 用java

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class java {

public static void main(String[] args) throws IOException {

BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));

System.out.println("请输入年份:");

String s1=buf.readLine();

System.out.println("请输入月份: ");

String s2=buf.readLine();

int year=Integer.parseInt(s1);

int month=Integer.parseInt(s2);

switch(month)

{

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 13:

System.out.println("在"+year+"年的"+month+"月有:31天");

break;

case 4:

case 6:

case 9:

case 11:

System.out.println("在"+year+"年的"+month+"月有:30天");

break;

case 2:

if(year%4==0year%100!=0||year%400==0)

{

System.out.println("在"+year+"年的"+month+"月有:29天");

}

else

{

System.out.println("在"+year+"年的"+month+"月有:28天");

}

break;

}

}

}

java键盘输入年月,输出一整月的日期

public static void main(String[] args) throws Exception {

        Scanner scan = new Scanner(System.in);

        System.out.println("请输入年份(仅包含数字):");

        int year = scan.nextInt();

        System.out.println("请输入月份:");

        int month = scan.nextInt();

        Calendar   cal   =   Calendar.getInstance();   

          cal.set(Calendar.YEAR,year);   

          cal.set(Calendar.MONTH,month-1);

          int maxDate = cal.getActualMaximum(Calendar.DATE);//获取当月的总天数

          String str="";

          for(int i=1;imaxDate;i++){

              str=str+i+",";

          }

          str+=maxDate;

          System.out.println(year+"年"+month+"月的日期为:"+str);

    }

java输入年份输出日期的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java 输出日期、java输入年份输出日期的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-22,除非注明,否则均为首码项目网原创文章,转载请注明出处。