SimpleDateFormat 中的12小时制与24小时制 |
您所在的位置:网站首页 › simpledateformat方法 › SimpleDateFormat 中的12小时制与24小时制 |
欲以当前时间作为唯一标识来为生成文件命名,遇到12小时制的问题,由于存在重复的可能,故查找相关内容。 效果展示如图,实际时间为晚间的八点多,上为24小时制,下为12小时制。 核心即在于为 SimpleDateFormat 指定格式 pattern 时输入的字符。 12小时制:使用 hh 代表小时。 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss"); ... String fileName = "generate_data_" + sdf.format(System.currentTimeMillis()); 24小时制:使用 HH 代表小时。 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); ... String fileName = "generate_data_" + sdf.format(System.currentTimeMillis()) 相关内容上一部分中可以看到, SimpleDateFormat 的简单使用,主要就是一个构造方法和一个 format() 方法。 构造方法摘取最上层的源码展示,有兴趣可以深入查看,大致了解一下时间系列类的逻辑和结构设计。 /** * Constructs a SimpleDateFormat using the given pattern and * the default date format symbols for the default * {@link java.util.Locale.Category#FORMAT FORMAT} locale. * Note: This constructor may not support all locales. * For full coverage, use the factory methods in the {@link DateFormat} * class. *This is equivalent to calling * {@link #SimpleDateFormat(String, Locale) * SimpleDateFormat(pattern, Locale.getDefault(Locale.Category.FORMAT))}. * * @see java.util.Locale#getDefault(java.util.Locale.Category) * @see java.util.Locale.Category#FORMAT * @param pattern the pattern describing the date and time format * @exception NullPointerException if the given pattern is null * @exception IllegalArgumentException if the given pattern is invalid */ public SimpleDateFormat(String pattern) { this(pattern, Locale.getDefault(Locale.Category.FORMAT)); } /** * Constructs a SimpleDateFormat using the given pattern and * the default date format symbols for the given locale. * Note: This constructor may not support all locales. * For full coverage, use the factory methods in the {@link DateFormat} * class. * * @param pattern the pattern describing the date and time format * @param locale the locale whose date format symbols should be used * @exception NullPointerException if the given pattern or locale is null * @exception IllegalArgumentException if the given pattern is invalid */ public SimpleDateFormat(String pattern, Locale locale) { if (pattern == null || locale == null) { throw new NullPointerException(); } initializeCalendar(locale); this.pattern = pattern; this.formatData = DateFormatSymbols.getInstanceRef(locale); this.locale = locale; initialize(locale); } format() 在上一节中传入的 System.currentTimeMillis() 可以直接被解析是因为该方法返回的本身即是时间,摘取部分源码如下,有兴趣深入了解的鼓励自己研究具体源码。 java.text.Format /** * Formats an object to produce a string. This is equivalent to * * {@link #format(Object, StringBuffer, FieldPosition) format}(obj, * new StringBuffer(), new FieldPosition(0)).toString(); * * * @param obj The object to format * @return Formatted string. * @exception IllegalArgumentException if the Format cannot format the given * object */ public final String format (Object obj) { return format(obj, new StringBuffer(), new FieldPosition(0)).toString(); } java.util.Date /** * Allocates a {@code Date} object and initializes it so that * it represents the time at which it was allocated, measured to the * nearest millisecond. * * @see java.lang.System#currentTimeMillis() */ public Date() { this(System.currentTimeMillis()); } java.lang.System /** * Returns the current time in milliseconds. Note that * while the unit of time of the return value is a millisecond, * the granularity of the value depends on the underlying * operating system and may be larger. For example, many * operating systems measure time in units of tens of * milliseconds. * *See the description of the class {@code Date} for * a discussion of slight discrepancies that may arise between * "computer time" and coordinated universal time (UTC). * * @return the difference, measured in milliseconds, between * the current time and midnight, January 1, 1970 UTC. * @see java.util.Date */ @HotSpotIntrinsicCandidate public static native long currentTimeMillis(); |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |