• Date类


     1 import java.text.DateFormat;
     2 import java.text.ParseException;
     3 import java.text.SimpleDateFormat;
     4 import java.util.Date;
     5 /**
     6  * 一.Date类获得的是当前时间
     7  * 二.
     8  *    1.SimpleDateFormat(y-m-d h:m:s)有参构造函数设置时间格式
     9  *    2.有两个方法   format()方法:将时间date转换为字符串的格式(String对象接收)
    10  *               parse()方法:将字符串格式的时间解析为Date形式(Date对象接收)
    11  * 三.gettime()方法:获得与1970之间的毫秒数
    12  * @author 罗摩衔那
    13  *
    14  */
    15 public class Demo_Date 
    16 {
    17    public static void main(String[] args) throws ParseException 
    18    {
    19       Date d=new Date();//获得当前时间
    20       System.out.println(d);
    21       
    22       SimpleDateFormat f=new SimpleDateFormat("y-m-d h:m:s");//设置时间格式
    23       String s=f.format(d);//转换
    24       System.out.println(s);
    25       
    26       //gettime获得从1970年一月一号到现在之间的毫秒数
    27       d.getTime();
    28       //设置你的生日
    29       String birthdays="1999-1-11 3:30:30";
    30       //用格式转换对象转换生日
    31       Date birthday=f.parse(birthdays);
    32       //现在与1970之间的毫秒数  -  生日与1970之间的毫秒数
    33       long time=d.getTime()-birthday.getTime();
    34       //毫秒-->1000秒-->60(分)-->60(时)-->24(天)-->360(年)
    35       time=time/1000/60/60/24/360;
    36       System.out.println(time);
    37    }
    38 }

    温馨小提示:求取出生到现在的过了多少年的思路-->就是用现在与1970年之间的毫秒数减去你生日与1970年之间的毫秒数,再根据时间单位转换为你想求的至今多少年或者多少小时

  • 相关阅读:
    PTA(Basic Level)1048.数字加密
    PTA(Basic Level)1037.在霍格沃茨找零钱
    PTA(Basic Level)1030.完美数列
    PTA(Basic Level)1047.编程团体赛
    PTA(Basic Level)1087.有多少不同的值
    PTA(Basic Level)1077.互评成绩计算
    PTA(Basic Level)1027.打印沙漏
    PTA(Basic Level)1029.旧键盘
    记录一次排查挖矿:快速跟踪一个进程
    JVM性能、多线程排查常用命令
  • 原文地址:https://www.cnblogs.com/zjm1999/p/10025248.html
Copyright © 2020-2023  润新知