get Last Date Of Last Month - Java java.time

Java examples for java.time:Month

Description

get Last Date Of Last Month

Demo Code


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

import java.time.temporal.TemporalAdjusters;
import java.util.Date;

public class Main {
    public static Date getLastDateOfLastMonth() {
        return toDate(LocalDate.now().minusMonths(1)
                .with(TemporalAdjusters.lastDayOfMonth()));
    }/*  w  w  w  .  j a v  a  2  s  .  com*/

    public static Date toDate(LocalDate date) {
        Instant instant = date.atStartOfDay()
                .atZone(ZoneId.systemDefault()).toInstant();
        return Date.from(instant);
    }

    public static Date toDate(LocalDateTime date) {
        Instant instant = date.atZone(ZoneId.systemDefault()).toInstant();
        return Date.from(instant);
    }
}

Related Tutorials