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());
    }    
    
}

效果:
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

2. java SimpleDateFormat 英文格式

//2012-01-16
System.out.println((new SimpleDateFormat("yyyy-MM-dd")).format(new Date()));
//16-Jan-2012
System.out.println((new SimpleDateFormat("dd-MMM-yyyy",Locale.ENGLISH)).format(new Date()));
//16-Jan-2012 Monday
System.out.println((new SimpleDateFormat("dd-MMM-yyyy EEEEE",Locale.ENGLISH)).format(new Date()));
//16-Jan-2012 Monday PM
System.out.println((new SimpleDateFormat("dd-MMM-yyyy EEEEE aa",Locale.ENGLISH)).format(new Date()));

3. 与毫秒的相互转换

     public static void main(String argv[]){ 

         String str = "2015 Aug 22  00:37:53.305";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd HH:mm:ss.SSS",Locale.ENGLISH);
long millionSeconds = 0;
try {
millionSeconds = sdf.parse(str).getTime();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//毫秒
System.out.println(millionSeconds);
Date date = new Date(millionSeconds);
System.out.println(sdf.format(date));
}

输出结果:

1440175073305
2015 Aug 22 00:37:53.305

4. 其它参考链接

SimpleDateFormat 参数

SimpleDateFormat使用详解及与毫秒的相互转换的更多相关文章

  1. SimpleDateFormat使用详解——日期、字符串应用

    public class SimpleDateFormat extends DateFormat SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类. 它允许格式化 (d ...

  2. SimpleDateFormat使用详解

    http://blog.csdn.net/gubaohua/article/details/575488 public class SimpleDateFormat extends DateForma ...

  3. (转)SimpleDateFormat使用详解

    1 SimpleDateFormat 介绍 public class SimpleDateFormat extends DateFormat SimpleDateFormat 是一个以国别敏感的方式格 ...

  4. SimpleDateFormat使用详解 <转>

    public class SimpleDateFormat extends DateFormat SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类. 它允许格式化 (d ...

  5. Java中SimpleDateFormat用法详解

    所有已实现的接口: Serializable, Cloneable SimpleDateFormat 是一个以与语言环境有关的方式来格式化和解析日期的具体类.它允许进行格式化(日期 -> 文本) ...

  6. DateFormat与SimpleDateFormat区别和使用详解

    DateFormat类 此类是一个日期的格式化类,用来格式化日期.具体日期可以通过java.util.Date类来获取. DateFormat类的定义:此类是定义在java.test包中的. publ ...

  7. SimpleDataFormat详解

    [转]SimpleDateFormat使用详解 public class SimpleDateFormat extends DateFormat SimpleDateFormat 是一个以国别敏感的方 ...

  8. android中getSystemService详解

        android的后台运行在很多service,它们在系统启动时被SystemServer开启,支持系统的正常工作,比如MountService监 听是否有SD卡安装及移除,ClipboardS ...

  9. 图像处理之基础---yuv420及其rgb,bayer, yuv, RGB的相互转换详解

    YUV格式解析1(播放器——project2) 根据板卡api设计实现yuv420格式的视频播放器 打开*.mp4;*.264类型的文件,实现其播放. 使用的视频格式是YUV420格式   YUV格式 ...

随机推荐

  1. codejam环境熟悉—Minimum Scalar Product

    今天准备熟悉一下codejam的在线编程,为google的笔试做准备,因此按照codejam上对新手的建议,先用了一个简单的题目来弄清楚流程.记录一下需要注意的地方.   1.输入输出 输入输出重定位 ...

  2. 基于gSOAP使用头文件的C语言版web service开发过程例子

    基于gSOAP使用头文件的C语言版web service开发过程例子 一服务端 1 打开VS2005,创建一个工程,命名为calcServer. 2 添加一个头文件calc.h,编辑内容如下: 1// ...

  3. mysql create table - data_type length -- clwu

    mysql create table 时,有时需要指定  data_type length http://dev.mysql.com/doc/refman/5.5/en/create-table.ht ...

  4. Uva 11183 - Teen Girl Squad (最小树形图)

    Problem ITeen Girl Squad Input: Standard Input Output: Standard Output You are part of a group of n  ...

  5. Flex SDK包内文件夹内容说明

    打开SDK目录,让我们看看SDK中都包含了哪些内容: ant包含Flex对Ant的支持库,JAR和Java源码都有,方便我们基于Ant完成对项目的构建 asdoc基于ASDOC我们可以很方便的生成代码 ...

  6. 实体框架 (EF) 入门 => 四、CodeFirst 枚举支持

    当使用 Code First 开发时,通常是从编写用来定义概念(域)模型的 .NET Framework 类开始. 插入记录没有为 Budget 赋值. 数值类型默认值为0,数据库中都为not nul ...

  7. cocos2d-x 3.2 椭圆运动

    直接上代码: // // OvalAction.h // LSWGameIOS // // Created by lsw on 14-10-27. // // #ifndef __LSWGameIOS ...

  8. 【132】iPad使用相关问题

    1.想iPad中导入音乐文件,保留原音乐文件名称 [方法]需要删除音乐文件中的“标题”和“参与创作的艺术家”,直接删除的话,效率比较低,需要借助一款软件——foobar2000,选中导入的文件,然后属 ...

  9. C++11空指针

    [C++11空指针] 早在 1972 年,C语言诞生的初期,常数 0 带有常数及空指针的双重身分. C 使用 preprocessor macro NULL 表示空指针, 让 NULL 及 0 分别代 ...

  10. HTML结构标签介绍

    HTML:超文本标记语言   介绍HTML基本标记   1:头部标记(head)-----  头部的内容不会再页面上显示 在头部元素中,一般需要包括标题<title>,基本信息(文档样式, ...