minus Months from java.util.Date via LocalDate - Java java.time

Java examples for java.time:Month

Description

minus Months from java.util.Date via LocalDate

Demo Code


//package com.java2s;
import java.time.LocalDate;

import java.time.ZoneId;
import java.util.Date;

public class Main {
    public static Date minusMonths(Date date, int months) {
        return localDateToDate(dateToLocalDate(date).minusMonths(months));
    }/*from   ww  w. j  a va 2  s .  c  o m*/

    public static Date localDateToDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay(ZoneId.systemDefault())
                .toInstant());
    }

    public static LocalDate dateToLocalDate(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault())
                .toLocalDate();
    }
}

Related Tutorials