java常用类之Date类

目录

一:简单的测试

二:java.util.Date​

 三:DateFormat时间格式化类

格式化字符的含义(不需要记)

parse和format函数


一:简单的测试

System.currentTimeMillis()返回当前时刻的毫秒数

 

 

package com.changYongLei;

public class TestDate {
    public static void main(String[] args) {
        //包装类转为基本类型
        long a=Long.MAX_VALUE/(1000l*3600*24*365);//大约在2.9亿年后
        long time=System.currentTimeMillis();//代表当前时刻的毫秒数
        System.out.println(a);
        System.out.println(time);
    }
}

 

二:java.util.Date

 

 三:DateFormat时间格式化类

格式化字符的含义(不需要记)

 

DateFormat是抽象类,不能够创建对象,所以我们用他的子类SimpleDateFormat来创建对象

parse和format函数

format: 按指定的目标格式把Date对象转换为String

parse: 按指定的源格式把String转换为Date对象

下面看代码

package com.changYongLei;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.SimpleFormatter;

/**
 * 测试时间对象和字符串的相互转化
 * 使用 DateFormat,SimpleDateFormat
 */
public class DateFormat {
    public static void main(String[] args) throws ParseException {//将异常直接丢出去
        SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//固定的格式
        //字符串转为Date对象
        //parse: 按指定的源格式把String转换为Date对象
        Date ss= s.parse("2021-9-14 13:51:52");
        System.out.println(ss.getTime());
        //Date对象转为字符串
        Date d=new Date(1000l*3600*24*365*52);
        //format: 按指定的目标格式把Date对象转换为String
        String dd=s.format(d);
        System.out.println(dd);

    }
}

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值