日常学习之-时间格式化类

日常学习之-时间格式化类 SimpleDateForma

**
 *
 * 〈测试时间格式类〉
 *  DateFormat
 *      public abstract class DateFormat extends Format {...}
 *      public class SimpleDateFormat extends DateFormat {...}
 * 从两个类结构可以看出,DateFormat是一个抽象类,如果要使用功能,则使用它的子类SimpleDateFormat
 * 自定义时间格式:1.   y   =*                  2.  M   =*                  3.  d   =*                  4.  H   =*                  5.  m   =*                  6.  s   =*--日    时:分:秒
 *  一般完整的格式为:yyyy-MM-dd HH:mm:ss
 *
 * 构造方法:    public SimpleDateFormat(String pattern):pattern为自定义时间格式
 *
 *  方法:1.public final String format(Date date):将date数据类型按照自定义的格式转化为字符串
 *         2.public Date parse(String strDate) throws ParseException:将字符串事件通过自定义格式转化为date数据类型
 *
 *
 *
 *:  y : (常规使用四个y 如:yyyy)
 *  月 : M :(常规使用两个M 如:MM)
 *  日 : d :(常规使用两个d 如:dd) ---- 分 :m :(常归使用两个m 如:mm) ---- 秒 :s :(常归使用两个s 如:ss)
 */
public class MyTest {
  public static void main(String[] args) throws Exception {
    Date date = new Date();
    System.out.println("没有格式化:"+date);
    //年:y
    //1. 一个 y 时返回四位数年份
    String formatYear1 = new SimpleDateFormat("y").format(date);
    System.out.println("一个y:"+formatYear1 );

    //2. 两个 y 时候返回两位数年份,例如2020,则返回20
    String formatYear2 = new SimpleDateFormat("yy").format(date);
    System.out.println("两个y:"+formatYear2 );


    // 3. 三个 y 是的时候返回四位数年份,例如2020
    String formatYear3 = new SimpleDateFormat("yyy").format(date);
    System.out.println("三个y:"+formatYear3 );

    // 4.四个 y 的时候返回四位数年份,例如2020
    String formatYear4 = new SimpleDateFormat("yyyy").format(date);
    System.out.println("四个y:"+formatYear4 );

    // 5. 四个以上 y 的时候,则在返回的年份前面补0,例如:02020、002020
    String formatYear5 = new SimpleDateFormat("yyyyy").format(date);
    System.out.println("五个y:"+formatYear5 );

    System.out.println("===================================================");
    // 月:M
      //1.一个M时候,返回一位数,如四月,就返回4,不会返回04,如当前是11月,则就返回当前月
    String formatMonth1 = new SimpleDateFormat("M").format(date);
    System.out.println("一个M的时候:"+formatMonth1 );

    // 2.两个M的时候,返回两位数,如果当前月为单月,则前面补0
    String formatMonth2 = new SimpleDateFormat("MM").format(date);
    System.out.println("两个M的时候:"+formatMonth2 );

    // 3.三个M的或者以上的M的时候,则返回默认格式
    String formatMonth3 = new SimpleDateFormat("MMMM").format(date);
    System.out.println("三个M或者以上的时候:"+formatMonth3 );

    System.out.println("============================================");

    // 日:d ,
      //一个d的时候,返回一位数,单个数字的前面不补0
    String formatDate1 = new SimpleDateFormat("d").format(date);
    System.out.println("一个d的时候:"+formatDate1 );
    // 两个d的时候,返回两位数,单个数字前面补0
    String formatDate2 = new SimpleDateFormat("dd").format(date);
    System.out.println("两个d的时候:"+formatDate2 );
    // 三个d的时候,则在两位数前面补0
    String formatDate3 = new SimpleDateFormat("ddd").format(date);
    System.out.println("三个d或者更多的时候:"+formatDate3 );

    System.out.println("====================");

    // 字符串转Date方法
    String strDate= "2020-04-12 12:13:111";
    Date pareseTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(string);
    System.out.println(pareseTime);
  }

}

控制台结果:
没有格式化:Sun Apr 12 20:01:27 CST 2020
一个y:2020
两个y:20
三个y:2020
四个y:2020
五个y:02020
===================================================
一个M的时候:4
两个M的时候:04
三个M或者以上的时候:Sun Apr 12 20:01:27 CST 2020
============================================
一个d的时候:12
两个d的时候:12
三个d或者更多的时候:012
====================
Sun Apr 12 12:14:51 CST 2020

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bboy_guan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值