`
aking86
  • 浏览: 82979 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论
阅读更多


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

public class TestTime2 {
        public static void main(String[] args) {
        Date date = new Date();     // 获取时间
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");  // 转换格式
        String curTime = sdf.format(date);
        System.out.println("curTime: " + curTime);   // curTime: 07:21 下午

        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
        curTime = sdf.format(date);
        System.out.println("curTime: " + curTime);
        // curTime: 2011-12-04 19:21:59.687  
     }  
}

//  //   SimpleDateFormat  /////////////////////

 

 

Open Declaration java.text.SimpleDateFormat

SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization.

SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting. However, you are encouraged to create a date-time formatter with either getTimeInstance, getDateInstance, or getDateTimeInstance in DateFormat. Each of these class methods can return a date/time formatter initialized with a default format pattern. You may modify the format pattern using the applyPattern methods as desired. For more information on using these methods, see DateFormat.

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):

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
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: If the formatter's Calendar is the Gregorian calendar, the following rules are applied.
    • 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 the SimpleDateFormat 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 by Character.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.
    Otherwise, calendar system specific forms are applied. For both formatting and parsing, if the number of pattern letters is 4 or more, a calendar specific long form is used. Otherwise, a calendar specific short or abbreviated form is used.
  • 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 Pattern Result
"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
"yyyy-MM-dd'T'HH:mm:ss.SSSZ" 2001-07-04T12:08:56.235-0700

Synchronization

Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.

Version:
1.88, 05/28/08
Author:
Mark Davis, Chen-Lieh Huang, Alan Liu
See Also:
Java Tutorial
java.util.Calendar
java.util.TimeZone
DateFormat
DateFormatSymbols



分享到:
评论

相关推荐

    Java时间控件精确到秒

    在Java编程中,时间控制是常见的需求,尤其是在开发用户界面或者进行定时任务处理时。Java标准库提供了多种方式来...通过阅读和分析源代码,我们可以深入了解这些实现细节,从而更好地理解和应用这些Java时间控制技术。

    _java时间格式大全.doc

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

    java时间工具类按照年月周维度在时间区间内输出时间点

    java时间工具类按照年月周维度在时间区间内输出时间点

    Java时间大小的比较

    Java时间大小的比较 date java

    java 时间片算法

    java 时间片算法 发生的泥泞i萨芬第几集

    转 Java校正电脑时间(java 时间同步)

    这篇博客“转 Java校正电脑时间(java 时间同步)”主要探讨了如何在Java中实现这一功能。 首先,Java中的日期和时间API包括`java.util.Date`、`java.util.Calendar`,以及从Java 8开始引入的更现代的`java.time`包。...

    java时间工具类

    java 时间 datatime 工具类

    java时间控件

    标题"java时间控件"指的是在Java应用程序中实现或使用这样的控件。描述中提到的“判断时间格式”意味着可能涉及对用户输入的时间字符串进行有效性验证,确保它符合预设的日期和时间格式。 在Java中,日期和时间处理...

    java时间格式转换大全

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

    java时间管理系统(springboot+mysql+vue完整源码+说明文档+LW).zip

    java时间管理系统(springboot+mysql+vue完整源码+说明文档+LW) 时间管理系统的功能分为管理员,用户两个部门,系统的主要功能包括首页,个人中心,系统公告管理,用户管理,时间分类管理,事件数据管理,目标数据...

    JAVA时间和日期图解.rar

    Java时间与日期处理是编程中的常见任务,尤其是在开发业务应用或者数据分析时。Java提供了多种类库来处理日期和时间,包括早期的`java.util.Date`和`Calendar`,以及Java 8引入的`java.time`包。这个"JAVA时间和日期...

    WIN7 JAVA获取时间有问题

    3. **系统时间和Java时间**: - Java获取系统时间通常是通过调用本地操作系统接口,因此,系统时间的准确性直接影响到Java程序。 - 如果系统时间不正确或与Java时区设置冲突,可能导致获取时间出错。 4. **...

    Java时间工具(带指针的圆形盘表,闹钟,定时关机,模拟屏保)

    Java时间工具是一个实用的软件应用,它集合了多种与时间管理相关的功能,如带指针的圆形盘表、闹钟、定时关机以及模拟屏保。这个应用是用Java编程语言开发的,Java以其跨平台的特性使得该工具能够在各种操作系统上...

    JAVA时间工具类(计算法定工作日工时等)

    JAVA时间工具类(计算法定工作日工时):计算某个日期加上几个工作日后的一个工作日期(除周末和法定节假日),计算某个日期后一天的工作日期(除周末和法定节假日),计算两个日期的真实工作日(除周末和法定节假日),...

    java时间格式大全

    在Java编程语言中,时间处理是一项至关重要的任务,涉及到日期、时间和时区的管理。Java提供了多种类库来处理时间,如`java.util.Date`, `java.util.Calendar`, `java.text.SimpleDateFormat`, `java.time`包(自...

    基于java的开发源码-xk-time Java时间工具包.zip

    基于java的开发源码-xk-time Java时间工具包.zip 基于java的开发源码-xk-time Java时间工具包.zip 基于java的开发源码-xk-time Java时间工具包.zip 基于java的开发源码-xk-time Java时间工具包.zip 基于java的开发...

    Java 时间日期详细介绍及实例

    Java时间日期处理是编程中常见的任务,特别是在处理与时间相关的逻辑和数据存储时。本文将深入探讨Java中处理时间日期的几个核心类及其用途。 首先,`java.util.Date`是Java标准库中处理时间日期的基本类,它表示自...

    java时间操作大集合

    在上述提供的代码段中,我们可以看到几个关于Java时间处理的关键知识点。这些方法主要用于获取当前时间,格式化时间字符串,以及将字符串转换为日期对象。 1. **日期对象创建与获取**:`java.util.Date` 类是Java中...

    java 时间工具类 java 时间工具类java 时间工具类java 时间工具类

    java 时间工具类 java 时间工具类java 时间工具类java 时间工具类java 时间工具类 java 时间工具类java 时间工具类java 时间工具类java 时间工具类 java 时间工具类java 时间工具类java 时间工具类java 时间工具类 ...

    Java时间转换

    Java时间转换 Java时间转换是指在Java编程语言中将日期和时间从一种格式转换为另一种格式的过程。这种转换对于在不同的应用程序中使用日期和时间非常重要。 在Java中,日期和时间可以使用java.util.Date类和java....

Global site tag (gtag.js) - Google Analytics