Java:給定一個日期,輸出這個日期是該年的第幾天。
阿新 • • 發佈:2019-01-09
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
String dateStr=sc.nextLine();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
java.util.Date startDate = null;
try {
startDate = sdf.parse(dateStr);
} catch (Exception e) {
e.printStackTrace();
}
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(startDate);
int index = startCalendar.get(Calendar.DAY_OF_YEAR);
System.out.println(index);
}
}
import java.text.SimpleDateFormat;
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
String dateStr=sc.nextLine();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
java.util.Date startDate = null;
try {
startDate = sdf.parse(dateStr);
} catch (Exception e) {
e.printStackTrace();
}
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(startDate);
int index = startCalendar.get(Calendar.DAY_OF_YEAR);
System.out.println(index);
}
}
}
最近找到的學習Java的網站,專做Java(還包含前端的基礎知識),個人感覺挺好,主要是有實踐專案供大家做。
這是網站,給大家分享一下:http://how2j.cn?p=23817