时间工具类TimeUtil 发表于 2015-07-08 | 分类于 AndroidUtil | 阅读次数: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114public class TimeUtil { /** * 获取当前时间 * * @param format "yyyy-MM-dd HH:mm:ss" */ public static String getCurrentTime(String format) { Date date = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.getDefault()); return simpleDateFormat.format(date); } /** * 获取当前时间为本月的第几周 */ public static int getWeekOfMonth() { Calendar calendar = Calendar.getInstance(); int week = calendar.get(Calendar.WEEK_OF_MONTH); return week - 1; } /** * 获取当前时间为本周的第几天 */ public static int getDayOfWeek() { Calendar calendar = Calendar.getInstance(); int day = calendar.get(Calendar.DAY_OF_WEEK); if (day == 1) { day = 7; } else { day = day - 1; } return day; } /** * 获取当前时间的年份 */ public static int getYear() { Calendar calendar = GregorianCalendar.getInstance(); return calendar.get(Calendar.YEAR); } /** * 获取当前时间的月份 */ public static int getMonth() { Calendar calendar = GregorianCalendar.getInstance(); return calendar.get(Calendar.MONTH); } /** * 获取当前时间是哪天 */ public static int getDay() { Calendar calendar = GregorianCalendar.getInstance(); return calendar.get(Calendar.DATE); } /** * @param date1 * @param date2 * @return 1:date1大于date2; * -1:date1小于date2 */ public static int compareDate(String date1, String date2, String format) { DateFormat df = new SimpleDateFormat(format, Locale.getDefault()); try { Date dt1 = df.parse(date1); Date dt2 = df.parse(date2); if (dt1.getTime() > dt2.getTime()) { return 1; } else if (dt1.getTime() < dt2.getTime()) { return -1; } else { return 0; } } catch (Exception exception) { exception.printStackTrace(); } return 0; } /** * 时间加减 * * @param day 如"2015-09-22" * @param dayAddNum 加减值 * @return */ public static String timeAddSubtract(String day, int dayAddNum) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()); try { Date newDate = new Date(simpleDateFormat.parse(day).getTime() + dayAddNum * 24 * 60 * 60 * 1000); return simpleDateFormat.format(newDate); } catch (ParseException e) { e.printStackTrace(); } return null; } /** * 毫秒格式化 * * @param millisecond 如"1449455517602" * @param format 如"yyyy-MM-dd HH:mm:ss" * @return */ public static String millisecond2String(long millisecond, String format) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.getDefault()); return simpleDateFormat.format(millisecond); }} 联系作者 我的微信公众号:吴小龙同学,欢迎关注交流,公号回复关键字「1024」有惊喜哦。