工具类代码:
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);
}
}