java日期练习题

//1. 请使用日期时间相关的API,计算出一个人已经出生了多少年,再转换为多少天(一年365天)。

public class Test01 {

public static void main(String[] args) throws ParseException {

Calendar c = Calendar.getInstance();

int year = c.get(Calendar.YEAR);

Scanner sc = new Scanner(System.in);

System.out.println("请输入您的生日(格式xxxx:xx:xx):");

SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd");

String birthday = sc.next();

//将string转换为date

Date parse = sdf.parse(birthday);

System.out.println(parse);

int birthdayYear = Integer.parseInt(birthday.substring(0, 4));

System.out.println("您出生了"+(year-birthdayYear)+"年");

//获取天数

Date nowTime = new Date();

long milliSecond = nowTime.getTime() - parse.getTime();

int day = (int)(milliSecond/1000/60/60/24);

System.out.println(day);

}

}

//2. 获取当前的日期,并把这个日期转换为指定格式的字符串

// 如2088-08-08 08:08:08

public class Task02 {

public static void main(String[] args) {

Date date = new Date();

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

System.out.println(sdf.format(date));

}

}

//3. 判断2018年2月14日是星期几

public class Task03 {

public static void main(String[] args) throws ExportException {

Calendar time = Calendar.getInstance();

time.set(2023,0,4);

SimpleDateFormat sdf = new SimpleDateFormat("E");

//System.out.println(sdf.format(time.getTime()).substring(11));

System.out.println(sdf.format(time.getTime()));

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值