有没有办法将当前的时间戳实际附加到这个 JSON 文件上?

huangapple go评论86阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年10月27日 07:02:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64546140.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定