get Name Of File from current date - Java java.io

Java examples for java.io:File Name

Description

get Name Of File from current date

Demo Code


//package com.java2s;

import java.io.File;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Main {
    public static void main(String[] argv) throws Exception {
        String mediaPath = "java2s.com";
        System.out.println(getNameOfFile(mediaPath));
    }/*from   w w w. j a v a2 s  . c o m*/

    public static String getNameOfFile(String mediaPath) {

        File directory = new File(mediaPath + year() + "/" + monthDay()
                + "/");

        if (!directory.exists()) {
            directory.mkdirs();
        }

        return mediaPath + year() + "/" + monthDay() + "/"
                + generateUniqueFileName("upload-", ".bin");

    }

    public static String year() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        return sdf.format(cal.getTime());

    }

    public static String monthDay() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("MMdd");
        return sdf.format(cal.getTime());

    }

    public static String generateUniqueFileName(String prefix, String suffix) {
        return (prefix != null ? prefix : "") + System.nanoTime()
                + (suffix != null ? suffix : "");
    }
}

Related Tutorials