Link to home
Start Free TrialLog in
Avatar of dkim18
dkim18

asked on

creating dir name with current date and time

hi!

i am trying to create dir and its name with current date and time that the user excute the application.
ex)if a user run application on 10.6.2004 at 06:23 pm
then file name shold be something like:
 filename_10_06_2004_18_23

thanks,
Avatar of lhankins
lhankins
Flag of United States of America image

One <very> simple way to do this is just to use the System.currentTimeMillis() method - this method returns the number of milliseconds since 1972... you could put this on the end of the filename.   This will only be accurate to the millisecond (so if you have > 1 file generated per ms, you may have a problem).

If you really want to use the date scheme you outlined above, you can do this with the SimpleDateFormat class, for example :

         SimpleDateFormat sdf = new SimpleDateFormat("MM_dd_yyyy_HH_mm_ss");
         System.out.println("date = [" + sdf.format(new Date()) + "]");

The output would be :

         date = [10_07_2004_17_54_35]

But beware - this strategy is only unique to the second... you could modify the above example to include milliseconds to, in the following way:

      SimpleDateFormat sdf = new SimpleDateFormat("MM_dd_yyyy_HH_mm_ss_SSS");
      System.out.println("date = [" + sdf.format(new Date()) + "]");

Then you'll end up with :

      date = [10_07_2004_17_57_04_328]

Avatar of ThummalaRaghuveer
ThummalaRaghuveer

This will do the job..

import java.io.File;
import java.util.Date;


public class temp {
      public static void main(String args[]) {
            Date dt = new Date();
            String t = "file_"+dt.getDate()+"_"+dt.getMonth()+"_"+dt.getYear()+"_"+dt.getHours()+"_"+dt.getMinutes();
            File tempFile=new File(t);
           if(tempFile.mkdir()) {
                 System.out.println("Directory Created");
           }
            
            
      }

}

:-)
SOLUTION
Avatar of lhankins
lhankins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yaa it is I just kept it like that........

If both above codes are mixed its done right.....

Anyway sorry for the depricated method answer
:-)
Avatar of dkim18

ASKER


i applied the following to my program...

String t = "file_"+dt.getDate()+"_"+dt.getMonth()+"_"+dt.getYear()+"_"+dt.getHours()+"_"+dt.getMinutes();

and got the following..
file_8_9_104_0_7

month and year seem to be wrong.

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial