SimpleDateFormat关于时间类的一些常用处理

项目中经常会出现对时间类的一些处理,记录一下:

实例一:
/**
* 获取当前时间是星期几? * * @param args */ public static void main(String[] args) { SimpleDateFormat format = new SimpleDateFormat("E"); Date date = new Date(); String s = format.format(date); System.out.println("s:"+s); }

 

s:星期四

 

 

实例二:
@Test
public void Test(){ Date date = new Date(); SimpleDateFormat weekFormat = new SimpleDateFormat("E"); System.out.println(weekFormat.format(date)); SimpleDateFormat monthFormat = new SimpleDateFormat("MMMM"); System.out.println(monthFormat.format(date)); SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy"); System.out.println(yearFormat.format(date)); SimpleDateFormat dayFormat = new SimpleDateFormat("dd"); System.out.println(dayFormat.format(date)); /** * 星期三 五月 2017 24 * */ }

 

 

实例三:
@Test
    public void Test1(){
         SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
            SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm"); 
            SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//等价于now.toLocaleString()
            SimpleDateFormat myFmt3=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E ");
            SimpleDateFormat myFmt4=new SimpleDateFormat(
                    "一年中的第 D 天 一年中第w个星期 一月中第W个星期 在一天中k时 z时区");
            Date now=new Date();
            System.out.println(myFmt.format(now));
            System.out.println(myFmt1.format(now));
            System.out.println(myFmt2.format(now));
            System.out.println(myFmt3.format(now));
            System.out.println(myFmt4.format(now));
            System.out.println(now.toGMTString());
            System.out.println(now.toLocaleString());
            System.out.println(now.toString());
    }
2017年05月25日 21时23分11秒
17/05/25 21:23
2017-05-25 21:23:11
2017年05月25日 21时23分11秒 星期四 
一年中的第 145 天 一年中第21个星期 一月中第4个星期 在一天中21时 CST时区
25 May 2017 13:23:11 GMT
2017-5-25 21:23:11
Thu May 25 21:23:11 CST 2017

 

以上!!!

 

posted @ 2017-05-25 21:24  Mr_伍先生  阅读(1372)  评论(0编辑  收藏  举报