Solved

creating dir name with current date and time

Posted on 2004-10-07
7
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jboss wildfly 10.1 10 618
maven module vs maven project 3 116
mysql jsp example issue 32 96
Is there a Google Voice API? 9 74
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…
Suggested Courses

738 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