在java中将当前时间戳作为文件夹名称
在java中,我们可以以文件名的形式给出当前时间,我们也可以对文件夹这样做吗? 我们可以将文件夹的名称作为当前时间戳吗?在java中将当前时间戳作为文件夹名称,java,directory,Java,Directory,在java中,我们可以以文件名的形式给出当前时间,我们也可以对文件夹这样做吗? 我们可以将文件夹的名称作为当前时间戳吗? 请帮忙。谢谢。是的,像这样的东西 Date date =new Date(); String s=""+date.getTime(); File file = new File("rootpath"+s); file.mkdir(); Date now = new Date();
请帮忙。谢谢。是的,像这样的东西
Date date =new Date();
String s=""+date.getTime();
File file = new File("rootpath"+s);
file.mkdir();
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("hh mm ss");
String time = dateFormat.format(now);
File dir = new File(time);
dir.mkdir();
如果您使用的是JodaTime,则可以通过以下方式完成:
DateTime date = DateTime.now();
File f = new File("C:\\tmp\\"+ date.getMillis());
f.mkdir();
您会得到一个名为1418210024492
(基于我运行它的时间)的文件夹
如果希望时间戳作为日期,则可以执行以下操作:
File f=新文件(“C:\\tmp\\”+日期)代码>
日期也可以按照您的意愿进行格式化,如下所示:
String dateTime = new DateTime().toString("dd-MM-yy HH:mm:ss");
File f = new File("C:\\tmp\\" + dateTime);
f.mkdir();
我更喜欢使用,因为它更容易实现日期和时间。Joda Time
与by类似,我建议使用JodaTime2.6(或java.Time)来处理java中的所有日期时间工作
ISO 8601
我建议使用标准格式,2014-12-10T17:05:33Z
。字母排序碰巧也是按时间顺序排序的。另一个好处是几乎在所有文化中都能清晰地阅读
替换冒号
除了替换冒号以与Mac兼容之外。我看到它们被连字符-
或句号
(句号)替换
结果值2014-12-10T17-05-33Z
与我所知的除MS-DOS以外的所有常见操作系统兼容(字符太多)
为了与Unix风格的操作系统和Microsoft Windows操作系统兼容,请不要替换为SOLIDUS(斜杠)或反向SOLIDUS(反斜杠)
更多信息,请阅读苹果公司的这篇文章
时区
最好指定所需的时区,而不是隐式地依赖JVM的当前默认时区
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();
DateTime now = DateTime.now( DateTimeZone.getDefault() );
…
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();
DateTime now = DateTime.now( DateTimeZone.getDefault() );
…
输出:
输出:2014-12-10T22-35-28.460Z
默认时区
如果要使用用户的JVM的当前默认时区
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();
DateTime now = DateTime.now( DateTimeZone.getDefault() );
…
输出:2014-12-10T14-49-00.752-08-00
特定时区
也许你想要一个特定的时区,比如公司总部的时区
DateTime now = DateTime.now( DateTimeZone.forID( "America/Montreal" ) );
分秒
您可能希望舍弃分数秒以使用整秒或整分钟。Joda Time有内置的格式化程序,或。这些格式省略了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();
运行时:
输出:2014-12-10T23-07-11Z
没有标点符号
另一种选择是不使用标点符号,例如20141211T214342Z
这种格式甚至被ISO 8601视为标准格式,使用最少数量的分隔符的格式被正式称为“基本”
DateTime now = DateTime.now( DateTimeZone.UTC );
DateTimeFormatter formatter = ISODateTimeFormat.basicDateTimeNoMillis();
String output = formatter.print( now );
File f = new File( output );
f.mkdir();
谢谢大家。学到了新东西。再次感谢。