任何人都可以告诉我SimpleDateFormat类中可用的日期格式。
我已经通过api但找不到满意的答案。非常感谢任何帮助。
日期和时间格式在下面有详细描述
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
- 一般时区(太平洋标准时间;太平洋标准时间;格林尼治标准时间-08:00)Z
- RFC 822时区(-0800)X
- ISO 8601时区(-08; -0800; -08:00)解析:
2000-01-23t 04:Line:07.000 + 0000
使用:new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
让我抛出一些我从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)
}
}
祝好运!
检查这里的格式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;
}
UPDATE
其他问题已经过时了。几年前,现代java.time类取代了像SimpleDateFormat
这样可怕的遗留类。
为了定义自己的自定义格式模式,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 );
输出:2018年11月5日星期一19:20:23中欧标准时间
通常,更好的做法是使用硬编码格式化模式自动本地化而不是烦恼。
java.time框架内置于Java 8及更高版本中。这些类取代了麻烦的旧legacy日期时间类,如java.util.Date
,Calendar
和SimpleDateFormat
。
现在在Joda-Time的maintenance 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的试验场。你可能会在这里找到一些有用的类,如Interval
,YearWeek
,YearQuarter
和more。