1、输入两个日期,请问相差多少天?
package com.timedate.test;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Daygap {
public static void main(String[] args) throws Exception {
String Atime = "2021-07-11";
String Btime = "2025-11-29";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date dateA = format.parse(Atime);
Date dateB = format.parse(Btime);
long l = Math.abs(dateA.getTime() - dateB.getTime()) / (1000 * 3600 * 24);
System.out.println(l);
}
}
2、已知你的生日,请问你多大了?
package com.timedate.test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class birthDay {
public static void main(String[] args) throws ParseException {
Scanner scanner = new Scanner(System.in);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("请输入你的生日:格式为yyyy-MM-dd");
String birthDay =scanner.nextLine();
Date birthday = sdf.parse(birthDay);
double day = new Date().getTime()-birthday.getTime();
int year = (int) (day/1000/60/60/24/365);
System.out.println("你今年"+year+"岁");
}
}
3、任意输入两个日期,比较大小?
package com.timedate.test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class compareDate {
public static void main(String[] args) throws ParseException {
Scanner scanner = new Scanner(System.in);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.print("请输入第一个日期(格式为yyyy-MM-dd):");
String day1 = scanner.nextLine();
Date dayA = sdf.parse(day1);
System.out.print("请输入第二个日期(格式为yyyy-MM-dd):");
String day2 = scanner.nextLine();
Date dayB = sdf.parse(day2);
Integer i = dayA.compareTo(dayB);
if (i != 0) {
System.out.println(i > 0 ? "第一个日期比第二个日期大" : "第一个日期比第二个日期小");
} else {
System.out.println("两个日期一样大");
}
}
}
4、新闻类:标题,内容,时间。有10条新闻,按照时间的降序排列。
package com.timedate.test;
import java.text.SimpleDateFormat;
import java.util.Date;
public class NewsC {
private String title;
private String content;
private Date time;
public NewsC(String title, String content, Date time) {
this.title = title;
this.content = content;
this.time = time;
}
@Override
public String toString() {
return "新闻:" + "\n" +
title + "\n"
+ content + "\n"
+ new SimpleDateFormat("yyyy-MM-dd").format(time) + "\n";
}
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 Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
}
package com.timedate.test;
import java.text.SimpleDateFormat;
import java.util.Date;
public class news {
public static void main(String[] args) throws Exception{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
NewsC news1 = new NewsC("吴亦凡出生","吴亦凡出生于广东省广州市,父母离异后随母亲生活,10岁时和家人移民加拿大温哥华。", new SimpleDateFormat("yyyy-MM-dd").parse("1990-11-6"));
NewsC news2 = new NewsC("吴亦凡参加少年NBA","在广州市第七中学就读初三,期间担任篮球队队长,参加少年NBA中国初中篮球联赛,随后,回到加拿大在加拿大温斯顿爵士丘吉尔中学就读。", new SimpleDateFormat("yyyy-MM-dd").parse("2005-8-8"));
NewsC news3 = new NewsC("吴亦凡成为练习生","2007年,高二时参加了SM公司的全球选秀,成为练习生", new SimpleDateFormat("yyyy-MM-dd").parse("2007-11-2"));
NewsC news4 = new NewsC("吴亦凡出演少女时代","2011年,吴亦凡出演少女时代中国台湾演唱会站的影像VCR。", new SimpleDateFormat("yyyy-MM-dd").parse("2011-9-2"));
NewsC news5 = new NewsC("吴亦凡上诉","2014年5月15日,吴亦凡正式向韩国首尔中央地方法院提出请求判决与SM娱乐公司《专属合同》无效", new SimpleDateFormat("yyyy-MM-dd").parse("2014-5-15"));
NewsC news6 = new NewsC("吴亦凡电影来了","2015年1月15日,出演周星驰导演的电影《美人鱼》。", new SimpleDateFormat("yyyy-MM-dd").parse("2015-1-15"));
NewsC news7 = new NewsC("吴亦凡电影又来了","11月21日,参加《老炮儿》电影跨界演唱会,演唱歌曲《花房姑娘》和《Bad Girl》", new SimpleDateFormat("yyyy-MM-dd").parse("2015-11-21"));
NewsC news8 = new NewsC("吴亦凡发行歌曲","2019年4月19日,发行原创单曲《大碗宽面》", new SimpleDateFormat("yyyy-MM-dd").parse("2019-4-19"));
NewsC news9 = new NewsC("吴亦凡参加新说唱","7月,参加爱奇艺说唱节目《中国新说唱2020》", new SimpleDateFormat("yyyy-MM-dd").parse("2020-7-2"));
NewsC news10 = new NewsC("吴亦凡被捕","经警方调查,吴某凡(男,30岁,加拿大籍)因涉嫌强奸罪,已被朝阳公安分局依法刑事拘留,案件侦办工作正在进一步开展。", new SimpleDateFormat("yyyy-MM-dd").parse("2021-7-30"));
NewsC[] arrnews = {news1,news2,news3,news4,news5,news6,news7,news8,news9,news10};
for (int i = 0; i < arrnews.length-1; i++) {
for (int j = 0; j < arrnews.length-1-i; j++) {
if (arrnews[j].getTime().compareTo(arrnews[j+1].getTime())<=0){
NewsC temp = arrnews[j];
arrnews[j] = arrnews[j+1];
arrnews[j+1]= temp;
}
}
}
for(NewsC n : arrnews){
System.out.println(n);
}
}
}
5、学生类:姓名,学号,出生日期。找出比“张三”大的所有同学信息。
package com.timedate.test;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Student {
private String name;
private int id;
private Date time;
public Student(String name, int id, Date time) {
this.name = name;
this.id = id;
this.time = time;
}
@Override
public String toString() {
return "姓名【" + name + "】" +
" 学号【" + id + "】" +
" 出生日期【" + new SimpleDateFormat("yyyy-MM-dd").format(time) + "】";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
}
package com.timedate.test;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
public class Mzhangsan {
public static void main(String[] args) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Student st1 = new Student("吴亦凡",new Random().nextInt(9999)+10000, new SimpleDateFormat("yyyy-MM-dd").parse("1990-11-6"));
Student st2 = new Student("朱丽叶",new Random().nextInt(9999)+10000, new SimpleDateFormat("yyyy-MM-dd").parse("1900-1-3"));
Student st3 = new Student("张三",new Random().nextInt(9999)+10000, new SimpleDateFormat("yyyy-MM-dd").parse("2018-5-12"));
Student st4 = new Student("耶稣",new Random().nextInt(9999)+10000, new SimpleDateFormat("yyyy-MM-dd").parse("0001-01-01"));
Student st5 = new Student("Atlas",new Random().nextInt(9999)+10000, new Date());
Student[] arrSt = {st1,st2,st3,st4,st5};
for (int i = 0; i < arrSt.length; i++) {
if (arrSt[i].getTime().compareTo(arrSt[2].getTime())<0){
System.out.println(arrSt[i]);
}
}
}
}
6、请问今天是今年的第多少天?
package com.timedate.test;
import java.util.Calendar;
public class whenDay {
public static void main(String[] args) {
System.out.println("今天是今年的"+Calendar.getInstance().get(Calendar.DAY_OF_YEAR)+"天");
}
}
7、输入任意一个年份,得到这年的2月份有几天?
package com.timedate.test;
import java.util.Calendar;
import java.util.Scanner;
public class whenSec {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入你所要查询的年份:");
int year = sc.nextInt();
Calendar ca = Calendar.getInstance();
ca.set(year, 2, 1);
ca.add(Calendar.DATE, -1);
int day = ca.get(Calendar.DATE);
System.out.println(year+"年2月份有"+day+"天");
}
}
8、求上个月的第三天是星期几?
package com.timedate.test;
import java.util.Calendar;
public class weekWhen {
public static void main(String[] args) {
Calendar instance = Calendar.getInstance();
instance.set(instance.get(Calendar.YEAR),instance.get(Calendar.MONTH), 3);
instance.add(Calendar.MONTH, -1);
int day = instance.get(Calendar.DAY_OF_WEEK)-1;
if (day==0){
day =7;
}
System.out.println("上个月第三天是星期"+day);
}
}
9、求下个月的倒数第三天是星期几?
package com.timedate.test;
import java.util.Calendar;
public class WhenWeekDay {
public static void main(String[] args) {
Calendar instance = Calendar.getInstance();
instance.set(instance.get(Calendar.YEAR),instance.get(Calendar.MONTH), 1);
instance.add(Calendar.MONTH, 2);
instance.add(Calendar.DATE, -3);
int day = instance.get(Calendar.DAY_OF_WEEK)-1;
if (day==0){
day =7;
}
System.out.println("下个月的倒数第三天是星期"+day);
}
}