当前位置: 移动技术网 > IT编程>开发语言>Java > Java中的SimpleDateFormat使用详解

Java中的SimpleDateFormat使用详解

2019年07月22日  | 移动技术网IT编程  | 我要评论
public class simpledateformat extends dateformat 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());
  }  
}

效果:

2004年12月16日 17时24分27秒
04/12/16 17:24
2004-12-16 17:24:27
2004年12月16日 17时24分27秒 星期四
一年中的第 351 天 一年中第51个星期 一月中第3个星期 在一天中17时 cst时区

16 dec 2004 09:24:27 gmt
2004-12-16 17:24:27
thu dec 16 17:24:27 cst 2004

下面是个javabean:

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 测试结果:

2004年12月16日 17时38分26秒 星期四
04年12月16日 17时38分
17 38 26 0965
04/12/16 17:38

以上所述是小编给大家介绍的java中的simpledateformat使用详解,希望对大家有所帮助

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网