SimpleDateFormat类中可用的日期格式是什么?

时间:2012-10-08 11:57:27

标签: java date simpledateformat

有人可以告诉我SimpleDateFormat类中可用的日期格式。

我已经通过api但找不到满意的答案。非常感谢任何帮助。

4 个答案:

答案 0 :(得分:97)

日期和时间格式在下面有详细描述

SimpleDateFormat (Java Platform SE 7) - Date and Time Patterns

可能有n种格式。前 - dd/MM/yyyyYYYY-'W'ww-u或者您可以混合和匹配字母以达到您所需的模式。模式字母如下。

  • G - 时代指示符(AD)
  • y - 年(1996; 96)
  • Y - 周年(2009年; 09年)
  • M - 一年一月(七月;七月; 07)
  • w - 一年中的一周(27)
  • W - 每月一周(2)
  • D - 一年中的一天(189)
  • d - 每月(10)
  • F - 每月的某一天(2)
  • E - 星期的名字(星期二;星期二)
  • u - 星期数(1 =星期一,...,7 =星期日)
  • a - 上午/下午标记
  • H - 一天中的小时(0-23)
  • k - 一天中的小时(1-24)
  • K - 上午/下午(0-11)
  • h - 上午/下午(1-12)
  • m - 小时(30)
  • s - 秒钟(55)
  • S - 毫秒(978)
  • z - 一般时区(太平洋标准时间; PST; GMT-08:00)
  • Z - RFC 822时区(-0800)
  • X - ISO 8601时区(-08; -0800; -08:00)

解析:

  

2000-01-23T04:56:07.000 + 0000

使用: new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

答案 1 :(得分:50)

让我抛出一些我从http://www3.ntu.edu.sg/home/ehchua/programming/java/DateTimeCalendar.html获得的示例代码然后你可以使用不同的选项,直到你理解它为止。

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

public class DateTest {
   public static void main(String[] args) {
       Date now = new Date();

       //This is just Date's toString method and doesn't involve SimpleDateFormat
       System.out.println("toString(): " + now);  // dow mon dd hh:mm:ss zzz yyyy
       //Shows  "Mon Oct 08 08:17:06 EDT 2012"

       SimpleDateFormat dateFormatter = new SimpleDateFormat("E, y-M-d 'at' h:m:s a z");
       System.out.println("Format 1:   " + dateFormatter.format(now));
       // Shows  "Mon, 2012-10-8 at 8:17:6 AM EDT"

       dateFormatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
       System.out.println("Format 2:   " + dateFormatter.format(now));
       // Shows  "Mon 2012.10.08 at 08:17:06 AM EDT"

       dateFormatter = new SimpleDateFormat("EEEE, MMMM d, yyyy");
       System.out.println("Format 3:   " + dateFormatter.format(now));
       // Shows  "Monday, October 8, 2012"

       // SimpleDateFormat can be used to control the date/time display format:
       //   E (day of week): 3E or fewer (in text xxx), >3E (in full text)
       //   M (month): M (in number), MM (in number with leading zero)
       //              3M: (in text xxx), >3M: (in full text full)
       //   h (hour): h, hh (with leading zero)
       //   m (minute)
       //   s (second)
       //   a (AM/PM)
       //   H (hour in 0 to 23)
       //   z (time zone)
       //  (there may be more listed under the API - I didn't check)

   }

}

祝你好运!

答案 2 :(得分:5)

检查格式http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

主要

System.out.println("date  : " + new classname().getMyDate("2014-01-09 14:06", "dd-MMM-yyyy E hh:mm a z", "yyyy-MM-dd HH:mm"));

方式

 public String getMyDate(String myDate, String returnFormat, String myFormat)
            {
                DateFormat dateFormat = new SimpleDateFormat(returnFormat);
                Date date=null;
                String returnValue="";
                try {
                    date = new SimpleDateFormat(myFormat, Locale.ENGLISH).parse(myDate);
                    returnValue = dateFormat.format(date);
                } catch (ParseException e) {
                    returnValue= myDate;
                    System.out.println("failed");
                    e.printStackTrace();
                }

            return returnValue;
            }

答案 3 :(得分:2)

java.time

更新

其他问题已过时。可怕的传统类,例如SimpleDateFormat在多年前被现代的 java.time 类所取代。

自定义

要定义自己的自定义格式设置模式,DateTimeFormatter中的代码与SimpleDateFormat中的代码相似但不完全相同。请务必阅读文档。并搜索“堆栈溢出”中的许多示例。

DateTimeFormatter f = 
    DateTimeFormatter.ofPattern( 
        "dd MMM uuuu" , 
        Locale.ITALY 
    ) 
;

标准ISO 8601

ISO 8601标准定义了许多类型的日期时间值的格式。这些格式是专为数据交换而设计的,可以通过机器轻松解析,也可以被跨文化的人类轻松阅读。

java.time 类在生成/解析字符串时默认使用ISO 8601格式。只需调用toStringparse方法。无需指定格式模式。

Instant.now().toString()
  

2018-11-05T18:19:33.017554Z

对于UTC值,最后的Z表示UTC,并发音为“ Zulu”。

本地化

您可以让 java.time 为您自动本地化,而不是指定格式模式。使用DateTimeFormatter.ofLocalized…方法。

获取当前时刻以及特定地区(时区)人们使用的挂钟时间。

ZoneId z = ZoneId.of( "Africa/Tunis" );
ZonedDateTime zdt = ZonedDateTime.now( z );

明智地扩展标准ISO 8601格式的文本,以将时区的名称添加在方括号中。

  

zdt.toString():2018-11-05T19:20:23.765293 + 01:00 [非洲/突尼斯]

生成自动本地化的文本。

Locale locale = Locale.CANADA_FRENCH;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( locale );

String output = zdt.format( f );
  

输出:lundi 5 novembre 2018à19:20:23 heure normale d’Europe centrale

通常,更好的做法是使用硬编码格式模式来自动定位而不是烦恼。


关于 java.time

java.time框架已内置在Java 8及更高版本中。这些类取代了麻烦的旧legacy日期时间类,例如java.util.DateCalendarSimpleDateFormat

目前位于Joda-Timemaintenance mode项目建议迁移到java.time类。

要了解更多信息,请参见Oracle Tutorial。并在Stack Overflow中搜索许多示例和说明。规格为JSR 310

您可以直接与数据库交换 java.time 对象。使用符合JDBC driver或更高版本的JDBC 4.2。不需要字符串,不需要java.sql.*类。

在哪里获取java.time类?

ThreeTen-Extra项目使用其他类扩展了java.time。该项目为将来可能在java.time中添加内容提供了一个试验场。您可能会在这里找到一些有用的类,例如IntervalYearWeekYearQuartermore