2011-03-09 14 views
26

私はあなたが私を助けてくれることを願っています。私は別のクラスの日付を呼びかけようとしていて、 "2011-03-09 06-57-40"のように見えます。以下のファイルを作成しますが、出力が実行されるたびにdat()を呼び出して新しいファイルが作成されます。私は何がうまくいかないことを知っています。私はそれを修正する方法がわかりません、私は同じファイルに執筆したいと思います。私はこれが意味をなさないと思いますか? :/日付と時刻を使用してファイル名を作成する

は、事前に任意の助けをありがとう:)

date d = new date(); 
    String cdate = d.date(); 


    String f = h; 

    try{ 
     PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(cdate + ".tsv", true))); 
     out.print(f); 
     out.print("\t"); 
     out.close(); 
    }catch (IOException e){ 
    } 
+0

ファイルで何をしたいのか明確にできますか?私は2つのアイデアを持っています:あなたが使用した日付/ファイル名を保管してください。あるいは、ログファイルやそれに類するもののために十分な時間ではなく、日付だけを使うこともできます。 btwはこのJavaですか?おそらくタグとしてプログラミングのlanuageを追加します。 – Yashima

+0

こんにちは申し訳ありませんが、これはJavaを使用しており、今日の日付と時刻という名前のファイルを作成したいので、常にユニークで、データを書きたいと思います。それは役に立ちますか?どうもありがとう。 – Tom

+0

@ジェームズ・ナウ、それは..あなたは質問でもっと詳しく教えていただけますか? –

答えて

9

私がしようとすると、すべて同じにお答えします。使用の可能性が最も制御された方法で、次のコード

Calendar cal = Calendar.getInstance(); 
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
String dateStr = dateFormat.format(cal.getTime()); 

を日付や時刻の文字列を取得するには、http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.htmlを検索します。理解に役立つかもしれません。時/分または必要なものをフォーマットされたStringに追加することもできます。

また、カレンダーのミリ秒、秒、分などの「下限」フィールドを常にゼロに設定することもできます。

cal.set(Calendar.MINUTE,0); 

あなたが別のクラスからあなたの日付を取得しているし、直接(注意:ちょうどあなたがカレンダーを必要としないフォーマットするために)あなたにもカレンダーに日付を入れることができますカレンダーを作成できない場合

cal.setTime(date); 

これは、作成されたファイル名/ファイルをより詳細に制御するのに役立ちます。現在の日付/時刻をという名前のファイルを作成するには

33

Date date = new Date() ; 
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss") ; 
File file = new File(dateFormat.format(date) + ".tsv") ; 
BufferedWriter out = new BufferedWriter(new FileWriter(file)); 
out.write("Writing to file"); 
out.close(); 
+0

あなたが考慮する必要があるのは、ループ内に複数のファイルを書き込むと、時間が等しくなるため、おそらくいくつかのファイルを上書きするということです。 – Khan

3

それはちょうど少しより効率的になります - 唯一のSimpleDateFormatとDateオブジェクトの各ファイルに対してだけでなく、無文字列の連結。

private final static String getDateTime() 
{ 
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd_hh:mm:ss"); 
    df.setTimeZone(TimeZone.getTimeZone("PST")); 
    return df.format(new Date()); 
} 
16

これはおそらくはるかに簡単です。コードの1行だけが日付と時刻としてファイルの名前を割り当てます。

String out = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss'.tsv'").format(new Date()); 
1
public class BELogs { 

    private final static Logger logger = Logger.getLogger(BSELogs.class 
      .getName()); 

      boolean makeDir = false; 
      Date date = new Date(); 
      SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy") ; 
      String curDate =dateFormat.format(date); 
      FileHandler fh;  

       public BSELogs() { 

     try { 

      File file = new File("/home//Desktop/Logs "+curDate); 
      makeDir = file.mkdir(); 
      fh = new FileHandler(file+"/MyLogFile.log "+curDate,true); 
      logger.addHandler(fh); 
      // Set the logger level to produce logs at this level and above. 
      logger.setLevel(Level.FINE); 
      SimpleFormatter formatter = new SimpleFormatter(); 
      fh.setFormatter(formatter); 

      } catch (SecurityException ex) { **strong text** 
      ex.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

    logger.info("Data Log Genrated............"); 

     } 
}    
2

それは、毎回の実行に新しいファイルを作成しています。この

public class TimeBasedFile { 
    public static void main(String[] args) { 


    Date date = new Date() ; 
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss") ; 
    File file = new File("D:\\datebasedFile\\"+dateFormat.format(date) + ".tsv") ; 

    try(BufferedWriter out = new BufferedWriter(new FileWriter(file))) { 
     out.write("Writing to file"); 
     // code for what data you want to store 
     System.out.println("the file is created successfully"); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

} 
} 

で試してみてください。

関連する問題