SimpleDateFormat 类中有哪些可用的日期格式?
2022-08-31 10:48:35
任何人都可以让我知道SimpleDateFormat类中可用的日期格式。
我已经通过api,但找不到满意的答案。任何帮助都非常感谢。
任何人都可以让我知道SimpleDateFormat类中可用的日期格式。
我已经通过api,但找不到满意的答案。任何帮助都非常感谢。
日期和时间格式在下面进行了详细描述
SimpleDateFormat (Java Platform SE 7) - 日期和时间模式
您可以制作的格式数量可能很多。ex - 或者您可以混合和匹配字母以实现所需的模式。图案字母如下。ndd/MM/yyyyYYYY-'W'ww-u
G- 时代标志(公元)y- 年份 (1996; 96)Y- 周年 (2009; 09)M- 一年中的月份(七月;七月;27 千米赛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;格林尼治标准时间-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");
让我抛出一些我从 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)
}
}
祝你好运!