java一年有多少天
① java中如何计算一年总共有多少周
用365
天历闷谨除罩族7得到周数
模7得到剩余天肢基数~代码如下
int
a=0,b=0;a=365/7;b=365%7;sys
tem.out.print(”一年有”+a+”周,余天数”+b);
② java中怎样得到某年有多少天
年闰年和非闰年,闰年纯余闭366天,非闰年365天。所以判断某年多少天也就是判断是否是闰年。
闰年的判断依据
①、普通年能被4整除且不能被100整除的为闰年。(如2004年就是闰年,1900年不是闰年)
②、世纪年能被400整除的是闰年。(如2000年是闰做裂年,1900年不是闰年)
代码:
intyear;//要判断的年份,比如2008
intdays;//某年(year)的天数
if(year%4==0&&year%100!=0||year%400==毁信0){//闰年的判断规则
days=366;
}else{
days=365;
}
③ Java语言,已知一个年月日的日期,获取该日期是这一年的多少天...........
这种不用这么烦的,你已逗猜灶知年月日,直接用年月日构造一个Date或者Calender对象,然后有现成的方法获得当前日期是所属年份的第几天,是当前周的第几天,当前月的第几天
比如Calender对象属性
DAY_OF_YEAR
public static final int DAY_OF_YEAR
get 和 set 的字段数字,指山扮示当前年中的天数。一年中第一天的值为 1。
直接就能拿到你要的结果了
你根本不用去兆空考虑闰年这些情况,Calender类早就帮你处理好了
④ java calendar 一年有几天
Calendar镇做陵d=Calendar.getInstance();
System.out.println(d.getActualMaximum(Calendar.DAY_OF_YEAR));
胡敏d.set(Calendar.YEAR,2000);
System.out.println(d.getActualMaximum(Calendar.DAY_OF_YEAR));
使用御戚Calendar.getActualMaximum(Calendar.DAY_OF_YEAR) 。
⑤ 用java实现年龄计算器,就是在界面上输入一个人出生年月日(yyyy-mm-dd),显示现在多少岁,假设一年有365天
这是一段javascript写的,无论是java或javascript原理都一样,进攻参考
<!-- Begin
function run() {
with (document.agecalc) {
dd = parseInt(day.selectedIndex) + 1;
mm = parseInt(month.selectedIndex) + 1;
yy = year.value;
if (yy.length != 4 || isNaN(yy)) {
document.agecalc.timealive.value = "请输举孝入4位数的余山年份竖答中.";
document.agecalc.year.select();
document.agecalc.year.focus();
return;
}
}
days = new Date();
gdate = days.getDate();
gmonth = days.getMonth();
gyear = days.getYear();
if (gyear < 2000) gyear += 1900;
age = gyear - yy;
if ((mm == (gmonth + 1)) && (dd <= parseInt(gdate))) {
age = age;
} else {
if (mm <= (gmonth)) {
age = age;
} else {
age = age - 1;
}
}
if (age == 0)
age = age;
document.agecalc.timealive.value = "你已经满了 " + age+ " 周岁 . . .\n\n";
if (mm <= (gmonth + 1))
age = age - 1;
if ((mm == (gmonth + 1)) && (dd > parseInt(gdate)))
age = age + 1;
var m;
var n;
if (mm == 12) n = 31 - dd;
if (mm == 11) n = 61 - dd;
if (mm == 10) n = 92 - dd;
if (mm == 9) n = 122 - dd;
if (mm == 8) n = 153 - dd;
if (mm == 7) n = 184 - dd;
if (mm == 6) n = 214 - dd;
if (mm == 5) n = 245 - dd;
if (mm == 4) n = 275 - dd;
if (mm == 3) n = 306 - dd;
if (mm == 2) { n = 334 - dd; if (leapyear(yy)) n++; }
if (mm == 1) { n = 365 - dd; if (leapyear(yy)) n++; }
if (gmonth == 1) m = 31;
if (gmonth == 2) {
m = 59;
if (leapyear(gyear)) m++;
}
if (gmonth == 3) { m = 90; if (leapyear(gyear)) m++; }
if (gmonth == 4) { m = 120; if (leapyear(gyear)) m++; }
if (gmonth == 5) { m = 151; if (leapyear(gyear)) m++; }
if (gmonth == 6) { m = 181; if (leapyear(gyear)) m++; }
if (gmonth == 7) { m = 212; if (leapyear(gyear)) m++; }
if (gmonth == 8) { m = 243; if (leapyear(gyear)) m++; }
if (gmonth == 9) { m = 273; if (leapyear(gyear)) m++; }
if (gmonth == 10) { m = 304; if (leapyear(gyear)) m++; }
if (gmonth == 11) { m = 334; if (leapyear(gyear)) m++; }
if (gmonth == 12) { m = 365; if (leapyear(gyear)) m++; }
months = age * 12;
months += 12 - parseInt(mm);
months += gmonth;
totdays = (parseInt(age) * 365);
totdays += age / 4;
totdays = parseInt(totdays) + gdate + m + n;
if (gmonth == 1) p = 31 + gdate;
if (gmonth == 2) {
p = 59 + gdate;
if (leapyear(gyear)) m = m+1;
}
if (gmonth == 3) { p = 90 + gdate; if (leapyear(gyear)) p++; }
if (gmonth == 4) { p = 120 + gdate; if (leapyear(gyear)) p++; }
if (gmonth == 5) { p = 151 + gdate; if (leapyear(gyear)) p++; }
if (gmonth == 6) { p = 181 + gdate; if (leapyear(gyear)) p++; }
if (gmonth == 7) { p = 212 + gdate; if (leapyear(gyear)) p++; }
if (gmonth == 8) { p = 243 + gdate; if (leapyear(gyear)) p++; }
if (gmonth == 9) { p = 273 + gdate; if (leapyear(gyear)) p++; }
if (gmonth == 10) { p = 304 + gdate; if (leapyear(gyear)) p++; }
if (gmonth == 11) { p = 334 + gdate; if (leapyear(gyear)) p++; }
if (gmonth == 12) { p = 365 + gdate; if (leapyear(gyear)) p++; }
weeks = (age * 365) + n + p;
weeks = weeks / 7;
etcdays = parseFloat(weeks) - parseInt(weeks);
etcdays = Math.round(etcdays * 7);
weeks = parseInt(weeks);
etcdays += parseInt(age / 4);
if (etcdays > 7)
weeks += parseInt(etcdays / 7);
document.agecalc.timealive.value += " 或 " + months + " 月龄\n";
document.agecalc.timealive.value += " 或 " + weeks + " 周龄\n";
document.agecalc.timealive.value += " 或 " + totdays + " 日龄\n";
var time = new Date();
ghour = time.getHours();
gmin = time.getMinutes();
gsec = time.getSeconds();
hour = ((age * 365) + n + p) * 24;
hour += (parseInt(age / 4) * 24);
document.agecalc.timealive.value += " 或 " + hour + " 小时\n";
var min = (hour * 60) + gmin;
document.agecalc.timealive.value += " 或 " + min + " 分钟\n";
sec = (min * 60) + gsec;
document.agecalc.timealive.value += " 或 " + sec + " 秒钟";
mm = mm - 1;
var r;
if (mm == 0) r = 0;
if (mm == 1) r = 31;
if (mm == 2) { r = 59; if (leapyear(gyear)) m++; }
if (mm == 3) { r = 90; if (leapyear(gyear)) r++; }
if (mm == 4) { r = 120; if (leapyear(gyear)) r++; }
if (mm == 5) { r = 151; if (leapyear(gyear)) r++; }
if (mm == 6) { r = 181; if (leapyear(gyear)) r++; }
if (mm == 7) { r = 212; if (leapyear(gyear)) r++; }
if (mm == 8) { r = 243; if (leapyear(gyear)) r++; }
if (mm == 9) { r = 273; if (leapyear(gyear)) r++; }
if (mm == 10) { r = 304; if (leapyear(gyear)) r++; }
if (mm == 11) { r = 334; if (leapyear(gyear)) r++; }
mm = mm + 1;
r = parseInt(r) + parseInt(dd);
if ((mm >= (gmonth + 1)) && (dd > gdate)) {
bday = r - m - gdate;
}
else {
if ((leapyear(gyear)) && ((mm > 2) && (dd < 29))) {
a = 366;
} else {
a = 365;
}
bday = a + (r - m - gdate);
}
nhour = 24 - parseInt(ghour);
nmin = 60 - parseInt(gmin);
nsec = 60 - parseInt(gsec);
while (bday > 366) bday -= 365;
if (((bday == 366) && (leapyear(gyear)) || ((bday == 365) && (!leapyear(gyear))))) {
document.agecalc.timealive.value += "\n\nSenlon祝贺您!今天是你的生日!祝你生日快乐!";
} else {
document.agecalc.timealive.value += "\n\n另外,你下一个生日距今还有:\n"
+ bday + " 天 " + nhour + " 小时 " + nmin + " 分 " + nsec + " 秒";
setTimeout("run()", 1000);
}
}
function leapyear(a) {
if (((a%4 == 0) && (a%100 != 0)) || (a%400 == 0))
return true;
else
return false;
}
// End -->
来自 http://www.dragon-guide.net/hangye/birthday.htm
⑥ java编写某年某月有多少天
import java.util.Scanner;
public class DayOfMonth {
public static void main(String args[]){
Scanner s=new Scanner(System.in);
System.out.println("请输入年昌纤");
int year=s.nextInt();
System.out.println("渣迅败请输入月"如颤);
int month=s.nextInt();
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
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 == 0&&year%100 != 0)||year%400 == 0)
System.out.println(year+"年2月有29天");
else
System.out.println(year+"年2月有28天");
break;
}
}
}
这样可以了么?
⑦ java如何计算某年某日在一年中是第几天
分类: 电脑燃消斗/网络桥粗 >> 程序设计 >> 其他编程语言
解析:
class CalTianShu{
public static int cal(int year,int month,int day){
int sum=0;
for(int i=1;i<month;i++){
switch(i){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:sum+=31; break;
case 4:
case 6:
case 9:
case 11:sum+=30; break;
case 2: if(((year%4==0) & (year%100!=0)) | (year%
400==0))sum+=29; else sum+=28;
}
}
return sum=sum+day;
}
public static void main(String[] args){
int val;
val=cal(2006,2,31);
System.out.println("该天是当年皮磨中的第"+val+"天");
}
}
⑧ 我是自学Java的,刚刚写了一个代码,输出那一年的哪一月,有多少天!各位帮我看一次,哪里有错
//你目的就随机一个年份,和随机一个月份,然后看有多少天,整理了一下你的亏羡代码;
publicclassTest{
publicstaticvoidmain(String[]args){
inta=(int)(3450*Math.random()+1),day=31;;//随机年份!
intb=老激(int)(12*Math.random()+1);//随机月份
booleanflag=jud(a);//判断是否闰年;
Stringstr=flag?"是闰年":"是平年";
switch(b){//判断月份有几天
case4:
case6:
case9:
case11:
day=30;
break;
case2:
day=flag销含拍?29:28;
break;
}
System.out.println(a+""+str+"..."+b+"月有"+day+"天;");
}
privatestaticbooleanjud(inta){//闰年判断:条件
returna%400==0||(a%4==0&&a%100!=0);
}
}
⑨ java程序一年里一个月有多少天
package Day02;
import java.util.Scanner;
public class YearandMonth {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入年份>");
int year = scan.nextInt();
System.out.println("请输入月份");
int month = scan.nextInt();
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
System.out.println("该月份有31天");
}else if (month == 2){
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
System.out.println("该月份有29天");
}else{
System.out.println("该月份有28天");
}
}else if (month == 4 || month == 6 || month == 9 || month == 11){
System.out.println("该月份有30天");
}else{
System.out.println("没有该月份");
}
}
}
⑩ 编写函数计算某一年第一天有多少天java
反复看了您的问题,我想你想表达的是想计算某一年的某一月有多少天吧?
好了,下面看代码缓孙:
public class Test2 {
static int year = 0;
static int month = 0;
int day = 0;
/**
* 这里分为闰年和平年。差异在源哪早于多一天和少一天。
*/
public int CountDay(int yearInput, int monthDay) {
if (yearInput != 0 && monthDay != 0) {
year = yearInput;
month = monthDay;
// 是闰年 能被4 和100 整除
if (yearInput % 4 == 0 && yearInput % 100 == 0) {
switch (monthDay) {
case 1:
day = 31;
break;
case 2:
day = 29;
break;
case 3:
day = 31;
break;
case 4:
day = 30;
break;
case 5:
day = 31;
break;
case 6:
day = 30;
break;
case 7:
day = 31;
break;
case 8:
day = 31;
break;
case 9:
day = 30;
break;
case 10:
day = 31;
break;
case 11:
day = 30;
break;
case 12:
day = 31;
break;
default:
break;
}
} else {
switch (monthDay) {
case 1:
day = 31;
break;
case 2:
day = 28;
break;
case 3:
day = 31;
break;
case 4:
day = 30;
break;
case 5:
day = 31;
break;
case 6:
day = 30;
break;
case 7:
day = 31;
break;
case 8:
day = 31;
break;
case 9:
day = 30;
break;
case 10:
day = 31;
break;
case 11:
day = 30;
break;
case 12:
day = 31;
break;
default:
break;
}
}
}
return day;
}
public static void main(String[] args) {
Test2 t = new Test2();
int result = t.CountDay(2016, 8);
System.out.println(year + "年" + month + "月一共:" + result + "天雹雀");
}
}