Mvkk
Mvkk

Reputation: 3

java - SimpleDateFormat does not update time

I was trying to make a class (DateTime) where i gather all the method related to formating time, waiting and such. When I'm trying to use SimpleDateFormat it keep returning (init) time, instead of new time everytime i call the methods.

public class DateTime {
    static String logtimeformat = DateTime.formatCurrentDate("HH:mm:ss  ");
    static String filenametimeformat = DateTime.formatCurrentDate("dd-MM-yyyy_HH-mm-ss");
    static String picturenameformat = DateTime.formatCurrentDate("dd-MM-yyyy_HH-mm-ss-SSS");

    public static String formatLogTime() {
        String formated = DateTime.formatCurrentDate(logtimeformat);
        return formated;
    }

    public static String formatPicName() {
        String formated = DateTime.formatCurrentDate(picturenameformat);
        return formated;
    }

    public static String formatCurrentDate (String format) {
        SimpleDateFormat dateformat = new SimpleDateFormat(format);
        String formateddate = dateformat.format(new Date());
        return formateddate;
    }
}

And where i call it

public class Screenshot {
    static Integer n = 1;
    public static void take() {
        WebDriver driver = WebDriverFactory.getInstance();
        TakesScreenshot scrShot =((TakesScreenshot)driver);
        File srcFile=scrShot.getScreenshotAs(OutputType.FILE);
        File destFile=null;
        n++;
        try {
            destFile=new File("/Users/me/Desktop/folder bez nazwy/"+DateTime.formatPicName()+".jpg");
            FileHandler.copy(srcFile, destFile);
            String destfilestr = destFile.getAbsolutePath();
            System.out.println("Created screenshot at: "+destfilestr);
        } catch (IOException e) {
            System.out.println("Something went wrong with copying file at destination. Kindly check Screenshot -> take method");
            e.printStackTrace();
        }
    }
}

I expect output to be current time everytime i call DateTime.formatLogTime DateTime.formatPicName

meanwhile it keep showing the init time everytime i launch it

Upvotes: 0

Views: 129

Answers (1)

Thrash Bean
Thrash Bean

Reputation: 658

Your format string is incorrect. Change this:

static String logtimeformat = DateTime.formatCurrentDate("HH:mm:ss  ");
static String filenametimeformat = DateTime.formatCurrentDate("dd-MM-yyyy_HH-mm-ss");
static String picturenameformat = DateTime.formatCurrentDate("dd-MM-yyyy_HH-mm-ss-SSS");

to this:

static String logtimeformat = "HH:mm:ss";
static String filenametimeformat = "dd-MM-yyyy_HH-mm-ss";
static String picturenameformat = "dd-MM-yyyy_HH-mm-ss-SSS";

Upvotes: 3

Related Questions