字符串转为Date对象
public static Date stringToDate(String strDate) { // 注意:SimpleDateFormat构造函数的样式与strDate的样式必须相符 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date=null; try { date = simpleDateFormat.parse(strDate); } catch (ParseException e) { e.printStackTrace(); } return date; } |
将long类型的转为Date对象
public static Date longToDate(long longDate){ return new Date(longDate); } |
获取今天是星期几
/** * 获取当前星期几 * @return 1,2,3,4,5,6,0 */ public static int getCurrentWeekday(){ Calendar cal = Calendar.getInstance(); int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK)-1;//今天是星期几 return dayOfWeek; } |
把日期格式化
/** * 把日期格式化 * @param dateformat 如:yyyy-MM-dd HH:mm:ss * @return */ public static String getFormatDate(Date date,String dateformat){ SimpleDateFormat format = new SimpleDateFormat(dateformat); return format.format(date); } |
判断某个时间是否在指定时间段之间
/** * 判断某个时间是否在指定时间段之间 * @param compareTime * @param beginTime * @param endTime * @return beginTime <= compareTime <= endTime : true; other: false; */ public static boolean timeCom(String compareTime, String beginTime,String endTime){ if(compareTime.compareTo(beginTime) >= 0 && compareTime.compareTo(endTime) <= 0){ return true; }else{ return false; } } |
获取当前时间
/** * 获取当前时间 * @param dateformat 如:yyyy-MM-dd HH:mm:ss * @return */ public static String getCurTime(String dateformat){ Calendar calendar = new GregorianCalendar(); Date date = calendar.getTime(); SimpleDateFormat format = new SimpleDateFormat(dateformat); return format.format(date); } |
获取今天之前/后的某一天
/** * 获取今天之前/后的某一天 * @param tip 正数表示之后,负数表示之前 * @param dateformat 日期格式 * @return */ public static String dateShortFormat(int tip,String dateformat){ Calendar calendar = new GregorianCalendar(); calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH)+tip); Date date = calendar.getTime(); SimpleDateFormat format = new SimpleDateFormat(dateformat); String dateInfo = format.format(date); return dateInfo; } |