「java计算月份程序」java计算每个月的天数

博主:adminadmin 2022-12-18 16:00:09 53

今天给各位分享java计算月份程序的知识,其中也会对java计算每个月的天数进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

编写程序,提示用户输入月份和年份,然后显示这个月份的天数.java语言编写

import java.util.Scanner;

public class $ {

    private static int[] DAYS = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

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

        int year = in.nextInt();

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

        int month = in.nextInt();

        String str = year + "-" + month + ":";

        // 闰年二月份

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

            str += 29;

        } else {

            str += DAYS[month - 1];

        }

        System.out.println(str);

    }

}

用java程序写出 接收一个年份和一个月份,判断得出该月的总天数。(用if语句完成)

import java.util.*;

public class Test {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

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

int year = input.nextInt();

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

int month = input.nextInt();

if(month==2){

if (((year % 100 == 0) (year % 400 == 0))

|| ((year % 100 != 0) (year % 4 == 0))) {

System.out.println(month+"月份天数是29天");

return;

}else {

System.out.println(month+"月份天数是28天");

return;

}

}else if(month%2==0 ) {

System.out.println(month+"月份是30天");

}else if(month%2!=0) {

System.out.println(month+"月份是31天");

}

}

}

Java编写程序完成输人年份和月份,计算指定年份中的天数并含代码注解

/**

 * 在一个时间段计算出多少天

 * @param beginDate 开始时间

 * @param endDate 结束时间

 * @param format 格式 如果  yyyy-MM-dd;yyyy

 * @return 天数

 */

public static int getDay(String beginDate, String endDate, String format) {

try {

// 日期操作类,设置制定的格式

SimpleDateFormat sim = new SimpleDateFormat(format);

Date d1 = sim.parse(beginDate); // 传入开始时间

Date d2 = sim.parse(endDate);   // 传入结束时间

//计算开始时间和结束时间的时间差

int num = (int) ((d2.getTime() - d1.getTime()) / (3600L * 1000 * 24));

if(!format.equals("yyyy")) {

num = num + 1;

}

return num;

} catch (Exception e) {

e.printStackTrace();

return 0;

}

}

public static void main(String[] args) {

System.out.println(getDay("2018", "2020", "yyyy"));

System.out.println(getDay("2018-01-01", "2018-12-31", "yyyy-MM-dd"));

System.out.println(getDay("2019-02-07", "2019-02-11", "yyyy-MM-dd"));

}

年的月的都有,有什么疑问可以问我。

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计算一个月有多少秒

有2592000(秒)。

jαVα是一个应用程序,每个月都不一样,但可以选定一个月为30天,就是30(天)*24(小时)*3600(秒)=2592000(秒)。

java计算月份程序的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java计算每个月的天数、java计算月份程序的信息别忘了在本站进行查找喔。

The End

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