1.输入两个日期,请问相差多少天
public class zy1 {
public static void main(String[] args) {
// Date d1=new Date(3213123);
// Date d2=new Date(1231223);
// long day=(d1.getTime()-d2.getTime())/1000/1000/24;
// System.out.println(day);
LocalDate ld1=LocalDate.of(2020,10,10);
LocalDate ld2=LocalDate.of(2021,10,10);
long between = ChronoUnit.DAYS.between(ld1, ld2);
System.out.println(between);
}
}
2.已知你的生日,请问你多大了?
public static void main(String[] args) {
LocalDate ld1=LocalDate.of(1999,2,26);
LocalDate ld2=LocalDate.now();
long between = ChronoUnit.DAYS.between(ld1, ld2);
long year=(between/365);
System.out.println(year);
}
3、新闻类:标题,内容,时间。有10条新闻,按照时间的降序排列。
package week2.day3;
import java.time.LocalDate;
public class News {
private String title;
private String content;
private LocalDate time;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public LocalDate getTime() {
return time;
}
public void setTime(LocalDate time) {
this.time = time;
}
public News() {
}
public News(String title, String content, LocalDate time) {
this.title = title;
this.content = content;
this.time = time;
}
@Override
public String toString() {
return "News{" +
"title='" + title + '\'' +
", content='" + content + '\'' +
", time=" + time +
'}';
}
}
public class Date03 {
public static void main(String[] args) {
News news1=new News("河大封校1","河大发现了一名密接者", LocalDate.of(2022,2,28));
News news2=new News("河大封校2","河大发现了一名密接者", LocalDate.of(2022,2,25));
News news3=new News("河大封校3","河大发现了一名密接者", LocalDate.of(2022,3,3));
News news4=new News("河大封校4","河大发现了一名密接者", LocalDate.of(2022,1,1));
News news5=new News("河大封校5","河大发现了一名密接者", LocalDate.of(2022,2,28));
News[] newsss={news1,news2,news3,news4,news5};
for (int i = 0; i < newsss.length-1; i++) {
for (int j = 0; j < newsss.length-1-i; j++) {
News n11=newsss[j];
News n22=newsss[j+1];
if(n11.getTime().compareTo(n22.getTime()) < 0 ){
newsss[j]=n22;
newsss[j+1]=n11;
}
}
}
for (int i = 0; i < newsss.length; i++) {
System.out.println(newsss[i]);
}
}
4、学生类:姓名,学号,出生日期。
找出比“张三”大的所有同学信息。
package week2.day3;
import java.util.Calendar;
public class Student {
private int num;
private String name;
private Calendar brith;
@Override
public String toString() {
return "Student{" +
"num=" + num +
", name='" + name + '\'' +
", brith=" + brith +
'}';
}
public Student() {
}
public Student(int num, String name, Calendar brith) {
this.num = num;
this.name = name;
this.brith = brith;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Calendar getBrith() {
return brith;
}
public void setBrith(Calendar brith) {
this.brith = brith;
}
}
public static void main(String[] args) {
Student s1=new Student(21312,"张三",Calendar.getInstance());
s1.getBrith().set(Calendar.MONTH,2);
Student s2=new Student(2131,"张三1",Calendar.getInstance());
s2.getBrith().set(Calendar.MONTH,3);
Student s3=new Student(2132,"张三2",Calendar.getInstance());
s3.getBrith().set(Calendar.MONTH,1);
Student s4=new Student(1312,"张三3",Calendar.getInstance());
s4.getBrith().set(Calendar.MONTH,6);
Student[] students={s1,s2,s3,s4};
Calendar zhangsan=null;
for (int i = 0; i < students.length; i++) {
if (students[i].getName().equals("张三")){
zhangsan=students[i].getBrith();
break;
}
}
for (int i = 0; i < students.length; i++) {
if(students[i].getBrith().compareTo(zhangsan)<0){
System.out.println(students[i]);
}
}
}
5、请问今天是今年的第多少天?
public class Date05 {
public static void main(String[] args) {
LocalDate d1=LocalDate.now();
int dayOfYear = d1.getDayOfYear();
System.out.println(dayOfYear);
Calendar c=Calendar.getInstance();
c.get(Calendar.DAY_OF_YEAR);
}
}