zoukankan      html  css  js  c++  java
  • (转)SimpleDateFormat使用

    1  SimpleDateFormat 

    public class SimpleDateFormat extends DateFormat

    SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类。 它允许格式化 (date -> text)、语法分析 (text -> date)和标准化

    SimpleDateFormat 允许以为日期-时间格式化选择任何用户指定的方式启动。 但是,希望用 DateFormat 中的getTimeInstance、 getDateInstance 或 getDateTimeInstance 创建一个日期-时间格式化程序。 每个类方法返回一个以缺省格式化方式初始化的日期/时间格式化程序。 可以根据需要用 applyPattern 方法修改格式化方式。 

    SimpleDateFormat函数的继承关系:
    java.lang.Object
       |
       +----java.text.Format
               |
               +----java.text.DateFormat
                       |
                       +----java.text.SimpleDateFormat

    下面是个小例子:

    import java.text.*;
    import java.util.Date;
    /**
      SimpleDateFormat函数语法:
      
      G 年代标志符
      y 年
      M 月
      d 日
      h 时 在上午或下午 (1~12)
      H 时 在一天中 (0~23)
      m 分
      s 秒
      S 毫秒
      E 星期
      D 一年中的第几天
      F 一月中第几个星期几
      w 一年中第几个星期
      W 一月中第几个星期
      a 上午 / 下午 标记符 
      k 时 在一天中 (1~24)
      K 时 在上午或下午 (0~11)
      z 时区
     */
    public class FormatDateTime {
        public static void main(String[] args) {
            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());
        }    
        
    }

    运行结果:

    2015年10月13日 09时51分14秒
    15/10/13 09:51
    2015-10-13 09:51:14
    2015年10月13日 09时51分14秒 星期二 
    一年中的第 286 天 一年中第42个星期 一月中第3个星期 在一天中9时 CST时区
    13 Oct 2015 01:51:14 GMT
    2015-10-13 9:51:14
    Tue Oct 13 09:51:14 CST 2015

    ps:为什么有的格式大写,有的格式小写,那是怕避免混淆,例如MM是月份,mm是分;HH是24小时制,而hh是12小时制

    下面是个JavaBean:

    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class FormatDateTime {
        
        public static String toLongDateString(Date dt){
            SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E ");        
            return myFmt.format(dt);
        }
        
        public static String toShortDateString(Date dt){
            SimpleDateFormat myFmt=new SimpleDateFormat("yy年MM月dd日 HH时mm分");        
            return myFmt.format(dt);
        }    
        
        public static String toLongTimeString(Date dt){
            SimpleDateFormat myFmt=new SimpleDateFormat("HH mm ss SSSS");        
            return myFmt.format(dt);
        }
        public static String toShortTimeString(Date dt){
            SimpleDateFormat myFmt=new SimpleDateFormat("yy/MM/dd HH:mm");        
            return myFmt.format(dt);
        }
        
        public static void main(String[] args) {
    
            Date now=new Date();
    
            System.out.println(FormatDateTime.toLongDateString(now));
            System.out.println(FormatDateTime.toShortDateString(now));
            System.out.println(FormatDateTime.toLongTimeString(now));
            System.out.println(FormatDateTime.toShortTimeString(now));
        }    
        
    }

    调用的main 测试结果:

    2015年10月13日 09时58分01秒 星期二 
    15年10月13日 09时58分
    09 58 01 0787
    15/10/13 09:58

    SimpleDateFormat类的使用并不复杂,常用的方法在下面的例子就包含了:

    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    
    public class Test
    {
        public static void main(String[] args)
        {
            //利用构造函数设置格式化模板
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
            Date date = new Date();
            //执行格式化功能
            System.out.println(formatter.format(date));
            //设置格式化模板
            formatter.applyPattern("yyyy:MM:dd");    //yyyy-MM-dd
            System.out.println(formatter.format(date));
        }
    }

    2 时间格式转换

    将yyyyMMddhhmmss(20140707103709)转换为yyyy-MM-dd HH:mm:ss

    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddhhmmss");  
    Date date = (Date) sdf1.parse(s);  
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
    System.out.print(sdf2.format(date)); 

     用于String和Date之间相互转换

  • 相关阅读:
    R set.seed()
    R tapply()
    R table
    清除R console中出现加号+
    r向量映射
    Java常识1
    IDEA配置
    SQL.字符串重叠项数量统计
    SQL.数据库内拆分字符串并返回数据表
    IDEA--TomCat配置
  • 原文地址:https://www.cnblogs.com/lixuwu/p/5676116.html
Copyright © 2011-2022 走看看