Java get the same day, the current month, current year (this year) start and end timestamps

Recent statistics related to the function of doing when it comes to the acquisition start and end timestamps of the day, the month and the beginning of the year end timestamp, hereby records to make notes.

The relevant code

com.lingyejun.authenticator Package; 

Import java.time.Instant; 
Import java.time.LocalDateTime; 
Import java.time.ZoneId; 
Import the java.util.Calendar; 
Import the java.util.TimeZone; 

public class CalendarAdjust { 

    / ** 
     * Gets the day of the start timestamp 
     * 
     * @param millisecond timestamp timeStamp 
     * @param timeZone eg. 8 + GMT: 00 
     * @return 
     * / 
    public static Long getDailyStartTime (timeStamp Long, String timeZone) { 
        Calendar Calendar = Calendar.getInstance (); 
        calendar.setTimeZone (TimeZone.getTimeZone (timeZone)); 
        calendar.setTimeInMillis (timeStamp);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set (Calendar.SECOND, 0); 
        calendar.set (Calendar.MINUTE, 0); 
        calendar.set (Calendar.MILLISECOND, 0); 
        return calendar.getTimeInMillis (); 
    } 

    / ** 
     * Gets a specified day end timestamp 
     * 
     * @param millisecond timestamp timeStamp 
     * @param timeZone eg. 8 + GMT: 00 
     * @return 
     * / 
    public static Long getDailyEndTime (timeStamp Long, String timeZone) { 
        Calendar Calendar Calendar.getInstance = (); 
        Calendar. setTimeZone (TimeZone.getTimeZone (timeZone)); 
        calendar.setTimeInMillis (timeStamp); 
        calendar.set (Calendar.HOUR_OF_DAY, 23 is); 
        calendar.set (Calendar.MINUTE, 59); 
        calendar.set (Calendar.SECOND, 59);
        calendar.set (Calendar.MILLISECOND, 999); 
        return calendar.getTimeInMillis (); 
    } 

    / ** 
     * acquisition start time stamp month 
     * 
     * @param millisecond timestamp timeStamp 
     * @param timeZone eg. 8 + GMT: 00 
     * @return 
     * / 
    public static Long getMonthStartTime (timeStamp Long, String timeZone) { 
        Calendar Calendar Calendar.getInstance = (); // get the current date 
        calendar.setTimeZone (TimeZone.getTimeZone (timeZone)); 
        calendar.setTimeInMillis (timeStamp); 
        Calendar.add (Calendar.YEAR, 0);  
        Calendar.add (the Calendar.MONTH, 0);
        calendar.set (Calendar.DAY_OF_MONTH, 1); // set to 1, only the current date is the first day of the month 
        calendar.set (Calendar.HOUR_OF_DAY, 0);
        calendar.set (Calendar.MINUTE, 0); 
        calendar.set (Calendar.SECOND, 0); 
        calendar.set (Calendar.MILLISECOND, 0); 
        return calendar.getTimeInMillis (); 
    } 

    / ** 
     * acquired end time of the month stamp 
     * 
     * @param millisecond timestamp timeStamp 
     * @param timeZone eg. 8 + GMT: 00 
     * @return 
     * / 
    public static Long getMonthEndTime (timeStamp Long, String timeZone) { 
        Calendar Calendar Calendar.getInstance = (); // get the current date 
        calendar.setTimeZone (TimeZone.getTimeZone (timeZone));  
        calendar.setTimeInMillis (timeStamp);
        Calendar.add (Calendar.YEAR, 0); 
        Calendar.add (the Calendar.MONTH, 0);
        calendar.set (Calendar.DAY_OF_MONTH, calendar.getActualMaximum (Calendar.DAY_OF_MONTH) ); // get the last day of the current month 
        calendar.set (Calendar.HOUR_OF_DAY, 23); 
        calendar.set (Calendar.MINUTE, 59); 
        calendar.set (Calendar.SECOND, 59); 
        calendar.set (Calendar.MILLISECOND, 999); 
        return calendar.getTimeInMillis (); 
    } 

    / ** 
     * acquisition start time stamp year 
     * 
     * @param millisecond timestamp timeStamp 
     * @param timeZone eg. 8 + GMT: 00 
     * @return 
     * / 
    public static Long getYearStartTime (timeStamp Long, String timeZone) {  
        Calendar Calendar Calendar.getInstance = (); // get the current date
        calendar.setTimeZone (TimeZone.getTimeZone (timeZone));
        calendar.setTimeInMillis(timeStamp);
        calendar.add(Calendar.YEAR, 0);
        calendar.add(Calendar.DATE, 0);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取当年的最后时间戳
     *
     * @param timeStamp 毫秒级时间戳
     * @param timeZone  如 GMT+8:00
     * @return
     */
    public static Long getYearEndTime(Long timeStamp, String timeZone) {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
        calendar.setTimeInMillis(timeStamp);
        int year = calendar.get(Calendar.YEAR);
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        return calendar.getTimeInMillis();
    }

    /**
     * 时间戳转字符串
     *
     * @param timestamp 毫秒级时间戳
     * @param zoneId    如 GMT+8或UTC+08:00
     * @return
     */
    public static String timestampToStr(long timestamp, String zoneId) {
        ZoneId timezone = ZoneId.of(zoneId);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), timezone);
        return localDateTime.toString();
    }

    public static void main(String[] args) {
        Long currentTime = System.currentTimeMillis();
        System.out.println("Current Time : " + currentTime + " = " + timestampToStr(currentTime, "GMT+8"));
        Long dailyStart = getDailyStartTime(currentTime, "GMT+8:00");
        Long dailyEnd = getDailyEndTime(currentTime, "GMT+8:00");
        Long monthStart = getMonthStartTime(currentTime, "GMT+8:00");
        Long monthEnd = getMonthEndTime(currentTime, "GMT+8:00");
        Long yearStart = getYearStartTime(currentTime, "GMT+8:00");
        Long yearEnd = getYearEndTime(currentTime, "GMT+8:00");

        System.out.println("Daily Start : " + dailyStart + " = " + timestampToStr(dailyStart, "GMT+8") + " Daily End : " + dailyEnd + " = " + timestampToStr(dailyEnd, "GMT+8"));
        System.out.println("Month Start : " + monthStart + " = " + timestampToStr(monthStart, "GMT+8") + " Month End : " + monthEnd + " = " + timestampToStr(monthEnd, "GMT+8"));
        System.out.println("Year Start : " + yearStart + " = " + timestampToStr(yearStart, "GMT+8") + " Year End : " + yearEnd + " = " + timestampToStr(yearEnd, "GMT+8"));
    }
}

effect

 

Guess you like

Origin www.cnblogs.com/lingyejun/p/11298186.html
Recommended