zoukankan      html  css  js  c++  java
  • Java第12次作业--你的生日

    一、题目

    利用Calendar类计算自己的出生日期距今天多少天,再将自己的出生日期利用SimpleDateFormat类设定的格式输出显示。

    二、源代码

    /**输入出生日期,定义一个Date对象,将字符串转换为date类型,最后进行毫秒值换算,输出结果。
     */
    package com1121;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Scanner;
    public class Test {
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner sc=new Scanner(System.in);
            System.out.println("请输入您的出生日期:如 格式 yyyy-MM-dd");
            String str=sc.nextLine();
            try {
                //将字符串转换为date类型
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
                Date birthdayDate = (Date)simpleDateFormat.parse(str);
                System.out.println("您的出生日期是:"+simpleDateFormat.format(birthdayDate));
                Date nowDate = new Date();
                //毫秒值换算
                long nowSecond = nowDate.getTime();//当前毫秒值
                long birthdaySecond = birthdayDate.getTime();//生日毫秒值
                long second = (nowSecond-birthdaySecond)/1000/60/60/24;
                if(second<0){
                    System.out.println("输入有误");
                }else {
                    System.out.println("您出生至今一共度过了"+second+"天啦!");
                }
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    三、运行结果

  • 相关阅读:
    字体符号版面设计
    有人嘲笑我ps技术不够好@罗小白
    浅谈UI:
    色彩基础:
    常用的Mysql数据库操作语句大全
    汇编(坑逼之路)
    Linux学习笔记|扬帆
    坑爹的C++要课堂检测了 然而我什么都没学
    why I need a flow learn note.
    burpsuite
  • 原文地址:https://www.cnblogs.com/jingxueyan/p/11908459.html
Copyright © 2011-2022 走看看