Question: I am developing an app that loops for several hours or even maybe days and what I want is that it loops past the current day into the next it creates a new file with a new file name.
Can anyone help me with the logic ?
Here is my code:

```
    timedata= new File(timedata)
    String timedatafile=  "/sdcard/timedata"+"-"+formattedDate+".csv";

    if(MsureData.exists()!=true)
{
    timedata.createNewFile();
    Calendar c = Calendar.getInstance();
    String formattedDate = df.format(c.getTime()); 
    // do stuff
}

```

Answer: 
Every time you write measure data to the csv file calculate the current filename and use that.

Assuming you have some kind of service which is called every now and then by a timer or similar, the entry point for that is `onSomeEvent` which figures out which file to use and then does something to it.

```
class Writer extends SomeService {
    File current = null;
    DateFormat df = ...;

    public void onSomeEvent() {
        determineCurrentFile();
        // you can be sure that this file exists and it may or may not contain data already,
        // but for sure it is for the current date (today)
        AnotherClass.doSomethingWithMyFile(current);
    }

    private void determineCurrentFile() {
        File newFile = new File(getFileName());
        if(current == null) { // first usage
            current = newFile;
        } else if(!this.current.equals(newFile)) {
            current = newFile; // new day (doesn't matter how many days passed)
        } else {
            // same day, don't touch current
        }
        ensureFileExists(); // not sure if needed, I would let whoever writing it do the creation
    }

    private String getFileName() {
        Calendar c = Calendar.getInstance();
        String formattedDate = df.format(c.getTime());
        return formattedDate;
    }

    private void ensureFileExists() {
        if(current == null) {
            throw new IllegalStateException("determineFile should have filled this.current");
        }
        if(!current.exists()) {
            current.createNewFile();
        }
    }
}

```
```
Date currentDate = new Date(System.currentTimeMillis());

```

From this you can get current date of system.