Java DateTimeFormatter changeDateByIndex(String date, String index)

PreviousNext

//package com.java2s;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static String changeDateByIndex(String date, String index) {
        DateTimeFormatter yyyyMMdd = DateTimeFormatter.ofPattern("yyyy/MM/dd");

        if (date == null) {
            date = LocalDateTime.now().format(yyyyMMdd);

        } else {/*w  w   w  . d   e m   o   2  s . c    om  */
            LocalDate localDate = LocalDate.parse(date, yyyyMMdd);

            if (index != null)
                localDate = Integer.valueOf(index) == 1 ? localDate.plusDays(1)
                        : Integer.valueOf(index) == -1 ? localDate.minusDays(1) : localDate;

            date = localDate.format(yyyyMMdd);
        }
        return date;
    }
}
PreviousNext

Related