Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将X天数添加到当前日期并设置为UTC格式_Java_Android - Fatal编程技术网

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

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

我正在尝试获取当前日期/时间,添加天数并以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(长天数)
)方法

结果是,您可以使用
DateTimeFromatter
进行格式化。 完成的结果是一个日期/格式正确的字符串