SimpleDateFormat 中的12小时制与24小时制

您所在的位置:网站首页 simpledateformat方法 SimpleDateFormat 中的12小时制与24小时制

SimpleDateFormat 中的12小时制与24小时制

2024-07-14 16:30:09| 来源: 网络整理| 查看: 265

欲以当前时间作为唯一标识来为生成文件命名,遇到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();



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭