java 在java中将当前时间戳作为文件夹名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27399419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Giving current timestamp as folder name in java
提问by abhay sachu
As we can give the current time as file name in java , can we do the same with folders too? Can we give the name of the folder as current time stamp? please help.Thank You.
因为我们可以在 java 中给出当前时间作为文件名,我们也可以对文件夹做同样的事情吗?我们可以将文件夹的名称作为当前时间戳吗?请帮忙。谢谢。
回答by Frozendragon
yes something like this.
是这样的。
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("hh mm ss");
String time = dateFormat.format(now);
File dir = new File(time);
dir.mkdir();
回答by Basil Bourque
Joda-Time
乔达时间
Similar to the answerby James Fox, I suggest using Joda-Time 2.6 (or java.time) for all your date-time work in Java.
类似的答案由詹姆斯·福克斯,我建议使用乔达时间2.6(或java.time)在Java中所有的日期时间的工作。
ISO 8601
ISO 8601
I would suggest using the standard ISO 8601format, 2014-12-10T17:05:33Z
. The alphabetical sort happens to also be chronologically sorted. Another benefit is unambiguous reading across nearly all cultures.
我建议使用标准的ISO 8601格式,2014-12-10T17:05:33Z
. 字母排序恰好也是按时间顺序排序的。另一个好处是在几乎所有文化中都能清楚地阅读。
Replace Colons
替换冒号
Except replace COLON for compatibility with the Mac's HFS+ file system. I've seen them replaced with HYPHEN -
or FULL STOP .
(period).
除了替换 COLON 以兼容 Mac 的HFS+ 文件系统。我已经看到它们被 HYPHEN-
或 FULL STOP .
(句点)取代。
The resulting value 2014-12-10T17-05-33Z
is compatible with every common OS I know of, other than MS-DOS (too many characters for 8.3 naming).
结果值2014-12-10T17-05-33Z
与我所知道的每个常见操作系统兼容,除了 MS-DOS(8.3 命名的字符太多)。
Do not replace with SOLIDUS (slash) nor REVERSE SOLIDUS (backslash), for compatibility with Unix-style OSes and Microsoft Windows OSes.
不要用 SOLIDUS(斜杠)或 REVERSE SOLIDUS(反斜杠)替换,以便与 Unix 风格的操作系统和 Microsoft Windows 操作系统兼容。
For more info, read this article by Apple, OS X: Cross-platform filename best practices and conventions.
有关更多信息,请阅读 Apple 撰写的这篇文章,OS X:跨平台文件名最佳实践和约定。
Time Zone
时区
Better to specify your desired time zone rather than implicitly relying on the JVM's current default time zone.
最好指定所需的时区,而不是隐式依赖 JVM 的当前默认时区。
UTC
世界标准时间
If mixing and matching files across computers, you may want to stick with UTCas the time zone.
如果跨计算机混合和匹配文件,您可能希望坚持使用UTC作为时区。
DateTime now = DateTime.now( DateTimeZone.UTC );
String output = now.toString().replace( ":" , "-" ); // Replace colons for compatibility with the Mac HFS+ file system.
File f = new File( output );
f.mkdir();
Output:
输出:
output : 2014-12-10T22-35-28.460Z
Default Time Zone
默认时区
If you want to use the user's JVM's current default time zone.
如果要使用用户的 JVM 当前默认时区。
DateTime now = DateTime.now( DateTimeZone.getDefault() );
…
output : 2014-12-10T14-49-00.752-08-00
Specific Time Zone
特定时区
Perhaps you want a specific time zone, such as that of the company's headquarters.
也许您想要一个特定的时区,例如公司总部的时区。
DateTime now = DateTime.now( DateTimeZone.forID( "America/Montreal" ) );
To The Minute Or Second
分秒必争
You may want to drop the fractional second to use a whole second or whole minute. Joda-Time has built-in formatters, dateHourMinuteSecond()
or dateHourMinute()
. Those formats omit the Z
or time zone offset. I suggest appending for clarity; notice the +"Z"
below.
您可能希望删除小数秒以使用整秒或整分钟。Joda-Time 有内置的格式化程序,dateHourMinuteSecond()
或者dateHourMinute()
. 这些格式省略了Z
或 时区偏移量。为了清楚起见,我建议附加;请注意+"Z"
以下内容。
DateTime now = DateTime.now( DateTimeZone.UTC );
DateTimeFormatter formatter = ISODateTimeFormat.dateHourMinuteSecond(); // Or dateHourMinute();
String output = formatter.print( now ).replace( ":" , "-" )+"Z"; // Replace colons for compatibility with the Mac HFS+ file system.
File f = new File( output );
f.mkdir();
When run:
运行时:
output : 2014-12-10T23-07-11Z
Without Punctuation
无标点符号
Another alternative is using no punctuation characters, such as 20141211T214342Z
.
另一种选择是不使用标点符号,例如20141211T214342Z
.
Such formats is even considered standard by ISO 8601, with the formats using a minimal number of separators officially called “basic”.
此类格式甚至被 ISO 8601 视为标准,这些格式使用最少数量的分隔符,正式称为“基本”。
DateTime now = DateTime.now( DateTimeZone.UTC );
DateTimeFormatter formatter = ISODateTimeFormat.basicDateTimeNoMillis();
String output = formatter.print( now );
File f = new File( output );
f.mkdir();
回答by James Fox
If you are using JodaTime then it can be done this way:
如果您使用的是 JodaTime,则可以通过以下方式完成:
DateTime date = DateTime.now();
File f = new File("C:\tmp\"+ date.getMillis());
f.mkdir();
You get a folder called 1418210024492
(based on the time I ran it).
您会得到一个名为1418210024492
(基于我运行它的时间)的文件夹。
If you want the timestamp as a date then you can do:
如果您想要时间戳作为日期,那么您可以执行以下操作:
File f = new File("C:\\tmp\\" + date);
File f = new File("C:\\tmp\\" + date);
The date can also be formatted how you wish, like below:
日期也可以按照您的意愿进行格式化,如下所示:
String dateTime = new DateTime().toString("dd-MM-yy HH:mm:ss");
File f = new File("C:\tmp\" + dateTime);
f.mkdir();
I prefer to use JodaTimeas it is a much easier implementation of Date and Time.
我更喜欢使用JodaTime,因为它更容易实现日期和时间。
回答by Arun Xavier
Date date =new Date();
String s=""+date.getTime();
File file = new File("rootpath"+s);
file.mkdir();