Java 如何将X天数添加到当前日期并设置为UTC格式
我正在尝试获取当前日期/时间,添加天数并以UTC格式显示 电流输出如下:2019-05-09T11:11:4226 请参阅下面的代码,我想我可能采取了错误的方法Java 如何将X天数添加到当前日期并设置为UTC格式,java,android,Java,Android,我正在尝试获取当前日期/时间,添加天数并以UTC格式显示 电流输出如下:2019-05-09T11:11:4226 请参阅下面的代码,我想我可能采取了错误的方法 int NumDaysInFurure = 1; Date currentDate = new Date();// get the current date SimpleDateFormat daateFormat = new SimpleDateFormat("dd-MM-yyyy H
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(长天数)
)方法
结果是,您可以使用DateTimeFromatter
进行格式化。
完成的结果是一个日期/格式正确的字符串