1、输入两个日期,请问相差多少天?
import java.util.Date;
public class Test01 {
public static void main(String[] args) {
Date date=new Date();//获取当前时间
Date date1=new Date();
date.setYear(100);//设置时间,相当于基准时间1900加100,年份为2000年
date.setMonth(6);
date.setDate(8);
date1.setYear(100);
date1.setMonth(7);
date1.setDate(8);
//发布获取对象距离基准时间1970年1月1日00:00:00的毫秒数,然后让两个相减得到相差数
double a=(double)date1.getTime()-date.getTime();
//毫秒数转为天数
System.out.println(a/(1000*60*60*24));
}
}
2、已知你的生日,请问你多大了?
import java.time.LocalDate;
public class Test02 {
public static void main(String[] args) {
LocalDate localDate1=LocalDate.now();//获取本地当前时间
LocalDate localDate=LocalDate.of(1999,8,6);//设置时间
//判断相差年份
int age=localDate1.getYear() - localDate.getYear();
//确定是否过完生日
if (localDate.getMonthValue()>localDate1.getMonthValue()){
System.out.println(age-1);
}else if(localDate.getMonthValue()==localDate1.getMonthValue()){
if(localDate.getDayOfMonth()>localDate1.getDayOfMonth()){
System.out.println(age-1);
}else {
System.out.println(age);
}
}else {
System.out.println(age);
}
}
}
3、任意输入两个日期,比较大小?
import java.time.LocalDate;
public class Test {
public static void main(String[] args) {
LocalDate localDate=LocalDate.of(2018,9,5);
LocalDate localDate1=LocalDate.now();
int a=localDate.compareTo(localDate1);
if (a==0){
System.out.println("这两个日期相等");
}else if(a>0){
System.out.println("第一个日期大于第二个日期");
}else {
System.out.println("第二个日期大于第一个日期");
}
}
}
4、新闻类:标题,内容,时间。
有10条新闻,按照时间的降序排列。
新闻类:
import java.time.LocalDate;
public class NewsClass {
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 NewsClass() {
}
public NewsClass(String title, String content, LocalDate time) {
this.title = title;
this.content = content;
this.time = time;
}
@Override
public String toString() {
return "NewsClass{" +
"title='" + title + '\'' +
", content='" + content + '\'' +
", time=" + time +
'}';
}
}
测试:
import java.time.LocalDate;
public class Test04 {
//按照时间排序
public static void sort(NewsClass[] nsc){
for (int i = 0; i <nsc.length-1 ; i++) {
for (int j = 0; j <nsc.length-i-1 ; j++) {
if(nsc[j].getTime().compareTo(nsc[j+1].getTime())<0){
NewsClass ns;
ns=nsc[j];
nsc[j]=nsc[j+1];
nsc[j+1]=ns;
}
}
}
}
public static void main(String[] args) {
NewsClass ns1=new NewsClass("标题1","内容1", LocalDate.of(2022,8,6));
NewsClass ns2=new NewsClass("标题2","内容2", LocalDate.of(2021,8,6));
NewsClass ns3=new NewsClass("标题3","内容3", LocalDate.of(2023,8,6));
NewsClass ns4= new NewsClass("标题4","内容4", LocalDate.of(2022,9,5));
NewsClass ns5=new NewsClass("标题5","内容5", LocalDate.of(2021,5,16));
NewsClass[] newsClass={ns1,ns2,ns3,ns4,ns5};
sort(newsClass);
for (int i = 0; i <newsClass.length ; i++) {
//输出对象名就是输出toString,隐藏了
System.out.println(newsClass[i]);
}
}
}
5、学生类:姓名,学号,出生日期。
找出比“张三”大的所有同学信息。
学生类:
import java.time.LocalDate;
public class Student {
private String name;
private String id;
private LocalDate birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public LocalDate getBirthday() {
return birthday;
}
public void setBirthday(LocalDate birthday) {
this.birthday = birthday;
}
public Student() {
}
public Student(String name, String id, LocalDate birthday) {
this.name = name;
this.id = id;
this.birthday = birthday;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", id='" + id + '\'' +
", birthday=" + birthday +
'}';
}
}
测试
public class Test05 {
public static void main(String[] args) {
Student st1 = new Student("张三", "1001", LocalDate.of(2020, 1, 5));
Student st2 = new Student("李四", "1002", LocalDate.of(2018, 1, 25));
Student st3 = new Student("王五", "1003", LocalDate.of(2020, 12, 15));
Student st4 = new Student("马六", "1004", LocalDate.of(2021, 3, 3));
Student st5 = new Student("麻子", "1005", LocalDate.of(2020, 10, 8));
Student[] s={st1,st2,st3,st4,st5};
for (int i = 0; i <s.length ; i++) {
if(s[i].getBirthday().compareTo(st1.getBirthday())>=0){
System.out.println(s[i]);
}
}
}
}
6、请问今天是今年的第多少天?
public class Test06 {
public static void main(String[] args) {
LocalDate localDate=LocalDate.now();
//类中含有方法获取对象这一天在一年中的天数,直接调用
System.out.println(localDate.getDayOfYear());
}
}
7、输入任意一个年份,得到这年的2月份有几天?
public class Test07 {
public static void main(String[] args) {
int year=2021;
Calendar instance = Calendar.getInstance();
//设置完时间的其中一个属性后,其他日期属性也会相应的做出改变
instance.set(year,2,1);
//
instance.add(Calendar.DATE ,-1);
System.out.println("该年二月份的天数为"+instance.get(Calendar.DATE));
}
}
8、求上个月的第三天是星期几?
public class Test08 {
public static void main(String[] args) {
int month=1;
Calendar instance = Calendar.getInstance();
//设置上个月
instance.set(2021,month-1,3);
System.out.println(instance.get(Calendar.DAY_OF_WEEK));
}
}
9、求下个月的倒数第三天是星期几?
public class Test09 {
public static void main(String[] args) {
//设置月份
int month=7;
Calendar instance = Calendar.getInstance();
//得到下一个月的天数
instance.set(2021,month+1,1);
instance.add(Calendar.DATE,-1);
//设置下一月的倒数第三天,因为不确定下个月的天数,倒数第三天的日期也可能不相同
instance.set(2021,month+1,instance.get(Calendar.DATE)-3);
System.out.println(instance.get(Calendar.DAY_OF_WEEK));
}
}