/**
* 时间戳转换时分秒
*
* @param mss传入时间戳
*/
public static String formatDuring(long mss) {
long days = mss / (60 * 60 * 24);
long hours = (mss % (60 * 60 * 24)) / (60 * 60);
long minutes = (mss % (60 * 60)) / (60);
if (days != 0) {
return days + "d " + hours + "h " + minutes + "min ";
} else if (hours != 0) {
return hours + "h " + minutes + "min ";
} else if (minutes != 0) {
return minutes + "min ";
}else{
return " 0min ";
}
}
/**
* 获取指定月份的前几个月
*
* @param strDate 传入的字符串时间
* @param num 想获取前几个月 就传几
* @return
*/
public String getStrDate(String strDate, int num) {
String stringDate = "";
try {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");//格式化为2017-10
Date month = formatter.parse(strDate);
Calendar calendar = Calendar.getInstance();//得到Calendar实例
calendar.setTime(month);
calendar.add(Calendar.MONTH, -num);//把月份减一个月
Date starDate = calendar.getTime();//得到时间赋给Data
stringDate = formatter.format(starDate).replace("-", "");//使用格式化Data
return stringDate;
} catch (Exception e) {
e.printStackTrace();
return stringDate;
}
}
/**
* 获取本月和上月第一天和最后一天
*
* @param strDate 传入的字符串时间
* @param num 想获取前几个月 就传几
* @return
*/
public Map<String, String> getStaEndTime(String strDate, int num) {
HashMap<String, String> map = new HashMap<>();
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date month = format.parse(strDate + "-1");
Calendar c = Calendar.getInstance();
c.setTime(month);
c.add(Calendar.MONTH, num);
c.set(Calendar.DAY_OF_MONTH, 1);//1:本月第一天
String day1 = format.format(c.getTime());
map.put("staDate", day1 + " 00:00:00");
//获取当前月最后一天
Calendar ca = Calendar.getInstance();
ca.setTime(month);
ca.add(Calendar.MONTH, num);
ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
String day2 = format.format(ca.getTime());
map.put("endDate", day2 + " 23:59:59");
} catch (ParseException e) {
e.printStackTrace();
} finally {
return map;
}
}
/**
*
* @Description: ${description} 去掉 "时分秒并且获取某周日期"
* @Author: YYY
* @Date: 日期time 某一天num
**/
public String weekStr(String time, int num) {
String week = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
Date month = sdf.parse(time);
cal.setTime(month);
cal.add(Calendar.DAY_OF_WEEK, num);
week = sdf.format(cal.getTime());
} catch (ParseException e) {
e.printStackTrace();
} finally {
}
return week;
}
/**
* 去掉"时分秒"
*
*/
public String delDatehms(String time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
Date month = null;
try {
month = sdf.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
cal.setTime(month);
return sdf.format(cal.getTime());
}
/**
* 获取当前星期
*/
public static String XQ(){
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
String[] weekDays = { "7","1", "2", "3", "4", "5","6" };
Calendar cal = Calendar.getInstance();
Date date;
try {
cal.setTime(new Date());
} catch (Exception e) {
e.printStackTrace();
}
//一周的第几天
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
/**
* 判断是否超过24小时
*
*/
public static boolean judgmentDate(String date1, String date2){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d HH:mm:ss");
Date start = null;
Date end = null;
try {
start = sdf.parse(date1);
end = sdf.parse(date2);
} catch (ParseException e) {
e.printStackTrace();
}
long cha = end.getTime() - start.getTime();
if (cha < 0) {
return false;
}
double result = cha * 1.0 / (1000 * 60 * 60);
if (result <= 24) {
return true;
} else {
return false;
}
}
/**
* 获取当前时分秒
*/
public static String sfm(){
Date t = new Date();
java.sql.Time sqlt = new java.sql.Time(t.getTime());
return sqlt.toString();
}
public interface TimeTypeConstant {
int TODAY=0;
int SEVEN_DAY=1;
int FIFTEEN_DAY=2;
int THIRTY_DAY=3;
int THREE_MONTHS=4;
int SIX_MONTHS=5;
int ONE_YEAR=6;
}
private Date getDate(Integer timeType) {
//当天的最早开始时间
Date date = DateUtil.beginOfDay(DateUtil.date());
//offsetDay常用偏移
if (timeType == TimeTypeConstant.SEVEN_DAY) {
date = DateUtil.offsetDay(date, -7);
}
if (timeType == TimeTypeConstant.FIFTEEN_DAY) {
date = DateUtil.offsetDay(date, -15);
}
if (timeType == TimeTypeConstant.THIRTY_DAY) {
date = DateUtil.offsetDay(date, -30);
}
if (timeType == TimeTypeConstant.THREE_MONTHS) {
date = DateUtil.offsetMonth(date, -3);
}
if (timeType == TimeTypeConstant.SIX_MONTHS) {
date = DateUtil.offsetMonth(date, -6);
}
if (timeType == TimeTypeConstant.ONE_YEAR) {
date = DateUtil.offset(date, DateField.YEAR, -1);
}
return date;
}
Java时间工具类
最新推荐文章于 2024-04-27 11:56:37 发布