Solved

creating dir name with current date and time

Posted on 2004-10-07
7
242 Views
Last Modified: 2010-05-18
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,
0
Comment
Question by:dkim18
7 Comments
 
LVL 7

Expert Comment

by:lhankins
ID: 12254450
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]

0
 
LVL 2

Expert Comment

by:ThummalaRaghuveer
ID: 12254524
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");
           }
            
            
      }

}

:-)
0
 
LVL 7

Assisted Solution

by:lhankins
lhankins earned 25 total points
ID: 12254848

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

That's 5 deprecated method calls in one line...  

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Expert Comment

by:ThummalaRaghuveer
ID: 12255468
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
:-)
0
 

Author Comment

by:dkim18
ID: 12255984

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.

0
 
LVL 5

Assisted Solution

by:Naeemg
Naeemg earned 50 total points
ID: 12256253
Let me correct it.

    java.util.Date dt = new java.util.Date();
    String myFileName = "myFile";
    String t = null;
    try
    {
      t = myFileName + "_" + dt.getDate() + "_" + dt.getMonth() + "_" + dt.getYear() + "_" + dt.getHours() + "_" + dt.getMinutes();
    }
    catch(Exception ex)
    {
    }
    File tempFile = new File(t);
    if(tempFile.mkdir())
    {
      System.out.println("Directory Created" + tempFile.getName());
    }



Naeem Shehzad Ghuman
0
 
LVL 2

Accepted Solution

by:
ThummalaRaghuveer earned 50 total points
ID: 12256307
Yaa I don`t know the reason but I too am getting the same problem...
Last time I tested it it work..... It does not have any depricated methods in it......


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



public class temp {
     public static void main(String args[]) {
         
          SimpleDateFormat sdf = new SimpleDateFormat("MM_dd_yy_HH_mm");
          String t = "file_"+sdf.format(new Date());
          File tempFile=new File(t);
          if(tempFile.mkdir()) {
               System.out.println("Directory Created");
          }
         
         
     }

}


Hope this works with out failing :-)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now