时间类应用

时间类应用

package Demo001;
/*
    java.util.Date 表示日期和时间的类
    类 Date 精确到毫秒
    把毫秒转换为日期:
    1天=24 x 60 x 60=86400 秒=86400x1000=86400000毫秒




 */
public class Demo01date {
    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis());//获取当前系统一共到1970年1月1日经历了多少毫秒
    }
}
package Demo001;
import  java.util.Date;//导入包
public class Demo02date {
    public static void main(String[] args) {
        demo01();
        demo02();
       demo03();
    }
    private  static  void demo01(){
        Date date =new Date();
        System.out.println(date);//Sat Apr 17 08:35:12 GMT+08:00 2021

    }
    private  static  void demo02(){
        Date date =new Date(0L);
        System.out.println(date);//Thu Jan 01 08:00:00 GMT+08:00 1970
        date =new Date(1618619905988L);
        System.out.println(date);//Sat Apr 17 08:38:25 GMT+08:00 2021
    }
    private  static  void demo03(){
        Date date =new Date();
        long time=date.getTime();//Sat Apr 17 08:41:33 GMT+08:00 2021 自1970年到现在
        System.out.println(date);
    }
}
import java.security.Signature;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Dem0date {
    public static void main(String[] args) throws ParseException {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入您的出生日期 格式为yyyy-MM-dd");
        String birthdaydateString=sc.next();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date birthdayDate =sdf.parse(birthdaydateString);
        long birthdayDateTime =birthdayDate.getTime();
        long todayTime=new Date().getTime();
        long time=birthdayDateTime-todayTime;
        System.out.println(time/1000/60/60/24);

    }
}

计算出生时间

import java.security.Signature;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Dem0date {
    public static void main(String[] args) throws ParseException {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入您的出生日期 格式为yyyy-MM-dd");
        String birthdaydateString=sc.next();//1.获取生日
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//2.使用DateFormat中的parse将出生日期解析为Date格式
        Date birthdayDate =sdf.parse(birthdaydateString);
        long birthdayDateTime =birthdayDate.getTime();//3.把date出生日期转换为毫秒
        long todayTime=new Date().getTime();//4.获取现在时间毫秒
        long time=todayTime-birthdayDateTime;
        System.out.println(time/1000/60/60/24);

    }
    //天=毫秒/1000/60/60/24   1 毫秒=0.001 秒
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值