public static void main(String[] args) {
//SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
try {
Systemoutprintln("当前月共多少天 : " + DateUtilsgetDaysOfTheMonth(new Date()));
Systemoutprintln("当前月星期天天数:"+DateUtilsgetSundays(new Date()));
} catch (Exception e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
public static int getDaysOfTheMonth(Date date){//获取当月天数
Calendar rightNow = CalendargetInstance();
rightNowsetTime(date); // 要计算你想要的月份,改变这里即可
int days = rightNowgetActualMaximum(CalendarDAY_OF_MONTH);
return days;
}
// 返回当月星期天数
public static int getSundays(Date dat) {
int sundays = 0;
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
Calendar setDate = CalendargetInstance();
// 从第一天开始
int day;
for (day = 1; day <= getDaysOfTheMonth(dat); day++) {
setDateset(CalendarDATE, day);
String str = sdfformat(setDategetTime());
//Systemoutprintln("str:"+str);
if (strequals("星期日")) {
sundays++;
}
}
return sundays;
}
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;
}
}
我做过测试了完全正确分给我吧嘻嘻!
import javautilDate;
import javatextSimpleDateFormat;
public class daysofmonth
{
public daysofmonth(){}//构造方法
public static void calculate(int m,int y)//知月份年份求月份天数的方法caluculate
{
if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)
Systemoutprintln(y+"年"+m+"月:"+"31天");
else if(m!=2)
Systemoutprintln(y+"年"+m+"月:"+"30天");
else if((y%4==0&&y%100!=0)||(y%400==0))
Systemoutprintln(y+"年"+m+"月:"+"29天");
else
Systemoutprintln(y+"年"+m+"月:"+"28天");
}
public static void main(String args[])
{
Date nowTime=new Date();//获取当前时间
SimpleDateFormat yearPattern=new SimpleDateFormat("yyyy");//获取字符串型年份的对象
SimpleDateFormat monthPattern=new SimpleDateFormat("MM");//获取字符串型月份的对象
int inty=IntegerparseInt(yearPatternformat(nowTime));//将当前年份转化为整型
int intm=IntegerparseInt(monthPatternformat(nowTime));//将当前月份转化为整型
int a[]=new int[7];//定义一个整型数组
a[0]=intm;
Systemoutprintln("当月之后半年内每月的天数:");
for(int i=1;i<7;i++)
{
if(a[i-1]==12)
{
a[i]=1;
inty=inty+1;
calculate(a[i],inty);
}
else
{
a[i]=a[i-1]+1;
calculate(a[i],inty);
}
}
}
}
以上就是关于java 求出当月天数和有几个星期天全部的内容,包括:java 求出当月天数和有几个星期天、计算某年某月的天数 JAVA高手帮忙 救命啊、请问JAVA中如计算之后3个月及半年每个月的天数等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)