Author: Is He Tian Tian there

juejin.im/post/5d7787625188252388753eae

In the process of project development, we often encounter time processing, but do you really use it correctly? Do you understand the disable static modifier SimpleDateFormat in Alibaba Development Manual

By reading this article you will know:

  • Why do we need LocalDate, LocalTime, LocalDateTime?
  • Java 8 uses the new time API, including create, format, parse, compute, modify

Why do we need localDate, localTime, localDateTime

Date If not formatted, the printed Date is not readable

Tue Sep 10 09:34:04 CST 2019

Time is formatted using SimpleDateFormat, but SimpleDateFormat is the final call code for the thread-unsafe format method of SimpleDateFormat:

private StringBuffer format(Date date, StringBuffer toAppendTo,
                              FieldDelegate delegate) {
        
        calendar.setTime(date);

        boolean useDateFormatSymbols = useDateFormatSymbols();

        for (int i = 0; i < compiledPattern.length; ) {
            int tag = compiledPattern[i] >>> 8;
            int count = compiledPattern[i++] & 0xff;
            if (count == 255) {
                count = compiledPattern[i++] << 16;
                count |= compiledPattern[i++];
            }

            switch (tag) {
            case TAG_QUOTE_ASCII_CHAR:
                toAppendTo.append((char)count);
                break;

            case TAG_QUOTE_CHARS:
                toAppendTo.append(compiledPattern, i, count);
                i += count;
                break;

            default:
                subFormat(tag, count, delegate, toAppendTo, useDateFormatSymbols);
                break;
            }
        }
        return toAppendTo;
    }

Calendar is a shared variable, and this shared variable is not thread safe. When multiple threads simultaneously call the format method using the same SimpleDateFormat object (e.g., static SimpleDateFormat), multiple threads simultaneously call the Calendar. SetTime method. It is possible that one thread has just set the time value and another thread has changed the time value immediately, so the formatted time returned may be incorrect.

In addition to format being thread-unsafe, the parse method is also thread-unsafe. The parse method actually calls the alb.establish(Calendar).getTime() method, which is mostly done in the alb.establish(Calendar) method

  • Resets the property value of the date object CAL
  • Set CAL using the middle property in CALB
  • Returns the CAL object set but these three steps are not atomic operations

How does multithreading concurrency ensure thread safety

  • Avoid sharing a simpleDateFormat object between threads. Each thread creates the simpleDateFormat object once when using it =>. Creating and destroying objects is expensive
  • Locking where format and parse methods are used => thread blocking performance is poor
  • Using ThreadLocal is a better way to ensure that the SimpleDateFormat object => is created at most once per thread

For example, if you want to get the Date in the month, month, week, and n days after the Date, it is very difficult to use the Date method. You might say that the Date class has getYear, getMonth and other methods, which are Easy to get, but all are deprecated

Come On to use Java 8’s new date and time API

LocalDate

Only the year, month and day will be retrieved

Create a LocalDate

LocalDate localDate = LocalDate.now();

LocalDate localDate1 = LocalDate.of(2019, 9, 10);

Get the year, month, day, and day of the week

int year = localDate.getYear();
int year1 = localDate.get(ChronoField.YEAR);
Month month = localDate.getMonth();
int month1 = localDate.get(ChronoField.MONTH_OF_YEAR);
int day = localDate.getDayOfMonth();
int day1 = localDate.get(ChronoField.DAY_OF_MONTH);
DayOfWeek dayOfWeek = localDate.getDayOfWeek();
int dayOfWeek1 = localDate.get(ChronoField.DAY_OF_WEEK);

LocalTime

It only gets a few minutes, a few seconds

Create a LocalTime

 LocalTime localTime = LocalTime.of(13, 51, 10);
 LocalTime localTime1 = LocalTime.now();

Gets the time minutes and seconds

int hour = localTime.getHour();
int hour1 = localTime.get(ChronoField.HOUR_OF_DAY);

int minute = localTime.getMinute();
int minute1 = localTime.get(ChronoField.MINUTE_OF_HOUR);

int second = localTime.getMinute();
int second1 = localTime.get(ChronoField.SECOND_OF_MINUTE);

LocalDateTime

Gets year, month, day, minute and second, equal to localDate + localTime

Create LocalDateTime

LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime localDateTime1 = LocalDateTime.of(2019, Month.SEPTEMBER, 10, 14, 46, 56);
LocalDateTime localDateTime2 = LocalDateTime.of(localDate, localTime);
LocalDateTime localDateTime3 = localDate.atTime(localTime);
LocalDateTime localDateTime4 = localTime.atDate(localDate);

To obtain a LocalDate

LocalDate localDate2 = localDateTime.toLocalDate();

To obtain a LocalTime

LocalTime localTime2 = localDateTime.toLocalTime();

Instant

Get number of seconds

Creating Instant Objects

Instant instant = Instant.now();

Get number of seconds

long currentSecond = instant.getEpochSecond();

Get number of milliseconds

long currentMilli = instant.toEpochMilli();

I find it more convenient to use System.currentTimeMillis() just to get the number of seconds or milliseconds

Change localDate, localTime, localDateTime, and Instant

LocalDate, LocalTime, LocalDateTime, and Instant are immutable objects, and modifying these objects will return a copy

Increase or decrease the number of years, months, days, etc. Take LocalDateTime as an example

LocalDateTime localDateTime = LocalDateTime.of(2019, Month.SEPTEMBER, 10,
              14, 46, 56);

localDateTime = localDateTime.plusYears(1);
localDateTime = localDateTime.plus(1, ChronoUnit.YEARS);

localDateTime = localDateTime.minusMonths(1);
localDateTime = localDateTime.minus(1, ChronoUnit.MONTHS);  

Modify some values with

localDateTime = localDateTime.withYear(2020);

localDateTime = localDateTime.with(ChronoField.YEAR, 2022);

You can also modify the month and day

Time to calculate

For example, if you want to know what the last day of the month is, or what the next weekend is, you can get a quick answer by providing the time and date API

LocalDate localDate = LocalDate.now(); LocalDate localDate1 = localDate.with(firstDayOfYear()); For example, firstDayOfYear() returns the first day of the current date, and there are many other methods that I won't illustrate here

Format time

LocalDate localDate = LocalDate.of(2019, 9, 10); String s1 = localDate.format(DateTimeFormatter.BASIC_ISO_DATE); String s2 = localDate.format(DateTimeFormatter.ISO_LOCAL_DATE); DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); String s3 = localDate.format(dateTimeFormatter); The DataTimeFormatter default provides a variety of formats. If the default does not meet the requirements, you can create a custom format using the ofPattern method of the DataTimeFormatter

Parsing time

LocalDate localDate1 = LocalDate.parse("20190910", DateTimeFormatter.BASIC_ISO_DATE);
LocalDate localDate2 = LocalDate.parse("2019-09-10", DateTimeFormatter.ISO_LOCAL_DATE);

Compared to SimpleDateFormat, DataTimeFormatter is thread-safe

summary

LocaldateTime: Date I have all of them, and I have some of them that I don’t have. Pick Me for the Date

Recommended reading

Amazing, this Java website, everything! https://markerhub.com

The B station of the UP Lord, the JAVA is really good!

Too great! The latest edition of Java programming ideas can be viewed online!