zoukankan      html  css  js  c++  java
  • 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

    以上!!!

  • 相关阅读:
    JDK源码学习之 java.util.concurrent.automic包
    本地工程引入maven工程的配置方式
    JAVA配置&注解方式搭建简单的SpringMVC前后台交互系统
    Spring常用注解简析
    HTTP/HTTPS GET&POST两种方式的实现方法
    【JS语法】作用域与绑定图示
    【编程范式】函数式基础图示
    【技术思维】写好你的博客
    在Nginx服务器上用Jenkins发布Vue/React代码的步骤
    React事件绑定总结
  • 原文地址:https://www.cnblogs.com/mr-wuxiansheng/p/6906034.html
Copyright © 2011-2022 走看看