How to add X number of days to current date and format as UTC

flatwhite :

I'm trying to get the current date/time add a number of days and display in a UTC format.

Current output is like this: 2019-05-09T11:11:4226

See code for below, I think I'm maybe taking the wrong approach to this.

       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);
Stultuske :

Starting from Java8, this would be a better approach:

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

to break it down:

LocalDateTime.now()

returns an instance of LocalDateTime with the current date and time. You add one day (using the plusDays(Long days)) method.

The result of this, you format using a DateTimeFromatter. The result of the complete is a String with the right date/format.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=157168&siteId=1