Get the first day timestamp in the same month for specify timestamp. - Java java.time

Java examples for java.time:Month

Description

Get the first day timestamp in the same month for specify timestamp.

Demo Code


//package com.java2s;

import java.time.*;

public class Main {
    /**/*from   w  ww .  j  a v a  2s. c  o  m*/
     * Get the first day timestamp in the same month for specify timestamp. That
     * mean the result is the first Mills second in the specify month.
     * 
     * @param timestamp
     *          A timestamp need to calculate.
     * @return
     */
    public static long getMonthHeadMills(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZonedDateTime dateTime = ZonedDateTime.ofInstant(instant,
                ZoneId.systemDefault());
        // Get the first day of this month.
        ZonedDateTime firstDateTime = ZonedDateTime.of(dateTime.getYear(),
                dateTime.getMonthValue(), 1, 0, 0, 0, 0,
                ZoneId.systemDefault());
        return firstDateTime.toInstant().toEpochMilli();
    }
}

Related Tutorials