简体   繁体   中英

Start and End of Week w/ ThreeTenBackport

I'm currently changing some code from Joda-Time to use Three-Ten Android Backport.

All of the following methods take in a long as param and return a long

Getting the end and start of the week with JodaTime was straight forward with Joda-Time:

LocalDate(long time).dayOfWeek().withMaximumValue()
LocalDate(long time).dayOfWeek().withMinimumValue()

The same issue is present with end and start of a day, with Joda-Time:

DateTime(long time).withTimeAtStartOfDay().getMillis() + DateUtils.DAY_IN_MILLIS - 1
DateTime(long time).withTimeAtStartOfDay().getMillis()

However I don't understand how to use threeTenAbp in this way.

One thought for end and start of days:

END OF DAY:

LocalDateTime dt = DateTimeUtils.toLocalDateTime(new Timestamp(time));
ZonedDateTime zdt = ZonedDateTime.of(dt, ZoneId.systemDefault());
return zdt.with(LocalTime.MAX).toEpochSecond();

START OF DAY:

LocalDateTime dt = DateTimeUtils.toLocalDateTime(new Timestamp(long time));
ZonedDateTime zdt = ZonedDateTime.of(dt, ZoneId.systemDefault());
return zdt.toLocalDate().atStartOfDay(ZoneId.systemDefault()).toEpochSecond();

This seems pretty convoluted and doesn't really provide me with any clues on how to obtain and time for the start and end of the week respective to the long time passed in to the functions.

To get the start and end of day, I would use the with() method, passing in one of the pre-defined LocalTime instances:

LocalDateTime ldt = LocalDateTime.now();
LocalDateTime startOfDay = ldt.with(LocalTime.MIN); // 00:00:00
LocalDateTime endOfDay = ldt.with(LocalTime.MAX); // 23:59:59

To get the first and last day of the week, you can do the same sort of thing but you have to first define what you mean by "a week". Some locales consider a week to start on Monday, and others consider it to start on Sunday.

In the US, weeks start on Sunday. We can get ahold of a TemporalField that represents this by using WeekFields.of() :

WeekFields usWeek = WeekFields.of(Locale.US);

We can then get the first and last day of the week by setting that field to 1 and to 7 , respectively:

LocalDateTime firstDayOfWeek = ldt.with(usWeek.dayOfWeek(), 1);
LocalDateTime lastDayOfWeek = ldt.with(usWeek.dayOfWeek(), 7);

We are not getting the full picture, so my suggestions may not be to the point, but let me attempt.

    long millisSinceEpoch = 1_555_555_555_555L;
    LocalDate date = Instant.ofEpochMilli(millisSinceEpoch)
            .atZone(ZoneId.systemDefault())
            .toLocalDate();
    LocalDate firstDayOfWeek = date.with(DayOfWeek.MONDAY);
    LocalDate lastDayOfWeek = date.with(DayOfWeek.SUNDAY);
    System.out.println("Week is from " + firstDayOfWeek + " through " + lastDayOfWeek);

Week is from 2019-04-15 through 2019-04-21

I am assuming that you want ISO weeks, that is, Monday is the first day of week (I think this is what your Joda-Time code gave you). If not, you need to introduce a WeekFields object as in Ben P.'s answer. I suggest that you don't want to convert back to long . A LocalDate is nicer to work with going forward.

For the start of the day:

    ZonedDateTime startOfDay = date.atStartOfDay(ZoneId.systemDefault());
    System.out.println("Start of day: " + startOfDay);

In my time zone I get:

Start of day: 2019-04-18T00:00+02:00[Europe/Copenhagen]

Again, keep your ZonedDateTime object rather than converting to milliseconds.

Don't calculate the end of the day. For the point where the day rolls over (“midnight”), always use the start of the day.

    ZonedDateTime startOfNextDay = date.plusDays(1)
            .atStartOfDay(ZoneId.systemDefault());
    System.out.println("Day is from " + startOfDay + " inclusive to " + startOfNextDay + " exclusive");

Day is from 2019-04-18T00:00+02:00[Europe/Copenhagen] inclusive to 2019-04-19T00:00+02:00[Europe/Copenhagen] exclusive

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM