时间操作相关工具类

工具类代码:

package com.ykl.studyMaterials.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 时间操作相关工具类
 * @author ykl
 *
 */
public class DateOperationUtil {

	/**
	 * 将时间戳转化为String格式
	 * 
	 * @param timeStamp 时间戳
	 *
	 */
	public static String timeConvertLongToString(Long timeStamp) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(new Date(timeStamp));
	}
	
	/**
	 * 根据时间戳获取该天开始时间
	 * 
	 * @param timeStamp 时间戳
	 */
	public static Long getCurrentStartTimeStamp(Long timeStamp) throws Exception {
		Long startTimeStamp = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		startTimeStamp = sdf.parse(sdf.format(new Date(timeStamp))).getTime();
		return startTimeStamp;
	}
	
    /**
     * 根据时间戳获取该月的实际天数
     * 
     * @param timeStamp 毫秒级时间戳
     * @param timeZone 时区,如 GMT+8:00(北京时间)
     */
    public static int getDayNumOfMonth(Long timeStamp, String timeZone) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
        calendar.setTimeInMillis(timeStamp);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);//获取月的实际天数
    }
    
	/**
     * 根据时间戳获取该月开始时间
     *
     * @param timeStamp 毫秒级时间戳
     * @param timeZone  时区,如 GMT+8:00(北京时间)
     */
    public static Long getMonthStartTime(Long timeStamp, String timeZone) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
        calendar.setTimeInMillis(timeStamp);
        calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为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
     */
    public static Long getMonthEndTime(Long timeStamp, String timeZone) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
        calendar.setTimeInMillis(timeStamp);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_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();
    }
    
    /**
     * 根据时间戳获取星期数(周日是一周的开始)
     *
     * @param timeStamp 毫秒级时间戳
     * @param timeZone  时区,如 GMT+8:00
     */
    public static int getWeekNum(Long timeStamp, String timeZone) {
    	Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
        calendar.setTimeInMillis(timeStamp);
        int week = calendar.get(Calendar.DAY_OF_WEEK)-1;
        if(week == 0){
        	week = 7;
        }
        return week;
    }
    
    /**
     * 根据时间戳获取该年的开始时间(月份从0开始)
     *
     * @param timeStamp 毫秒级时间戳
     * @param timeZone  时区,如 GMT+8:00
     */
    public static Long getYearStartTime(Long timeStamp, String timeZone) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
        calendar.setTimeInMillis(timeStamp);
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为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();
    }
    
    /**
     * 根据时间戳获取该年的结束时间(月份从0开始)
     *
     * @param timeStamp 毫秒级时间戳
     * @param timeZone  时区,如 GMT+8:00
     */
    public static Long getYearEndTime(Long timeStamp, String timeZone) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
        calendar.setTimeInMillis(timeStamp);
        calendar.set(Calendar.MONTH, 11);
        calendar.set(Calendar.DAY_OF_MONTH, 31);
        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();
    }

	public static void main(String[] args) throws Exception {
		Long nowTime = System.currentTimeMillis();
		String result1 = timeConvertLongToString(nowTime);
		System.out.println("当天时间戳转化为String:" + result1);
		
		Long result2 = getCurrentStartTimeStamp(nowTime);
		System.out.println("当天开始时间:" + result2);
		
		Long result3 = getCurrentStartTimeStamp(nowTime) + 86399999l;
		System.out.println("当天结束时间:" + result3);
		
		int result4 = getDayNumOfMonth(nowTime, "GMT+8:00");
		System.out.println("当月实际天数:" + result4);
		
		Long result5 = getMonthStartTime(nowTime, "GMT+8:00");
		System.out.println("当月开始时间:" + result5);
		
		Long result6 = getMonthEndTime(nowTime, "GMT+8:00");
		System.out.println("当月结束时间:" + result6);
		
		int result7 = getWeekNum(nowTime, "GMT+8:00");
		System.out.println("当天星期数:" + result7);
		
		Long result8 = getYearStartTime(nowTime, "GMT+8:00");
		System.out.println("当年开始时间:" + result8);
		
		Long result9 = getYearEndTime(nowTime, "GMT+8:00");
		System.out.println("当年结束时间:" + result9);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值