DateFormat 日期格式化類(必須掌握)


package com.sxt.utils.date1;

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

/*
 * 日期格式化類(必須掌握)
 * API:
 * G  Era 標志符  Text  AD  
y  年  Year  1996; 96  
M  年中的月份  Month  July; Jul; 07  
w  年中的周數  Number  27  
W  月份中的周數  Number  2  
D  年中的天數  Number  189  
d  月份中的天數  Number  10  
F  月份中的星期  Number  2  
E  星期中的天數  Text  Tuesday; Tue  
a  Am/pm 標記  Text  PM  
H  一天中的小時數(0-23)  Number  0  
k  一天中的小時數(1-24)  Number  24  
K  am/pm 中的小時數(0-11)  Number  0  
h  am/pm 中的小時數(1-12)  Number  12  
m  小時中的分鍾數  Number  30  
s  分鍾中的秒數  Number  55  
S  毫秒數  Number  978  
z  時區  General time zone  Pacific Standard Time; PST; GMT-08:00  
Z  時區  RFC 822 time zone  -0800  

 */
public class TestDate3 {
    @SuppressWarnings("deprecation")
    public static void main(String[] args) {
        //Date ------> Date對象
        //創建日期格式化對象   因為DateFormat類為抽象類 所以不能new
        DateFormat bf = new SimpleDateFormat("yyyy-MM-dd E a HH:mm:ss");//多態
        //2017-04-19 星期三 下午 20:17:38
        
        Date date = new Date();//創建時間
        String format = bf.format(date);//格式化 bf.format(date);
        System.out.println(format);
        
        //String ------->Date對象
        String s = "2017-04-19 星期三 下午 20:17:38";//有格式要求 必須和自定義模式嚴格一致
        try {
            Date parse = bf.parse(s);// df.parse(s);String轉成對象
            System.out.println(parse);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM