java :输入某年某月某日,判断这一天是这一年的第几天?(考虑闰年的情况)

用java编写啊

import java.lang.*;
import javax.swing.JOptionPane;
public class Days{
public static void main(String args[]){
String str1,str2,str3;
double year,month,day,sum=0;
int i;
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
str1=JOptionPane.showInputDialog(null,"请输入年","suhao",JOptionPane.QUESTION_MESSAGE);
year=Double.parseDouble(str1);
str2=JOptionPane.showInputDialog(null,"请输入月","suhao",JOptionPane.QUESTION_MESSAGE);
month=Double.parseDouble(str2);
str3=JOptionPane.showInputDialog(null,"请输入日","suhao",JOptionPane.QUESTION_MESSAGE);
day=Double.parseDouble(str3);
if(year%4==0&&year%100!=0||year%400==0)
a[1]=29;
if(month==0)
sum=day;
else
{
for(i=0;i<month-1;i++)
sum+=a[i];
sum+=day;
}
System.out.println("该天是该年的第"+sum+"天");
}
}
拿到机器上编译看看!!应该能行!!好好学习,天天向上!!共勉!
温馨提示:答案为网友推荐,仅供参考
第1个回答  2007-12-11
你要考虑的情况,已经有API直接可以使用。

比如说如的日期为:String time="2007-12-1"

import java.util.*;

DateFormat f = SimpleDateFormat("yyyy-MM-dd");
Date d = f.parse(time);

Calendar c = Calendar.getInstance();
c.setTime(d);

System.out.println(c.get(Calendar.DAY_OF_YEAR));
第2个回答  2007-12-11
大概的思想是这样的。
1.先计算日期 int d。
2.判断是否为闰年。
3.计算月份 int m。
4.月份加日期(m+d)

大功告成!!!
第3个回答  2007-12-11
Calendar rightNow=new GregorianCalendar();
rightNow.set(2007, 3, 1);
int date=rightNow.get(Calendar.DAY_OF_YEAR);
date就是一年中的第多少天(2007年4月1日)
第4个回答  2007-12-11
一个 switch case 不就解决了?
=================上午比较闲==================

public static void main (String arge[]){
int year = 2008;
int month = 12;
int day = 11;
int count=0;
switch(month)
{
case 12:
count += 30;
case 11:
count += 31;
case 10:
count += 30;
case 9:
count += 31;
case 8:
count += 30;
case 7:
count += 31;
case 6:
count += 31;
case 5:
count += 30;
case 4:
count += 31;
case 3:
count += 28;
case 2:
count += 31;
case 1:
count += 0;
}
count += day;
if (year%4==0&&year%100!=0&&month>=3)
{count += 1;}

System.out.print(count);
}

看得懂哦 不写解释了;

下次分高一点,不然都没人看的。本回答被提问者采纳
相似回答