获取时间的特殊字段TimeHelp
public class DateHelp { private final static String FORMAT_TIME = "yyyy-MM-dd HH:mm:ss"; private final static String FORMAT_TO_DEFAULT_TIME = "yyyy-MM-dd 22:00:00"; /** * 获取当前时间 * * @return String(如:2019-06-28 16:46:47) */ public static String getTimeString() { @SuppressLint("SimpleDateFormat") SimpleDateFormat df = new SimpleDateFormat(FORMAT_TIME); Calendar calendar = Calendar.getInstance(); return df.format(calendar.getTime()); } /** * 获取日期年份 * * @param date date * @return int (如:2019) */ public static int getYear(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.YEAR); } /** * 功能描述:返回月 * * @param date date * @return int (如:10) */ public static int getMonth(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int mouth = calendar.get(Calendar.MONTH) + 1; return mouth > 12 ? 1 : mouth; } /** * 功能描述:返回日期 * * @param date date * @return int (如:28) */ public static int getDay(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.DAY_OF_MONTH); } /** * 功能描述:返回小时 * * @param date date * @return int (如:12) */ public static int getHour(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.HOUR_OF_DAY); } /** * 功能描述:返回分 * * @param date date * @return int (如:60) */ public static int getMinute(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.MINUTE); } /** * 返回秒钟 * * @param date date * @return int (如:60) */ public static int getSecond(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.SECOND); } /** * 功能描述:返回毫 * * @param date date * @return int (如:60) */ public static long getMillis(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.getTimeInMillis(); } /** * 功能描述:将传入的时间(String类型)转化成long * 传入时间格式如:"2019-06-28 16:46:47" * * @param time time * @return long */ public static long defaultEndTime(String time) { @SuppressLint("SimpleDateFormat") SimpleDateFormat dateFormat = new SimpleDateFormat(FORMAT_TIME); long mTime = 0; try { Date date = dateFormat.parse(time); mTime = date.getTime(); } catch (ParseException e) { e.printStackTrace(); } return mTime; } /** * 功能描述:获取当前年月日下的22:00:00 * 返回的结果如:2019-06-28 2:00:00" * (只有22:00:00不会变) * * @return String */ public static String endTime() { @SuppressLint("SimpleDateFormat") SimpleDateFormat dateFormat = new SimpleDateFormat(FORMAT_TO_DEFAULT_TIME); Calendar cal = Calendar.getInstance(); long time = cal.getTimeInMillis(); Date date = new Date(time); return dateFormat.format(date); } /** * 功能描述:推迟或提前月份 * 如:当前是"2019-06-28 22:00:00" 返回"2019-08-28 22:00:00" * * @param isDelay 月份是推迟还是提前 * @param number 推迟或提前月份多少月 * @return String */ public static String getDelayTime(boolean isDelay, int number) { @SuppressLint("SimpleDateFormat") SimpleDateFormat dateFormat = new SimpleDateFormat(FORMAT_TO_DEFAULT_TIME); Calendar calendar = Calendar.getInstance(); if (isDelay) { //得到后几个月 calendar.add(Calendar.MONTH, +number); } else { //得到前几个月 calendar.add(Calendar.MONTH, -number); } long timeInMillis = calendar.getTimeInMillis(); Date date = new Date(timeInMillis); return dateFormat.format(date); } }