|NO.Z.00036|——————————|BigDataEnd|——|Java&核心类库.V06|——|Java.v06|SimpleDateFormat类|概念使用|

一、[SimpleDateFormat类的概念和使用]
### --- SimpleDateFormat类的概述

~~~     # 基本概念
~~~     ——>        java.text.SimpleDateFormat类主要用于实现日期和文本之间的转换。
二、常用的方法
方法声明 功能介绍
SimpleDateFormat() 使用无参方式构造对象
SimpleDateFormat(Stringpattern) 根据参数指定的模式来构造对象,
模式主要有: y-年 M-月 d-日H-时 m-分 s-秒
final String format(Datedate) 用于将日期类型转换为文本类型
Date parse(String source) 用于将文本类型转换为日期类型
三、编程代码
package com.yanqi.task13;

import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleDateFormatTest {

    public static void main(String[] args) throws Exception {

        // 1.获取当前系统时间并打印
        Date d1 = new Date();
        System.out.println("d1 = " + d1);

        // 2.构造SimpleDateFormat类型的对象并指定格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 3.实现日期类型向文本类型的转换并打印
        // alt+Enter 可以实现返回值的生成
        String format = sdf.format(d1);
        System.out.println("转换后的日期为:" + format);
        // 4.实现文本类型到日期类型的转换并打印
        Date parse = sdf.parse(format);
        System.out.println("转回日期格式的结果为:" + parse);
    }
}
四、编译打印
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=53043:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath E:\NO.Z.10000——javaproject\NO.H.00001.javase\javase\out\production\javase com.yanqi.task13.SimpleDateFormatTest
d1 = Fri Jul 30 19:53:09 CST 2021
转换后的日期为:2021-07-30 19:53:09
转回日期格式的结果为:Fri Jul 30 19:53:09 CST 2021

Process finished with exit code 0

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

posted on 2022-04-04 14:53  yanqi_vip  阅读(24)  评论(0)    收藏  举报

导航