`
xubaolin9
  • 浏览: 96158 次
社区版块
存档分类
最新评论

java日期格式

    博客分类:
  • misc
阅读更多

http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html

Date and Time Patterns

Date and time formats are specified by date and time pattern strings. Within date and time pattern strings, unquoted letters from 'A' to 'Z' and from 'a' to 'z' are interpreted as pattern letters representing the components of a date or time string. Text can be quoted using single quotes (') to avoid interpretation. "''" represents a single quote. All other characters are not interpreted; they're simply copied into the output string during formatting or matched against the input string during parsing.

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

LetterDate or Time ComponentPresentationExamples
G Era designator Text AD
y Year Year 199696
M Month in year Month JulyJul07
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 TuesdayTue
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 TimePSTGMT-08:00
Z Time zone RFC 822 time zone -0800
Pattern letters are usually repeated, as their number determines the exact presentation:
  • Text: For formatting, if the number of pattern letters is 4 or more, the full form is used; otherwise a short or abbreviated form is used if available. For parsing, both forms are accepted, independent of the number of pattern letters.
  • Number: For formatting, the number of pattern letters is the minimum number of digits, and shorter numbers are zero-padded to this amount. For parsing, the number of pattern letters is ignored unless it's needed to separate two adjacent fields.
  • Year: For formatting, if the number of pattern letters is 2, the year is truncated to 2 digits; otherwise it is interpreted as a number.

    For parsing, if the number of pattern letters is more than 2, the year is interpreted literally, regardless of the number of digits. So using the pattern "MM/dd/yyyy", "01/11/12" parses to Jan 11, 12 A.D.

    For parsing with the abbreviated year pattern ("y" or "yy"), SimpleDateFormat must interpret the abbreviated year relative to some century. It does this by adjusting dates to be within 80 years before and 20 years after the time theSimpleDateFormat instance is created. For example, using a pattern of "MM/dd/yy" and a SimpleDateFormat instance created on Jan 1, 1997, the string "01/11/12" would be interpreted as Jan 11, 2012 while the string "05/04/64" would be interpreted as May 4, 1964. During parsing, only strings consisting of exactly two digits, as defined byCharacter.isDigit(char), will be parsed into the default century. Any other numeric string, such as a one digit string, a three or more digit string, or a two digit string that isn't all digits (for example, "-1"), is interpreted literally. So "01/02/3" or "01/02/003" are parsed, using the same pattern, as Jan 2, 3 AD. Likewise, "01/02/-3" is parsed as Jan 2, 4 BC.

  • Month: If the number of pattern letters is 3 or more, the month is interpreted as text; otherwise, it is interpreted as a number.
  • General time zone: Time zones are interpreted as text if they have names. For time zones representing a GMT offset value, the following syntax is used:
         GMTOffsetTimeZone:
                 GMT Sign Hours : Minutes
         Sign: one of
                 + -
         Hours:
                 Digit
                 Digit Digit
         Minutes:
                 Digit Digit
         Digit: one of
                 0 1 2 3 4 5 6 7 8 9
    Hours must be between 0 and 23, and Minutes must be between 00 and 59. The format is locale independent and digits must be taken from the Basic Latin block of the Unicode standard.

    For parsing, RFC 822 time zones are also accepted.

  • RFC 822 time zone: For formatting, the RFC 822 4-digit time zone format is used:
         RFC822TimeZone:
                 Sign TwoDigitHours Minutes
         TwoDigitHours:
                 Digit Digit
    TwoDigitHours must be between 00 and 23. Other definitions are as for general time zones.

    For parsing, general time zones are also accepted.

SimpleDateFormat also supports localized date and time pattern strings. In these strings, the pattern letters described above may be replaced with other, locale dependent, pattern letters. SimpleDateFormat does not deal with the localization of text other than the pattern letters; that's up to the client of the class.

 

Examples

The following examples show how date and time patterns are interpreted in the U.S. locale. The given date and time are 2001-07-04 12:08:56 local time in the U.S. Pacific Time time zone.
Date and Time PatternResult
"yyyy.MM.dd G 'at' HH:mm:ss z" 2001.07.04 AD at 12:08:56 PDT
"EEE, MMM d, ''yy" Wed, Jul 4, '01
"h:mm a" 12:08 PM
"hh 'o''clock' a, zzzz" 12 o'clock PM, Pacific Daylight Time
"K:mm a, z" 0:08 PM, PDT
"yyyyy.MMMMM.dd GGG hh:mm aaa" 02001.July.04 AD 12:08 PM
"EEE, d MMM yyyy HH:mm:ss Z" Wed, 4 Jul 2001 12:08:56 -0700
"yyMMddHHmmssZ" 010704120856-0700

分享到:
评论

相关推荐

    Java日期格式转换.doc

    Java 日期格式转换是指在 Java 编程语言中将日期和时间从一种格式转换为另一种格式的过程。日期格式转换是 Java 编程中非常重要的一部分,因为日期和时间是许多应用程序中的关键元素。 在 Java 中,日期和时间通常...

    java日期格式转换

    ### Java日期格式转换详解 在Java开发中,对日期和时间进行操作是非常常见的需求之一。本文将基于给定文件中的代码示例,详细介绍如何在Java中实现日期格式的转换,并探讨其中涉及的关键知识点。 #### 一、理解...

    Java时间格式转换大全

    本文将深入探讨Java中与时间格式转换相关的知识点,包括如何使用`SimpleDateFormat`类进行时间的格式化和解析,以及如何处理常见的日期时间操作。 ### Java中的时间格式转换 #### 1. `SimpleDateFormat`类 `...

    Java日期格式化及其使用例子收集

    ### Java日期格式化详解及其应用实例 #### 一、引言 在Java开发过程中,日期时间的处理是一项非常常见的需求。对于日期时间的格式化、解析等操作,`SimpleDateFormat` 类提供了强大的支持。本文将详细介绍`...

    java日期格式的转换

    Java 日期格式转换 Java 中日期格式转换是一个常见的问题,在编程中,我们经常需要将字符串转换为日期对象,或者将日期对象转换为字符串。下面,我们将详细讨论 Java 中日期格式转换的知识点。 字符串转换为 java....

    Java时间格式转化

    - 然后创建了一个`SimpleDateFormat`对象`formatter`,并指定了日期时间格式为“年-月-日 时:分:秒”。 - 使用`formatter`的`format()`方法将`date`对象转换为字符串`dateString`。 - 最后打印出转换后的字符串。 #...

    _java时间格式大全.doc

    Java 中的时间格式是指使用 Java 语言来处理和操作日期和时间的方式。Java 提供了多种方式来处理时间格式,包括使用 `java.util.Date`、`java.util.Calendar`、`java.text.SimpleDateFormat` 等类。 获取当前时间 ...

    java日期格式化

    Java日期格式化是Java开发中常见的一项任务,它涉及到日期和时间的处理,尤其是在数据输入输出、用户界面显示以及数据库交互等场景。Java提供了一系列的类来帮助我们完成日期和时间的格式化工作,其中最常用的包括`...

    java日期格式化,针对各种日期进行不同的格式化

    java日期格式化,针对各种日期进行不同的格式化,获取两个日期之间的日期(包含前后)获取未来 第 past 天的日期获取过去第几天的日期

    与众不同的 Java 日期格式化大全

    Java 日期格式化大全是 Java 中一个非常重要的知识点,它对于将时间字符串转换为日期或将日期转换为时间字符串起着关键作用。在 Java 中,我们通常使用 `java.text.SimpleDateFormat` 类来处理日期和时间字符串的...

    java 时间格式转换符的使用

    虽然`SimpleDateFormat`是Java的旧API,但在Java 8及更高版本中,引入了`java.time`包,其中的`DateTimeFormatter`提供了更强大且安全的日期时间格式化功能。例如: ```java DateTimeFormatter formatter = ...

    java时间格式转换

    ### Java时间格式转换详解 在Java开发过程中,对日期时间的处理是非常...总之,在Java中进行日期时间格式的转换是一项基本而重要的技能,通过灵活运用`SimpleDateFormat`类,可以轻松应对各种日期时间格式化的场景。

    java时间格式大全

    7. **日期时间的格式化和解析**:除了`SimpleDateFormat`,Java 8还引入了`DateTimeFormatter`,它提供了更多的日期时间格式选项,并且在处理日期时间方面更加安全和线程安全。 8. **日期时间的转换**:可以使用`...

    JAVA日期格式大全

    `JAVA日期格式大全`这个主题涵盖了许多关于在Java中如何格式化、解析和操作日期的技巧和方法。这篇博客文章,虽然链接未提供具体内容,但我们可以根据常规知识和Java API来探讨日期和时间处理的关键知识点。 1. **...

    java时间格式转换大全

    Java时间格式转换是Java开发中常见的任务,涉及到日期和时间的处理。在Java中,主要通过`java.util.Date`、`java.util.Calendar`以及`java.text.SimpleDateFormat`等类来完成。下面将详细介绍这些类和方法在时间格式...

    oracle日期格式和java日期格式区别 HH24:mm:ss和HH24:mi:ss的区别

    oracle日期格式和java日期格式区别 HH24:mm:ss和HH24:mi:ss的区别 1.java 1)分钟用mm表示 24小时制: java(区分大小写):yyyy-MM-dd HH:mm:ss 12小时制: java(区分大小写):yyyy-MM-dd hh:mm:ss 2)...

Global site tag (gtag.js) - Google Analytics