Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

creating dir name with current date and time

Posted on 2004-10-07
7
Medium Priority
?
251 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 100 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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 200 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 200 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

722 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