Solved

creating dir name with current date and time

Posted on 2004-10-07
7
246 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to parse custom JSON to POJO java 4 74
split53 challenge 7 98
varialbe initialization 11 33
Html Table Looping (part 2) 5 10
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.

823 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