fengyuanlongyi 于 2015.06.23 22:37 提问

7个回答

devmiao      2015.06.23 23:12
`````` public int orderDate(Date date){
int dateSum = 0;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = format.format(date);
System.out.println(dateStr);
int year = Integer.valueOf(dateStr.substring(0,4));
int month = Integer.valueOf(dateStr.substring(5,7));
int day = Integer.valueOf(dateStr.substring(8,10));
for (int i = 1; i < month; i++){
switch(i){
case 1: case 3: case 5: case 7: case 8: case 10: case 12:dateSum += 31; break;
case 4: case 6: case 9: case 11:dateSum += 30; break;
case 2:
if(((year % 4 == 0) & (year % 100 != 0)) | (year % 400 == 0))
dateSum += 29;
else dateSum += 28;
}
}

return dateSum = dateSum + day;
}
``````
u012706811   2015.06.24 00:11
``````public class currentDay {

public static void main(String[] args) {
int year,month,day;
int temp = 0;
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
Scanner input = new Scanner(System.in);
System.out.print("请输入年月日");
year = input.nextInt();
month = input.nextInt();
day = input.nextInt();
for (int i = 0; i < month-1; i++) {
temp +=a[i];
}
if((year%400==0||(year%4==0 &&year%100!=0))&&month>2){
System.out.println("这一天是今年的第"+(temp+1+day)+"天");
}else{
System.out.println("这一天是今年的第"+(temp+day)+"天");
}

}

}
``````
u012706811 忘记加上输入限制条件了,这个可以自行加入
2 年多之前 回复
liboshi123   2015.06.24 08:45

tongyi55555   2015.06.24 09:20

Java 中的Calendar类是提供直接获取年份第几天的方法的。参考下面的代码：

``````/**
* 根据输入日期输出该日期所在年份的天数。
*
* @param dateStr
*            必须为2014-04-02格式
* @return 返回日期所在年份的第几天
*/
public static int printDayOfYear(String dateStr) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

Date startDate;
try {
startDate = sdf.parse(dateStr);
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(startDate);
int index = startCalendar.get(Calendar.DAY_OF_YEAR);
System.out.println("输入的日期" + dateStr + "是这一年中的第" + index + "天。");
return index;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
}
``````
dcxy0      2015.06.24 09:55

/**

• !!请使用jdk1.8
• *
• @author puruidong
• @version 2015-06-24
/
public class Main {
/
*

• 接受输入并输出该日期是当年的第多少天. / private static void showDate() { System.out.println("输入一个日期,格式[yyyy-MM-dd,例如:2000-08-08]:"); Scanner sc = new Scanner(System.in); String dates = sc.next(); try { LocalDate d = LocalDate.parse(dates, DateTimeFormatter.ISO_LOCAL_DATE); System.out.println("这是当年的第"+d.getDayOfYear()+"天."); }catch(Exception e){ System.err.println("*******************************不许乱输********************************"); //e.printStackTrace(); } }

public static void main(String[] args) {
showDate();
}
}

``````
``````
dcxy0      2015.06.24 09:56
`````` /**
*   !!请使用jdk1.8
*
*
* @author puruidong
* @version 2015-06-24
*/
public class Main {
/**
* 接受输入并输出该日期是当年的第多少天.
*/
private static void showDate() {
System.out.println("输入一个日期,格式[yyyy-MM-dd,例如:2000-08-08]:");
Scanner sc = new Scanner(System.in);
String dates = sc.next();
try {
LocalDate d = LocalDate.parse(dates, DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println("这是当年的第"+d.getDayOfYear()+"天.");
}catch(Exception e){
System.err.println("********************************不许乱输********************************");
//e.printStackTrace();
}
}

public static void main(String[] args) {

showDate();
}
}
``````
syjgreen07   2015.06.24 10:08