简单日期格式化 : 日期格式 « 数据类型 « Java 教程

En
Java 教程
1. 语言基础
2. 数据类型
3. 操作符
4. 流程控制
5. 类定义
6. 开发相关
7. 反射
8. 正则表达式
9. 集合
10. 线
11. 文件
12. 泛型
13. 本土化
14. Swing
15. Swing事件
16. 二维图形
17. SWT
18. SWT 二维图形
19. 网络
20. 数据库
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web服务SOA
27. EJB3
28. Spring
29. PDF
30. 电子邮件
31. 基于J2ME
32. J2EE应用
33. XML
34. 设计模式
35. 日志
36. 安全
37. Apache工具
38. 蚂蚁编译
39. JUnit单元测试
Java
Java 教程 » 数据类型 » 日期格式 
2. 38. 21. 简单日期格式化

With SimpleDateFormat, you can set your own date patterns. For example, dd/mm/yyyy, mm/dd/yyyy, yyyy-mm-dd, and so on.

The following pattern letters are defined (all other characters from 'A' to 'Z' and from 'a' to 'z' are reserved):

Letter   Date or Time Component   Presentation       Examples
G        Era designator             Text                AD
y        Year                       Year                1996;    96
M        Month in year               Month               July; Jul; 07
w        Week in year               Number               27
W        Week in month               Number               2
D        Day in year               Number               189
d        Day in month               Number               10
F        Day of week in month       Number               2
E        Day in week               Text               Tuesday; Tue
a        Am/pm marker               Text               PM
H        Hour in day (0-23)       Number               0
k        Hour in day (1-24)       Number               24
K        Hour in am/pm (0-11)       Number               0
h        Hour in am/pm (1-12)       Number               12
m        Minute in hour           Number               30
s        Second in minute           Number               55
S        Millisecond                Number               978
z        Time zone                   General time zone   Pacific Standard Time; PST; GMT-08:00
Z        Time zone                  RFC 822 time zone   -0800

(from Java API doc)

The more commonly used patterns can be used by a combination of y (representing a year digit), M (representing a month digit) and d (representing a date digit).

Examples of patterns are dd/MM/yyyy, dd-MM-yyyy, MM/dd/yyyy, yyyy-MM-dd.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainClass {
  public static void main(String[] args) {
    String pattern = "MM/dd/yyyy";
    SimpleDateFormat format = new SimpleDateFormat(pattern);
    try {
      Date date = format.parse("12/31/2006");
      System.out.println(date);
    catch (ParseException e) {
      e.printStackTrace();
    }
    // formatting
    System.out.println(format.format(new Date()));
  }
}
Sun Dec 31 00:00:00 PST 2006
01/26/2007
2. 38. 日期格式
2. 38. 1. 日期解析和格式DateFormat
2. 38. 2. 解析时间使用自定义格式
2. 38. 3. 剖析了自定义格式
2. 38. 4. 解析的默认格式
2. 38. 5. Parse string date value input with SimpleDateFormat('dd-MMM-yy')
2. 38. 6. 解析的日期和时间
2. 38. 7. Parse string date value input with SimpleDateFormat('E, dd MMM yyyy HH:mm:ss Z')
2. 38. 8. Parse string date value with default format: DateFormat.getDateInstance(DateFormat.DEFAULT)
2. 38. 9. Leniency
2. 38. 10. 格式化字符串符号SimpleDateFormat
2. 38. 11. SimpleDateFormat: hh:mm:ss, dd MMM yyyy hh:mm:ss zzz, E MMM dd yyyy
2. 38. 12. 格式日期采用YYYYMMDD
2. 38. 13. 表示在任期期间
2. 38. 14. 日期格式SimpleDateFormat
2. 38. 15. Demonstrate date formats with different DateFormat constants
2. 38. 16. 显示时间格式。
2. 38. 17. DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.MEDIUM)
2. 38. 18. 打印出当前的日期和时间
2. 38. 19. 日期时代变化
2. 38. 20. 格式的日期与System.out.format
2. 38. 21. 简单日期格式化
2. 38. 22. 格式化日期和时间
2. 38. 23. Four different date formats for four countries: US, UK, GERMANY, FRANCE
2. 38. 24. 各种日期格式各种日期格式
2. 38. 25. 在短格式显示日期名字
2. 38. 26. 短格式显示日期天,月
2. 38. 27. 格式的日期到日/月/年
2. 38. 28. Format current date and time with the SimpleDateFormat: dd/MM/yyyy
2. 38. 29. Format current date and time with the SimpleDateFormat: HH:mm:ss
2. 38. 30. 格式符号SimpleDateFormat
2. 38. 31. 格式化时间使用自定义格式
2. 38. 32. 更改日期格式符号
2. 38. 33. 获得短名单显示一个月的名称
2. 38. 34. Get a List of Weekday Names
2. 38. 35. Get a List of Short Weekday Names
2. 38. 36. 时间在12小时格式
2. 38. 37. 时间在24小时格式
2. 38. 38. 日期和时间与一个月
2. 38. 39. 日期和时间同一天,一个月长格式
2. 38. 40. Output current time: %tc
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.