當前位置:首頁 » 工資水平 » java一年有多少天

java一年有多少天

發布時間: 2023-04-15 09:33:55

① 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 + "天雹雀");
}
}

熱點內容
馬路上汽車的噪音在多少分貝 發布:2023-08-31 22:08:23 瀏覽:1809
應孕棒多少錢一盒 發布:2023-08-31 22:08:21 瀏覽:1290
標准養老金一年能領多少錢 發布:2023-08-31 22:05:05 瀏覽:1574
湖北通城接網線多少錢一個月 發布:2023-08-31 21:59:51 瀏覽:1658
開隨車吊車多少錢一個月 發布:2023-08-31 21:55:06 瀏覽:1417
京東付尾款怎麼知道前多少名 發布:2023-08-31 21:52:58 瀏覽:1737
在學校租鋪面一個月要多少錢 發布:2023-08-31 21:52:09 瀏覽:1871
2寸有多少厘米 發布:2023-08-31 21:50:34 瀏覽:1518
知道電壓如何算一小時多少電 發布:2023-08-31 21:46:20 瀏覽:1497
金手鐲54號圈周長是多少厘米 發布:2023-08-31 21:44:28 瀏覽:1677