Reputation: 451
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.
Upvotes: 2
Views: 24221
Reputation: 340350
Similar to the answer by James Fox, I suggest using Joda-Time 2.6 (or java.time) for all your date-time work in Java.
I would suggest using the standard ISO 8601 format, 2014-12-10T17:05:33Z
. The alphabetical sort happens to also be chronologically sorted. Another benefit is unambiguous reading across nearly all cultures.
Except replace COLON for compatibility with the Mac’s HFS+ file system. I’ve seen them replaced with HYPHEN -
or FULL STOP .
(period).
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).
Do not replace with SOLIDUS (slash) nor REVERSE SOLIDUS (backslash), for compatibility with Unix-style OSes and Microsoft Windows OSes.
For more info, read this article by Apple, OS X: Cross-platform filename best practices and conventions.
Better to specify your desired time zone rather than implicitly relying on the JVM’s current default time zone.
If mixing and matching files across computers, you may want to stick with UTC as the time zone.
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
If you want to use the user’s JVM’s current default time zone.
DateTime now = DateTime.now( DateTimeZone.getDefault() );
…
output : 2014-12-10T14-49-00.752-08-00
Perhaps you want a specific time zone, such as that of the company’s headquarters.
DateTime now = DateTime.now( DateTimeZone.forID( "America/Montreal" ) );
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.
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
Another alternative is using no punctuation characters, such as 20141211T214342Z
.
Such formats is even considered standard by ISO 8601, with the formats using a minimal number of separators officially called “basic”.
DateTime now = DateTime.now( DateTimeZone.UTC );
DateTimeFormatter formatter = ISODateTimeFormat.basicDateTimeNoMillis();
String output = formatter.print( now );
File f = new File( output );
f.mkdir();
Upvotes: 6
Reputation: 691
If you are using JodaTime then it can be done this way:
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).
If you want the timestamp as a date then you can do:
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 JodaTime as it is a much easier implementation of Date and Time.
Upvotes: 2
Reputation: 1995
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();
Upvotes: 8
Reputation: 771
Date date =new Date();
String s=""+date.getTime();
File file = new File("rootpath"+s);
file.mkdir();
Upvotes: 0