dai
dai

Reputation: 1075

How to get last week and last month with java8

I have a date and I need to know the last week and last month before the date.

For example,

on July 15, 2018, Its last week was from July 2 to July 8. Its last month was June 1 to June 30.

on July 16, 2018, Its last week was from July 9 to July 15. Its last month was June 1 to June 30.

on July 17, 2018, Its last week was from July 9 to July 15. Its last month was June 1 to June 30.

It is different from get-date-of-first-day-of-week-based-on-localdate-now-in-java-8, my problem is to get last week or last month.

Upvotes: 5

Views: 11859

Answers (4)

Leviand
Leviand

Reputation: 2805

You can easly do that with Java 8 LocalDate, here's my solution:

import java.time.LocalDate;

LocalDate now = LocalDate.now();
LocalDate weekStart = now.minusDays(7+now.getDayOfWeek().getValue()-1);
LocalDate weekEnd = now.minusDays(now.getDayOfWeek().getValue());

LocalDate previousMonth = now.minusMonths(1);
LocalDate monthStart = previousMonth.withDayOfMonth(1);
LocalDate monthEnd = previousMonth.withDayOfMonth(previousMonth.getMonth().maxLength());

System.out.println("WeekStart:"+weekStart+", weekEnd:"+weekEnd+", monthStart:"+monthStart+", monthEnd:"+monthEnd);

Result

WeekStart:2018-07-09, weekEnd:2018-07-15, monthStart:2018-06-01, monthEnd:2018-06-30

If you change the now line with

LocalDate now = LocalDate.of(2018,07,15);

You'll get:

WeekStart:2018-07-02, weekEnd:2018-07-08, monthStart:2018-06-01, monthEnd:2018-06-30

Upvotes: 4

Alexandros
Alexandros

Reputation: 733

Not sure if you got a Date object, hopefully you got. From date object you can get Instant with method toInstant() (To pasrse String to date..

DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
Date date = format.parse(string);

date format must be addapted to your needs ofcourse )

Instant.now().minus(Duration.ofDays(7))); // last week
Instant.now().minus(Duration.of(1,ChronoUnit.MONTHS)) // last month

Upvotes: 0

Jai
Jai

Reputation: 8363

You can use these helper methods.

public static LocalDate[] getPreviousWeek(LocalDate date) {
    final int dayOfWeek = date.getDayOfWeek().getValue();
    final LocalDate from = date.minusDays(dayOfWeek + 6); // (dayOfWeek - 1) + 7
    final LocalDate to = date.minusDays(dayOfWeek);

    return new LocalDate[]{from, to};
}

public static LocalDate[] getPreviousMonth(LocalDate date) {
    final LocalDate from = date.minusDays(date.getDayOfMonth() - 1).minusMonths(1);
    final LocalDate to = from.plusMonths(1).minusDays(1);

    return new LocalDate[]{from, to};
}

There are in fact many ways to write this. I would suggest you to do some exploration on your own.

Upvotes: 13

ZonedDateTime is useful for that.

import java.time.*;
import java.time.format.DateTimeFormatter;

class DateTest
{
    public static void main (String[] args) throws java.lang.Exception
    {
        DateTimeFormatter format = DateTimeFormatter.ofPattern("dd MMM yyyy");
        ZoneId usCentral = ZoneId.of("America/Chicago");
        LocalDateTime localDateTime = LocalDateTime.of(2018, Month.JULY, 16, 9, 30);

        ZonedDateTime input = ZonedDateTime.of(localDateTime, usCentral);
        System.out.println("Input date = " + format.format(input));

        ZonedDateTime startDate = input.minusWeeks(1).with(DayOfWeek.MONDAY);
        System.out.println("Start date = " + format.format(startDate));

        ZonedDateTime endDate = startDate.plusDays(6);
        System.out.println("End date = " + format.format(endDate));
    }
}

Output:

Input date = 16 Jul 2018
Start date = 09 Jul 2018
End date = 15 Jul 2018

Upvotes: 1

Related Questions