Unexpected formatting time with Calendar

Keywords: Java SQL

Java8 Chinese Documentation: http://www.matools.com/api/java8

java8 LocalDate Reference: https://www.journaldev.com/2800/java-8-date-localdate-localdatetime-instant

Background: Time sections are formatted where deadlines are involved, such as the start and end times of events (2019-11:00:00~2019-11-18:00:00), the validity times of prizes (2019-12-1:00:00~2019-12-30 23:59:59), etc.

Realization:

Calendar calendar = Calendar.getInstance();
 calendar.setTime(new Date());
 calendar.set(Calendar.HOUR_OF_DAY, 0);
 calendar.set(Calendar.MINUTE, 0);
 calendar.set(Calendar.MILLISECOND, 0);
 calendar.set(Calendar.SECOND, 0);
 return calendar.getTime();

Problem: As shown in the following figure, part of the time was not formatted as expected:

Reason: After formatting, the get operation has errors (<1s), and the getTime implementation is based on milliseconds:

public final Date getTime() {
        return new Date(getTimeInMillis());
    }

Improvement: Since we need an exact time node and don't care about the changes in that time and the specific meaning it contains, we can choose a new date time Api LocalDate, LocalDateTime for java8.

The following is an excerpt from the Internet. Why do we need a new API?Existing date and time related classes in java have some problems, some of which are:

  1. The Java Date Time class is not uniformly defined, and we have Date Class in both the java.util and java.sql packages.Formatting and parsing classes again are defined in the java.text package.

  2. java.util.Date contains date and time, while java.sql.Date only contains date.This is meaningless in the java.sql wrapper.Both classes have the same name, which in itself is a very bad design.

  3. Classes that are not explicitly defined for time, timestamp, formatting, and parsing.We have abstract classes of java.text.DateFormat for parsing and formatting requirements.Typically, the SimpleDateFormat class is used for parsing and formatting.

  4. All Date classes are mutable, so they are not thread safe.This is one of the biggest problems with the Java Date and Calendar classes.

  5. Date classes do not provide internationalization and no time zone support.So the java.util.Calendar and java.util.TimeZone classes are introduced, but they also have all the issues listed above.

Realization:

LocalDate today = LocalDate.now();
LocalDateTime format = LocalDateTime.of(today.getYear(), today.getMonth(), today.getDayOfMonth(), 23, 59, 59);
return Date.from(format.atZone(ZoneId.systemDefault()).toInstant());

Example usage scenarios:

  • Get the first second of the week

private Date getThisWeekMonday() {
        LocalDate today = LocalDate.now();
        LocalDateTime format = LocalDateTime.of(today.getYear(), today.getMonth(), today.getDayOfMonth(), 0, 0, 0);
       	format= format.with(DayOfWeek.MONDAY);
        return Date.from(format.atZone(ZoneId.systemDefault()).toInstant());
    }
  • Get the last second of the week
private Date getThisWeekSunday() {
        LocalDate today = LocalDate.now();
        LocalDateTime format = LocalDateTime.of(today.getYear(), today.getMonth(), today.getDayOfMonth(), 23, 59, 59);
        format=format.with(DayOfWeek.SUNDAY);
        return Date.from(format.atZone(ZoneId.systemDefault()).toInstant());
    }
  • Get the first second of last week
private Date getLastWeekMonday() {
        LocalDate today = LocalDate.now();
        LocalDate lastWeek = today.plusWeeks(-1);
        LocalDateTime format = LocalDateTime.of(lastWeek.getYear(), lastWeek.getMonth(), lastWeek.getDayOfMonth(), 0, 0, 0);
        format=format.with(DayOfWeek.MONDAY);
        return Date.from(format.atZone(ZoneId.systemDefault()).toInstant());
    }
  • Get last second of last week
private Date getLastWeekSunday() {
        LocalDate today = LocalDate.now();
        LocalDate lastWeek = today.plusWeeks(-1);
        LocalDateTime format = LocalDateTime.of(lastWeek.getYear(), lastWeek.getMonth(), lastWeek.getDayOfMonth(), 23, 59, 59);
        format=format.with(DayOfWeek.SUNDAY);
        return Date.from(format.atZone(ZoneId.systemDefault()).toInstant());
    }

 

56 original articles published. 53% praised. 80,000 visits+
Private letter follow

Posted by Altairzq on Tue, 04 Feb 2020 19:27:29 -0800