英文:
Is there a way to actually append the current timestamp onto this json file?
问题
public static String FILE_LOCATION = "/fitness" + File.separator
+ "customers_" + "yyyymmddmmss" + ".json";
this is what i have so far.
英文:
public static String FILE_LOCATION = "/fitness"+File.separator
+"customers_" + "yyyymmddmmss" + ".json";
this is what i have so far.
答案1
得分: 0
你的格式String
中包含了两次mm
,但没有用于小时的HH
。但是是的,可以使用DateTimeFormatter
,并且类似下面这样:
public static String FILE_LOCATION = "/fitness/customers_"
+ DateTimeFormatter.ofPattern("yyyyMMddHHmmss")
.format(LocalDateTime.now()) + ".json";
英文:
Your format String
includes mm
twice and no HH
for hours. But yes, using a DateTimeFormatter
and something like
public static String FILE_LOCATION = "/fitness/customers_"
+ DateTimeFormatter.ofPattern("yyyyMMddHHmmss")
.format(LocalDateTime.now()) + ".json";
答案2
得分: 0
如果您希望将本地日期时间(在不同的时区中可能不同)作为文件名的一部分进行捕获,可以使用Elliott Frisch提供的答案。
然而,如果您希望日期时间与时区无关,您可以附加Instant.now()
的值,该值为您在UTC时间轴上的当前时刻。
public static String FILE_LOCATION = "/fitness/customers_"
+ Instant.now().toString() + ".json";
或者以自定义格式:
public static String FILE_LOCATION = "/fitness/customers_"
+ DateTimeFormatter.ofPattern("yyyyMMddHHmmss")
.format(LocalDateTime.now(ZoneOffset.UTC)) + ".json";
请注意,LocalDateTime.now()
等同于 LocalDateTime.now(ZoneId.systemDefault())
,它会给您 JVM 所在时区的日期时间。
英文:
If you want the local date-time (which is different in different time-zones) to be captured as part of the name of the file, the answer by Elliott Frisch is what you need.
However, if you want the date-time to be independent of a time-zone, you can append the value of Instant.now()
which gives you current moment at UTC time-line.
public static String FILE_LOCATION = "/fitness/customers_"
+ Instant.now().toString() + ".json";
or in the custom format as
public static String FILE_LOCATION = "/fitness/customers_"
+ DateTimeFormatter.ofPattern("yyyyMMddHHmmss")
.format(LocalDateTime.now(ZoneOffset.UTC)) + ".json";
Note that LocalDateTime.now()
is equivalent to LocalDateTime.now(ZoneId.systemDefault())
which gives you the date-time in the JVM's time-zone.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论