2022.01.17 日期总结使用

一:Date   date= new  Date()的使用

在Java中,如果我们想获取当前时间,一般会使用Date类的无参构造函数,如下所示,我们获取到当前时间并输出:

import java.util.Date;

 public class SimpleDateFormatDemo {

    public static void main(String[] args) {

        Date currentTime = new Date();

        System.out.println(currentTime); // 输出:Mon Feb 18 10:24:30 CST 2019

    }

}

我们希望的格式都是类似于2019-02-18,2019-02-18 10:24:30,2019/02/18这样的,此时我们就需要用到java.text.SimpleDateFormat来自定义格式。

SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

1.使用format()方法将日期转换为字符串

 Date currentTime = new Date();

simpleDateFormat1.format(currentTime);

2.使用parse()方法将字符串转换为日期: (parse中有s就是参数填字符串)

String strDate1 = "2019-02-18 13:58";

 Date date1 = simpleDateFormat1.parse(strDate1);

 2.自己做的测试

 

 测试二:

 

posted @ 2022-01-18 09:43  iLisa  阅读(58)  评论(0编辑  收藏  举报