demand

Requirement: Give you a time format (YYYY-MM-DD) and you give me the time to return the previous month’s timestamp, the previous day’s timestamp, the previous week’s timestamp

Me: ????

Use Java8’s new API and you’ll find you can’t go back?

Not so much code

/ * * *@author :hujiansong
 * @date: 2019/6/24 17:59 *@since: 1.8 * /
public class NewDateTimeAPI {

    private static long getTimestamp(String dateText){
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // Parse to a Java8 date
        LocalDate parse = LocalDate.parse(dateText, pattern);
        // Parse to Java8 date-time (only date-time has timestamp)
        LocalDateTime dateTime = LocalDateTime.of(parse, LocalTime.of(0.0));
        // Convert to timestamp
        return dateTime.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
    }


    public static void main(String[] args) {
        long lastMonthTimestamp = getTimestamp("2018-11-12"); System.out.println(lastMonthTimestamp); }}Copy the code

As you can see, it’s very simple:

There are three main steps:

  1. Format to date
  2. Date becomes date time
  3. Date time passatZone()Determine the time zone and convert toInstant, and finally converted to a timestamp

Then the SAO operation came.

Get last month’s timestamp:

    private static long getLastMonthTimestamp(String dateText){
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // Parse to a Java8 date
        LocalDate parse = LocalDate.parse(dateText, pattern);
        // Parse to Java8 date-time (only date-time has timestamp)
        LocalDateTime dateTime = LocalDateTime.of(parse, LocalTime.of(0.0));

        // Get the timestamp of the previous month
        dateTime = dateTime.minusMonths(1);

        // Convert to timestamp
        return dateTime.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
    }

Copy the code

Just add one line datetime.minusmonths (1);

Similarly, the same fetch day before, week before, specific call different methods.

minusDays() minusHours() minusMinutes() minusSeconds()

You can specify a specific time:

    private static long specialDate(String dateText) {
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // Parse to a Java8 date
        LocalDate parse = LocalDate.parse(dateText, pattern);
        // Parse to Java8 date-time (only date-time has timestamp)
        LocalDateTime dateTime = LocalDateTime.of(parse, LocalTime.of(0.0));

        // Get the timestamp of 12:00.0sec of the current date
        dateTime = dateTime.withHour(12)
                .withMinute(0)
                .withSecond(0);

        // Convert to timestamp
        return dateTime.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
    }
Copy the code

Gets the timestamp for a specific hour, minute and second.

withHour() withMinute() withSecond()

The timestamp is converted to a specific format date

    public static String ts2Str(long second){
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(second), ZoneId.systemDefault());
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = localDateTime.format(pattern);
        return format;
    }
Copy the code

The timestamp is converted to Instant, then from Instant to LocalDateTime, and finally to format.

conclusion

Timestamp to date: Convert to Instant using instant.ofepochsecond () and then call localDatetime.ofinstant () to get the date and time object

Note: The timestamp is for LocalDateTime only. LocalDate does not have a timestamp.

Of (LocalDate, localtime. of(0, 0));

String to date: Localdate.parse () is used in YYYY-MM-DD format and localDatetime.parse () is used in YYYY-MM-DD HH: MM :ss

Convert to LocalDateTime or LocalDate so you can call specific methods to manipulate dates. You’ll find you can’t go back.