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. 【boost】使用装饰者模式改造boost::thread_group

    在项目中使用boost::thread_group的时候遇到几个问题: 1.thread_group不提供删除全部thread列表的方法,一直使用create会是其内部列表不断增加. 2.thread ...

  2. 【原创】Docker容器及Spring Boot微服务应用

    Docker容器及Spring Boot微服务应用 1 什么是Docker 1.1 Docker的出现 问题一:项目实施环境复杂问题 传统项目实施过程中经常会出现“程序在我这跑得好好的,在你那怎么就不 ...

  3. QCon2013上海站总结 -- 前端开发

    选择这个专题开始主要有两个原因:一是这次会议关于前端开发的内容不多.二是我做过几年前端开发,这个专题对我来说会容易点:) 这次QCon上海关于前端开发有一个Keynote,一个Javascript专题 ...

  4. permission denied部署django 遇到没有python_egg_cache的问题解决

    检查/etc/httpd/logs/error_log,看是否有如下错误: [Errno 13] Permission denied: '/var/www/.python-eggs' 这时候需要编辑“ ...

  5. <转>Linux环境进程间通信(三)

    原文链接:http://www.ibm.com/developerworks/cn/linux/l-ipc/part3/index.html 原文内容: 消息队列(也叫做报文队列)能够克服早期unix ...

  6. C++11角括号

    [C++11角括号] 标准 C++ 的剖析器一律将 ">>" 视为右移运算符. 但在样板定义式中,绝大多数的场合其实都代表两个连续右角括号. 为了避免剖析器误判,撰码时 ...

  7. 微软IOC容器Unity简单代码示例1

    @(编程) 1. 通过Nuget下载Unity 这个就不介绍了 2. 接口代码 namespace UnityDemo { interface ILogIn { void Login(); } } 3 ...

  8. C++视频课程小结(3)

    C++远征之封装篇(上) 章节介绍: 每章小结: 第一章:课程介绍. 按照惯例是章节的总介绍,内容明显多了很多(为了做作业我还要赶进度的说),主要说了:类和对象是本章的主角,然后还有很多配角,像数据成 ...

  9. SetTimer函数的用法

    什么时候我们需要用到SetTimer函数呢?当你需要每个一段时间执行一件事的的时候就需要使用SetTimer函数 了.使用定时器的方法比较简单,通常告诉WINDOWS一个时间间隔,然后WINDOWS以 ...

  10. (剑指Offer)面试题24:二叉搜索树的后序遍历序列

    题目: 输入一个整数数组,判断该数组是不是某个二叉搜索树的后序遍历的结果,如果是则返回true,否则返回false. 假设输入的数组的任意两个数字都互不相同. 思路: 根据二叉搜索树的后序遍历特点,很 ...