|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   yanqi_vip  阅读(19)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示