Solved

creating dir name with current date and time

Posted on 2004-10-07
7
250 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

623 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