public class GetTime {
/**
* 获取当前时间
*
* @param type
* @return
*/
public static String getDateToString(String type, long time) {
SimpleDateFormat sf = new SimpleDateFormat(type);
Date d = new Date(time);
return sf.format(d);
}
/**
* 获取指定哪天的日期
* 如 day=2 就是后天
* day=-2 就是前天
*
* @param day 正数向后推,负数向前推
* @return
*/
public static String getLastDateToString(int day) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, day);
return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
}
public static String getLastDateTimeToString(int day) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, day);
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cal.getTime());
}
/**
* 获取当前星期
*
* @param date
* @return
*/
public static String getWeekday(String date) {//必须yyyy-MM-dd
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdw = new SimpleDateFormat("E");
Date d = null;
try {
d = sd.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return sdw.format(d);
}
/*
* 将时间转换为时间戳
*/
public static String dateToStamp(String s) throws ParseException {
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(s);
long ts = date.getTime();
res = String.valueOf(ts);
return res;
}
/**
* 获取当前日的开始时间戳
* 即 xxxx-xx-xx 00:00:00
*
* @return
* @throws ParseException
*/
public static String dayStartTime() throws ParseException {
Calendar startCal = new GregorianCalendar();
startCal.set(Calendar.HOUR_OF_DAY, 0);
startCal.set(Calendar.MINUTE, 0);
startCal.set(Calendar.SECOND, 0);
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startCal.getTime());
}
/**
* 获取当前日的结束时间戳
* 即 xxxx-xx-xx 23:59:59
*
* @return
* @throws ParseException
*/
public static String dayEndTime() throws ParseException {
Calendar endCal = new GregorianCalendar();
endCal.set(Calendar.HOUR_OF_DAY, 23);
endCal.set(Calendar.MINUTE, 59);
endCal.set(Calendar.SECOND, 59);
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(endCal.getTime());
}
}
网友评论