时间日期工具类

来源:互联网 发布:部落冲突地震数据 编辑:程序博客网 时间:2024/05/18 09:06

平时经常会用到的日期时间工具类


/** * 时间日期工具 */public class DateUtils {/** * 获取当前月份,0當月,1下月 */public static String getMonth(int day) {long time = System.currentTimeMillis();SimpleDateFormat sdf = new SimpleDateFormat("MM");int i = Integer.parseInt(sdf.format(time));if (day == 1) {i++;if (i > 12) {i = 1;}}String month = "";switch (i) {case 1:return "一月";case 2:return "二月";case 3:return "三月";case 4:return "四月";case 5:return "五月";case 6:return "六月";case 7:return "七月";case 8:return "八月";case 9:return "九月";case 10:return "十月";case 11:return "十一月";case 12:return "十二月";default:return "";}}/** * 获取当月的 天数,0当月,1下月 */public static int getTheMonthDay(int month) {Calendar a = Calendar.getInstance();if (month == 1) {long time = System.currentTimeMillis();SimpleDateFormat sdf = new SimpleDateFormat("MM");int i = Integer.parseInt(sdf.format(time));if (i > 12) {i = 1;}a.set(Calendar.MONTH, i);}a.set(Calendar.DATE, 1);// 把日期设置为当月第一天a.roll(Calendar.DATE, -1);// 日期回滚一天,也就是最后一天int maxDate = a.get(Calendar.DATE);return maxDate;}/** * 获取今天的日期,0表示今天,1表示明天,-1表示昨天 返回实例:29 */public static int getDay(int day) {long time = System.currentTimeMillis() + (day * 24 * 60 * 60 * 1000);SimpleDateFormat sdf = new SimpleDateFormat("dd");return Integer.parseInt(sdf.format(time));}/** * 获取今天的日期,0表示今天,1表示明天,-1表示昨天 返回实例:2015-10-10 */public static String getYearAndDay(int day) {long time = System.currentTimeMillis() + (day * 24 * 60 * 60 * 1000);SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");return sdf.format(time);}/** * 获取今天的日期,0表示今天,1表示明天,-1表示昨天 返回实例:9.2 */public static String getToDay(int day) {long time = System.currentTimeMillis() + (day * 24 * 60 * 60 * 1000);SimpleDateFormat month = new SimpleDateFormat("MM");SimpleDateFormat day1 = new SimpleDateFormat("dd");return Integer.parseInt(month.format(time)) + "." + Integer.parseInt(day1.format(time));}/** * 获取今天是星期几 */public static int getToDayOfWeek() {Calendar calendar = Calendar.getInstance();int day = calendar.get(Calendar.DAY_OF_WEEK);if (day == 1) {day = 7;} else {day = day - 1;}return day;}/** * 获取本周的日期数组 返回实例{9.2,9.3,9.4~~~} */public static String[] getToWeek() {// int day = getToDayOfWeek();int[] ints = { 0, 1, 2, 3, 4, 5, 6 };String[] weeks = new String[7];for (int i = 0; i < ints.length; i++) {// ints[i] -= day;weeks[i] = getToDay(ints[i]);}return weeks;}/** * 根据最后签到日期以及连续签到天数获取签到7天日期数据 */public static String[] getSigninList(String day, int ConNum) {long time = getTimeForDay(day);int[] ints = { 1, 2, 3, 4, 5, 6, 7 };String[] weeks = new String[7];for (int i = 0; i < ints.length; i++) {ints[i] -= ConNum;weeks[i] = getToDay(time, ints[i]);}return weeks;}public static long getTimeForDay(String day) {java.sql.Date bb = java.sql.Date.valueOf(day);Calendar date = Calendar.getInstance();date.setTime(bb);date.set(date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DAY_OF_MONTH), 0, 0, 0);date.set(Calendar.MILLISECOND, 0);return date.getTime().getTime();}/** * 获取指定的日期前后的日期,0表示今天,1表示明天,-1表示昨天 返回实例:9.2 */public static String getToDay(long time2, int day) {long time = time2 + (day * 24 * 60 * 60 * 1000);SimpleDateFormat month = new SimpleDateFormat("MM");SimpleDateFormat day1 = new SimpleDateFormat("dd");return Integer.parseInt(month.format(time)) + "." + Integer.parseInt(day1.format(time));}/** * 日期大小比较 */public static int compare_date(String DATE1, String DATE2) {SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd\tHH:mm:ss");try {Date d1 = df.parse(DATE1);Date d2 = df.parse(DATE2);// 获取到差额,负数表示过去long diff = d1.getTime() - d2.getTime();if (diff < 0) {// 时间在过去return -1;}// 相差的分钟数long days = diff / (1000 * 60 * 60 * 24);if (days >= 0) {// 相差0或者几分钟return 1;} else {return -1;}} catch (Exception exception) {exception.toString();}return 0;}/** * 日期大小比较,是否已经超过day天 * DATE2是最新的日期 */public static boolean compare_date_15(String DATE1, String DATE2,long day) {SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");try {Date d1 = df.parse(DATE1);Date d2 = df.parse(DATE2);// 获取到差额,负数表示过去long diff = d2.getTime() - d1.getTime();if ((diff - (24 * 60 * 60 * 1000 * day)) < 0) {// 没超过day天return false;} else {return true;}} catch (Exception exception) {exception.toString();}return false;}/** * 返回年月日 *  * @param time * @return yyyy-MM-dd HH:mm:ss */public static String getFormatDate(long time) {SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = new Date(time);String startDateString = format1.format(date);return startDateString;}/** * 返回年月日 *  * @param time * @return yyyy-MM-dd HH:mm:ss */public static String getFormatDate2(long time) {SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");Date date = new Date(time);String startDateString = format1.format(date);return startDateString;}/** * 返回年月日 *  * @param time * @return yyyy-MM-dd HH:mm:ss */public static String getFormatDate3(long time) {SimpleDateFormat format1 = new SimpleDateFormat("MM-dd HH:mm");Date date = new Date(time);String startDateString = format1.format(date);return startDateString;}/** * 返回月日 *  * @param time * @return MM月dd日 */public static String getFormatDate1(long time) {SimpleDateFormat format1 = new SimpleDateFormat("MM月dd日");Date date = new Date(time);String startDateString = format1.format(date);return startDateString;}/** * 返回月日0是今天,1是明天 *  * @param time * @return MM月dd日 */public static String getFormatDate1(int day) {long time = System.currentTimeMillis() + (day * 24 * 60 * 60 * 1000);SimpleDateFormat format1 = new SimpleDateFormat("MM月dd日");Date date = new Date(time);String startDateString = format1.format(date);return startDateString;}public static long getFormatToLong(String str) {try {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");long millionSeconds = sdf.parse(str).getTime();return millionSeconds;} catch (ParseException e) {return 0;}}/**传入一个float将数据变成想要的数据 * 先除以2 * 然后把2.4变成2.5,2.7变成3; * */public static float getFloat(float f){f=f/2;//float newf=f-(int)f;//if(newf==0||newf==0.5){//return f;//}else if(newf<0.5&&newf>0){//f=(float) ((int)f+0.5);//}else{//f=(int)f+1;//}return f;}/** * 格式转换 * 传入yyyy-MM-dd hh:mm:ss格式的时间,转换为MM-dd HH:mm格式 * */public static String ChangeDate(String day){SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");long time=0;try {time=df.parse(day).getTime();} catch (ParseException e) {e.printStackTrace();}return getFormatDate3(time);}}


0 0
原创粉丝点击