gpt4 book ai didi

java - SimpleDateFormat不更新时间

转载 作者:行者123 更新时间:2023-12-01 19:38:04 25 4
gpt4 key购买 nike

我试图创建一个类(DateTime),在其中收集与格式化时间、等待等相关的所有方法。当我尝试使用 SimpleDateFormat 时,它会不断返回(初始化)时间,而不是每次调用方法时的新时间。

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;
}
}

我在哪里调用它

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();
}
}
}

我希望每次调用时输出都是当前时间日期时间.formatLogTime日期时间.formatPicName

同时,每次我启动它时,它都会显示初始化时间

最佳答案

您的格式字符串不正确。更改此:

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");

对此:

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";

关于java - SimpleDateFormat不更新时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56661247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com