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

    以上!!!

  • 相关阅读:
    springmvc log4j 配置
    intellij idea maven springmvc 环境搭建
    spring,property not found on type
    intellij idea maven 工程生成可执行的jar
    device eth0 does not seem to be present, delaying initialization
    macos ssh host配置及免密登陆
    centos7 搭建 docker 环境
    通过rest接口获取自增id (twitter snowflake算法)
    微信小程序开发体验
    gitbook 制作 beego 参考手册
  • 原文地址:https://www.cnblogs.com/mr-wuxiansheng/p/6906034.html
Copyright © 2011-2022 走看看