【发布时间】:2012-09-28 16:15:00
【问题描述】:
谁能告诉我 SimpleDateFormat 类中可用的日期格式。
我已经通过 api 但找不到满意的答案。非常感谢任何帮助。
【问题讨论】:
-
你追求什么格式?您必须构建由您需要的元素组成的格式。
-
您可以构建 API 允许的任何格式。没有明确的清单。
标签: java date simpledateformat
谁能告诉我 SimpleDateFormat 类中可用的日期格式。
我已经通过 api 但找不到满意的答案。非常感谢任何帮助。
【问题讨论】:
标签: java date simpledateformat
日期和时间格式如下所述
SimpleDateFormat (Java Platform SE 7) - Date and Time Patterns
可能有n 您可以制作的格式数量。 ex - dd/MM/yyyy 或 YYYY-'W'ww-u 或者您可以混合和匹配字母以实现所需的模式。图案字母如下。
G - 纪元 (AD)y - 年份 (1996; 96)Y - 周年 (2009; 09)M - 一年中的月份(7 月;7 月;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");
【讨论】:
SimpleDateFormat 允许在构造函数中设置语言环境。 SimpleDateFormat formatter = new SimpleDateFormat(pattern, locale);
z 接受“Z” :)
让我把我从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)
}
}
祝你好运!
【讨论】:
更新
其他问题已过时。像SimpleDateFormat 这样可怕的遗留类在几年前就被现代的java.time 类所取代。
为了定义您自己的自定义格式模式,DateTimeFormatter 中的代码与SimpleDateFormat 中的代码相似但不完全相同。一定要研究文档。并在 Stack Overflow 上搜索许多示例。
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… 方法。
使用特定地区(时区)的人们使用的挂钟时间获取当前时刻。
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 2018 年 11 月 5 日 à 19:20:23 heure normale d'Europe centrale
一般来说,自动本地化是一种更好的做法,而不是为硬编码的格式模式而烦恼。
java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.Date、Calendar 和 SimpleDateFormat。
要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310。
Joda-Time 项目现在位于maintenance mode,建议迁移到java.time 类。
您可以直接与您的数据库交换 java.time 对象。使用符合JDBC 4.2 或更高版本的JDBC driver。不需要字符串,不需要java.sql.* 类。 Hibernate 5 & JPA 2.2 支持 java.time。
从哪里获取 java.time 类?
【讨论】:
在此处查看格式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;
}
【讨论】: