Java实现的日期处理类完整实例
程序员文章站
2024-02-28 19:51:28
本文实例讲述了java实现的日期处理类。分享给大家供大家参考,具体如下:
开发中常常要使用日期,先小结如下,以备后用。
import java.text.par...
本文实例讲述了java实现的日期处理类。分享给大家供大家参考,具体如下:
开发中常常要使用日期,先小结如下,以备后用。
import java.text.parseexception; import java.text.simpledateformat; import java.util.calendar; import java.util.date; public class formattime { private final static simpledateformat sdf=new simpledateformat("yyyymmdd"); private final static simpledateformat sdfymdhm = new simpledateformat("yyyymmddhhmmss"); private final static simpledateformat sdfymdhms = new simpledateformat("yyyy-mm-dd hh:mm:ss"); /** * * @title: getcurrentday * @description: todo 获取当天时间(20161109) * @return */ public static string getcurrentday(){ return sdf.format(new date()); } /** * * @title: ftime2 * @description: todo 获取time这个日期以前dayago天的日期 * @return */ public static string ftime(string time,int dayago){ date date = null; try { date = sdf.parse(time); } catch (parseexception e) { throw new runtimeexception(e); } calendar calendar = calendar.getinstance(); calendar.settime(date); if(dayago>0){ calendar.add(calendar.day_of_month, -dayago);//前15天数据 date = calendar.gettime(); calendar.settime(date); } int year=calendar.get(calendar.year); int month=calendar.get(calendar.month) + 1; int day = calendar.get(calendar.day_of_month); string mon=""; string d=""; if(month<10){ mon="0"+month; }else{ mon=month+""; } if(day<10){ d="0"+day; }else{ d=""+day; } string ret=year+""+mon+""+d; return ret; } /** * * @title: ftime2 * @description: todo 获取time这个日期以后dayafter天的日期 * @return */ public static string ftime2(string time,int dayafter){ date date = null; try { date = sdf.parse(time); } catch (parseexception e) { throw new runtimeexception(e); } calendar calendar = calendar.getinstance(); calendar.settime(date); if(dayafter>0){ calendar.add(calendar.day_of_month, +dayafter);//后15天数据 date = calendar.gettime(); calendar.settime(date); } int year=calendar.get(calendar.year); int month=calendar.get(calendar.month) + 1; int day = calendar.get(calendar.day_of_month); string mon=""; string d=""; if(month<10){ mon="0"+month; }else{ mon=month+""; } if(day<10){ d="0"+day; }else{ d=""+day; } string ret=year+""+mon+""+d; return ret; } /** * * @title: getdefaulttime * @description: todo 获取昨天的日期 * @return */ public static string getdefaulttime(){ calendar calendar = calendar.getinstance(); calendar.add(calendar.day_of_month, -1);//前1天 date date = calendar.gettime(); string time=sdf.format(date); return time; } /** * * @title: getsunday * @description: todo 获取最近一个星期天 * @return */ public static string getsunday(){ simpledateformat f = new simpledateformat("yyyymmdd"); calendar c = calendar.getinstance(); c.set(calendar.day_of_week, calendar.sunday); return f.format(c.gettime()); } /** * * @title: getmonthfirstday * @description: todo 获取本月第一天 * @return */ public static string getcurrentmonthfirstday(){ calendar cal_1=calendar.getinstance();//获取当前日期 cal_1.add(calendar.month, 0); cal_1.set(calendar.day_of_month,1);//设置为1号,当前日期既为本月第一天 string firstday = sdf.format(cal_1.gettime()); return firstday; } /** * * @title: getmonthfirstday * @description: todo 获取上月第一天 * @return */ public static string getpreviousmonthfirstday(){ //获取当前月第一天: calendar c = calendar.getinstance(); c.add(calendar.month, -1); c.set(calendar.day_of_month,1);//设置为1号,当前日期既为本月第一天 string first = sdf.format(c.gettime()); return first; } /** * * @title: getmonthfirstday * @description: todo 获取上月最后一天 * @return */ public static string getpreviousmonthlastday(){ //获取当前月最后一天 calendar ca = calendar.getinstance(); ca.set(calendar.day_of_month,0);// string lastday = sdf.format(ca.gettime()); return lastday; } /** * * @title: getcurrentmonthlastday * @description: todo 获取指定时间最后一天 * @return */ public static string getcurrentmonthlastday(string time){ date date =null; try { date= sdf.parse(time); } catch (parseexception e) { // todo auto-generated catch block e.printstacktrace(); } //获取当前月最后一天 calendar ca = calendar.getinstance(); ca.settime(date); ca.set(calendar.day_of_month, ca.getactualmaximum(calendar.day_of_month)); // string lastday = sdf.format(ca.gettime()); return lastday; } /*** * * @title: getcurrentweekday * @description: todo 获取本周周一 */ public static string getcurrentmonday(){ calendar cal = calendar.getinstance(); cal.setfirstdayofweek(calendar.monday);//将每周第一天设为星期一,默认是星期天 cal.add(calendar.date, 0); cal.set(calendar.day_of_week,calendar.monday); string monday = sdf.format(cal.gettime()); return monday; } /*** * * @title: getprevioussunday * @description: todo 获取上周周日 */ public static string getprevioussunday(){ calendar cal = calendar.getinstance(); cal.setfirstdayofweek(calendar.monday);//将每周第一天设为星期一,默认是星期天 cal.add(calendar.date, -1*7); cal.set(calendar.day_of_week, calendar.sunday); string sunday =sdf.format(cal.gettime()); return sunday; } /** * * @title: getminisencond * @description: todo 将日期转换为毫秒数 * @param str * @return */ public static string getminisencond(string str){ long millionseconds=0; try { millionseconds = sdfymdhm.parse(str).gettime();//毫秒 } catch (parseexception e) { // todo auto-generated catch block e.printstacktrace(); } return millionseconds+""; } /** * * @title: getdatesencond * @description: todo 将日期转换为毫秒数 * @param str * @return */ public static long getdatesencond(string str){ long millionseconds=0; try { millionseconds = sdfymdhms.parse(str).gettime();//毫秒 } catch (parseexception e) { // todo auto-generated catch block e.printstacktrace(); } return millionseconds; } /** * 计算日期相差天数 * @param str1 * @param str2 * @return */ public static int getdistanceoftwodate(string str1,string str2){ int result=0; try{ date date1 = sdf.parse(str1); date date2 =sdf.parse(str2); calendar acalendar = calendar.getinstance(); acalendar.settime(date1); int day1 = acalendar.get(calendar.day_of_year); acalendar.settime(date2); int day2 = acalendar.get(calendar.day_of_year); result = day1-day2; }catch(exception e){ e.printstacktrace(); } return result; } /** * * @title: long2date * @description: todo long 转日期(年-月-日 时-分-秒) * @param timestamp * @return */ public static string longtodate(long msecond){ date date = new date(msecond); return sdfymdhms.format(date); } }
ps:这里再为大家推荐几款关于日期与天数计算的在线工具供大家使用:
在线日期/天数计算器:
在线万年历日历:
在线阴历/阳历转换工具:
更多关于java相关内容感兴趣的读者可查看本站专题:《java日期与时间操作技巧汇总》、《java数据结构与算法教程》、《java操作dom节点技巧总结》和《java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。