gpt4 book ai didi

java - 如何将 X 天数添加到当前日期并将格式设置为 UTC

转载 作者:行者123 更新时间:2023-11-30 10:04:04 25 4
gpt4 key购买 nike

我正在尝试获取当前日期/时间加上天数并以 UTC 格式显示。

当前输出是这样的:2019-05-09T11:11:4226

请参阅下面的代码,我认为我可能对此采取了错误的方法。

       int NumDaysInFurure = 1;

Date currentDate = new Date();// get the current date

SimpleDateFormat daateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
daateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String date = ""+dateFormat.format(currentDate) + NumDaysInFurure;//add one day to the current date


Log.i("the future date is", date);

最佳答案

从 Java8 开始,这将是一个更好的方法:

DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss").format(LocalDateTime.now().plusDays(1))

分解:

LocalDateTime.now()

返回具有当前日期和时间的 LocalDateTime 实例。您添加一天(使用 plusDays(Long days))方法。

结果是,您使用 DateTimeFromatter 进行格式化。complete 的结果是一个具有正确日期/格式的字符串。

关于java - 如何将 X 天数添加到当前日期并将格式设置为 UTC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56057108/

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