编写java计算输入年份输入月份得到这个月份的总共天数是是多少这是我编写的代码····

编写java计算输入年份输入月份得到这个月份的总共天数是是多少这是我编写的代码····,第1张

错误的不少呢,修改如下:

import javaxswingJOptionPane;

public class Year {

public static void main(String[] args) {

String yearString = JOptionPaneshowInputDialog(null, "请输入您要查找的年份");

String monthString = JOptionPaneshowInputDialog(null, "请输入您要查找的月份:");

int daysOfMonth = 0;

int year = IntegerparseInt(yearString);

int month = IntegerparseInt(monthString);

switch (month) {

case 1:

daysOfMonth = 31;

break;

case 2:

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

daysOfMonth = 29;

} else

daysOfMonth = 28;

break;

case 3:

daysOfMonth = 31;

break;

case 4:

daysOfMonth = 30;

break;

case 5:

daysOfMonth = 31;

break;

case 6:

daysOfMonth = 30;

break;

case 7:

daysOfMonth = 31;

break;

case 8:

daysOfMonth = 31;

break;

case 9:

daysOfMonth = 30;

break;

case 10:

daysOfMonth = 31;

break;

case 11:

daysOfMonth = 30;

break;

case 12:

daysOfMonth = 31;

break;

default:

break;

}

;

JOptionPaneshowMessageDialog(null, "这个月份总共的天数是" + daysOfMonth);

Systemoutprintln("您输入的年份是:" + yearString + "\n您输入的月份是:" + monthString + "\n这个月份总共的天数是:" + daysOfMonth);

}

}

import javautilDate;

import javautilCalendar;

import javatextSimpleDateFormat;class Demo{

//返回当前年份

int getYear(){

Date date=new Date();

String year=new SimpleDateFormat("yyyy")format(date);

return IntegerparseInt(year);

}

//返回当前月份

int getMonth(){

Date date=new Date();

String month=new SimpleDateFormat("MM")format(date);

return IntegerparseInt(month);

}

//判断闰年

boolean isLeap(int year){

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

return true;

else

return false;

}

//返回当月天数

int getDays(int year,int month){

int days;

int FebDay=28;

if(isLeap(year))

FebDay=29;

switch(month){

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

days=31;break;

case 4:

case 6:

case 9:

case 11:

days=30;break;

case 2:

days=FebDay;break;

default:

days=0;break;

}

return days;

}

//返回当月星期天数

int getSundays(int year,int month){

int sundays=0;

SimpleDateFormat sdf=new SimpleDateFormat("EEEE");

Calendar setDate= CalendargetInstance();

//从第一天开始

int day;

for(day=1;day<=getDays(year,month);day++){

setDateset(CalendarDATE,day);

String str=sdfformat(setDategetTime());

if(strequals("星期日")){

sundays++;

}

}

return sundays;

}

public static void main(String[] args){

Demo demo=new Demo();

Systemoutprintln ("本月有"+demogetDays(demogetYear(),demogetMonth())+"天");

Systemoutprintln ("本月有"+demogetSundays(demogetYear(),demogetMonth())+"个星期天");

}

}次程序已经验证正确如果看程序的话我自认为我的代码比楼上的好,而且楼桑有用到java API已经过时的方法但是当写玩代码后去看了电脑下的时间,发现我好SB,一个月不就四个星期天或者五个星期谈吗?害我费了这么多时间,哎~~

import javaio;

import javautilScanner;

class DayTest

{

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

{

int year = -1;

int month = -1;

Scanner in = new Scanner(Systemin);

Systemoutprintln("请输入年");

year = innextInt();

Systemoutprintln("请输入月");

month = innextInt();

Systemoutprintln(year+"年"+month+"月有"+days(year,month)+"天");

}

public static int days(int year,int month)

{

int days = 0;

if(month!=2)

{

switch(month)

{

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:days = 31 ;break;

case 4:

case 6:

case 9:

case 11:days = 30;

}

}

else

{

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

days = 29;

else days = 28;

}

return days;

}

}

我做过测试了完全正确分给我吧嘻嘻!

首先把获取的字符串日期转换成Date类型(从前台页面获取的是字符串类型的日期 a,b):

Date a1 = new SimpleDateFormat("yyyy-MM-dd")parse(a);

Date b1 = new SimpleDateFormat("yyyy-MM-dd")parse(b);

//获取相减后天数

long day = (a1getTime()-b1getTime())/(2460601000);

希望可以帮到你。

import javautil;

import javatext;

public class CalendarDemo

{

public static void main(String[] args)

{

Calendar c1 = CalendargetInstance();

DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String time1 = "2011-03-11 11:39:04";

String time2 = "2022-02-03 22:33:44";

try

{

c1setTime(formatparse(time1));

}

catch (Exception e)

{

Systemoutprintln(e);

}

Systemoutprintln("nian" + c1get(c1YEAR) + "yue" + (c1get(c1MONTH) + 1) + "ri" + c1get(c1DAY_OF_MONTH)); //获取年月日

c1add(c1DAY_OF_MONTH,5); //增加5天

c1add(c1HOUR_OF_DAY,-4); //减少4个小时

c1roll(c1YEAR,2); //增加2年

c1roll(c1MONTH,-2);//减少2个月

Systemoutprintln("time1 = " + format2format(c1getTime()));

c1set(c1MONTH,6);//增加7天

Systemoutprintln("time1 = " + format2format(c1getTime()));

try

{

c1setTime(formatparse(time2));//将日历时间设为指定时间

Systemoutprintln("time1 = " + format2format(c1getTime()));

}

catch (Exception e)

{

Systemerrprintln(e);

}

}

}

多看看文档就好

以上就是关于编写java计算输入年份输入月份得到这个月份的总共天数是是多少这是我编写的代码····全部的内容,包括:编写java计算输入年份输入月份得到这个月份的总共天数是是多少这是我编写的代码····、java 求出当月天数和有几个星期天、计算某年某月的天数 JAVA高手帮忙 救命啊等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9764505.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-01
下一篇 2023-05-01

发表评论

登录后才能评论

评论列表(0条)

保存