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

Java SimpleDateFormat类中可用的日期格式是什么?,java,date,simpledateformat,Java,Date,Simpledateformat,有人能告诉我SimpleDataFormat类中可用的日期格式吗 我已经浏览了api,但没有找到满意的答案。非常感谢您的帮助。日期和时间格式如下所述 您可以制作的格式可能有很多种。ex-dd/MM/yyyy或yyyy-'W'ww-u或者您可以混合和匹配字母以实现所需的模式。图案字母如下 G-纪元指示符(AD) 年(1996年;1996年) Y周年(2009;09) M-一年中的月份(7月;7月;7月) w-一年中的一周(27) W-月中的周数(2) D-一年中的天数(189) d-月内的天(

有人能告诉我SimpleDataFormat类中可用的日期格式吗


我已经浏览了api,但没有找到满意的答案。非常感谢您的帮助。

日期和时间格式如下所述

您可以制作的格式可能有很多种。ex-
dd/MM/yyyy
yyyy-'W'ww-u
或者您可以混合和匹配字母以实现所需的模式。图案字母如下

  • G
    -纪元指示符(AD)
  • 年(1996年;1996年)
  • Y
    周年(2009;09)
  • M
    -一年中的月份(7月;7月;7月)
  • w
    -一年中的一周(27)
  • W
    -月中的周数(2)
  • D
    -一年中的天数(189)
  • d
    -月内的天(10)
  • F
    -月份中的星期几(2)
  • E
    -一周中的日期名称(星期二;星期二)
  • u
    -一周的天数(1=周一,…,7=周日)
  • a
    -AM/PM标记
  • H
    一天中的小时数(0-23)
  • k
    -一天中的一小时(1-24小时)
  • K
    -上午/下午的小时数(0-11)
  • h
    -上午/下午的小时数(1-12)
  • m
    -以小时为单位的分钟(30)
  • s
    -秒(55分钟)
  • S
    -毫秒(978)
  • z
    -一般时区(太平洋标准时间;太平洋标准时间;格林尼治标准时间-08:00)
  • Z
    -RFC 822时区(-0800)
  • X
    -ISO 8601时区(-08;-0800;-08:00)
要分析:

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

使用:
newsimpledateformat(“yyyy-MM-dd'T'HH:MM:ss.SSSZ”)

让我抛出一些示例代码,然后您可以使用不同的选项,直到您理解它

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)

   }
}

祝你好运

检查此处的格式

main

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;
            }
java.time 更新

其他问题已经过时了。可怕的遗留类,如
SimpleDateFormat
,几年前被现代java.time类所取代

习俗 为了定义自己的自定义格式模式,中的代码与
SimpleDateFormat
中的代码类似,但并不完全相同。一定要研究文档。和搜索堆栈溢出的许多例子

DateTimeFormatter f = 
    DateTimeFormatter.ofPattern( 
        "dd MMM uuuu" , 
        Locale.ITALY 
    ) 
;
标准ISO 8601 该标准定义了许多类型的日期时间值的格式。这些格式是为数据交换而设计的,很容易被机器解析,也很容易被跨文化的人类读取

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

Instant.now().toString()
2018-11-05T18:19:33.017554Z

对于UTC值,末尾的
Z
表示UTC,发音为“Zulu”

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

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

生成标准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 );
产出:伦迪2018年11月5日19:20:23欧洲中部高等师范学院

通常,自动本地化是一种更好的做法,而不是使用硬编码的格式模式


关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,&

要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是

该项目现已启动,建议迁移到类

您可以直接与数据库交换java.time对象。使用兼容的或更高版本。不需要字符串,也不需要
java.sql.*
类。Hibernate5和JPA2.2支持java.time

从哪里获得java.time类

  • 、和更高版本-标准Java API的一部分,带有捆绑实现。
    • 带来了一些小功能和修复
    • 大多数java.time功能都在中向后移植到Java6和Java7
    • Android(26+)的更高版本捆绑了java.time类的实现

    • 对于早期的Android(你想要什么格式?你必须构建一个由你需要的元素组成的格式。你可以构建API允许的任何格式。没有确定的列表。谢谢你。但是你能告诉我如何将日期转换成不同的标准,如EST、IST、gdt、gmt等吗?然后你要在不同的语言环境中查找日期。
      SimpleDateFormat
      允许在构造函数中设置区域设置。
      SimpleDateFormat格式化程序=新的SimpleDateFormat(模式,区域设置);
      好的,这是我一直在寻找的东西..谢谢你..:)我很感激你的开放性。:)不要混淆Z和“Z”。我很傻,我希望其他人不会像我一样。
      Locale locale = Locale.CANADA_FRENCH;
      DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( locale );
      
      String output = zdt.format( f );