Java 获取指定日期的实现方法总结
程序员文章站
2023-12-16 11:07:16
格式化日期 string-->date 或者 data-->string
simpledateformat sdf = new simpledat...
格式化日期 string-->date 或者 data-->string simpledateformat sdf = new simpledateformat("yyyy-mm-dd"); date date = sdf.parse("2009-11-04");//string-->date string sdate = sdf.format(date );// data-->string =============================================================== package com.hefeng.test; import java.text.dateformat; import java.text.parseposition; import java.text.simpledateformat; import java.util.calendar; import java.util.date; import java.util.gregoriancalendar; public class timetest { //用来全局控制 上一周,本周,下一周的周数变化 private int weeks = 0; private int maxdate;//一月最大天数 private int maxyear;//一年最大天数 /** * @param args */ public static void main(string[] args) { timetest tt = new timetest(); system.out.println("得到6个月后的日期:"+tt .getaftermonth(6); system.out.println("获取当天日期:"+tt.getnowtime("yyyy-mm-dd")); system.out.println("获取本周一日期:"+tt.getmondayofweek()); system.out.println("获取本周日的日期~:"+tt.getcurrentweekday()); system.out.println("获取上周一日期:"+tt.getpreviousweekday()); system.out.println("获取上周日日期:"+tt.getpreviousweeksunday()); system.out.println("获取下周一日期:"+tt.getnextmonday()); system.out.println("获取下周日日期:"+tt.getnextsunday()); system.out.println("获得相应周的周六:"+tt.getnowtime("yyyy-mm-dd")); system.out.println("获取本月第一天日期:"+tt.getfirstdayofmonth()); system.out.println("获取本月最后一天日期:"+tt.getdefaultday()); system.out.println("获取上月第一天日期:"+tt.getpreviousmonthfirst()); system.out.println("获取上月最后一天的日期:"+tt.getpreviousmonthend()); system.out.println("获取下月第一天日期:"+tt.getnextmonthfirst()); system.out.println("获取下月最后一天日期:"+tt.getnextmonthend()); system.out.println("获取本年的第一天日期:"+tt.getcurrentyearfirst()); system.out.println("获取本年最后一天日期:"+tt.getcurrentyearend()); system.out.println("获取去年的第一天日期:"+tt.getpreviousyearfirst()); system.out.println("获取去年的最后一天日期:"+tt.getpreviousyearend()); system.out.println("获取明年第一天日期:"+tt.getnextyearfirst()); system.out.println("获取明年最后一天日期:"+tt.getnextyearend()); system.out.println("获取本季度第一天到最后一天:"+tt.getthisseasontime(11)); system.out.println("获取两个日期之间间隔天数2008-12-1~2008- .29:"+timetest.gettwoday("2008-12-1","2008-9-29")); } /** * 得到指定月后(前)的日期 参数传负数即可 */ public static string getaftermonth(int month) { calendar c = calendar.getinstance();//获得一个日历的实例 simpledateformat sdf = new simpledateformat("yyyy-mm-dd"); date date = null; try{ date = sdf.parse("2009-11-04");//初始日期 }catch(exception e){ } c.settime(date);//设置日历时间 c.add(calendar.month,month);//在日历的月份上增加6个月 string strdate = sdf.format(c.gettime()));//的到你想要得6个月后的日期 return strdate; } /** * 得到二个日期间的间隔天数 */ public static string gettwoday(string sj1, string sj2) { simpledateformat myformatter = new simpledateformat("yyyy-mm-dd"); long day = 0; try { java.util.date date = myformatter.parse(sj1); java.util.date mydate = myformatter.parse(sj2); day = (date.gettime() - mydate.gettime()) / (24 * 60 * 60 * 1000); } catch (exception e) { return ""; } return day + ""; } /** * 根据一个日期,返回是星期几的字符串 */ public static string getweek(string sdate) { // 再转换为时间 date date = timetest.strtodate(sdate); calendar c = calendar.getinstance(); c.settime(date); // int hour=c.get(calendar.day_of_week); // hour 中存的就是星期几了,其范围 1~7 // 1=星期日 7=星期六,其他类推 return new simpledateformat("eeee").format(c.gettime()); } /** * 将短时间格式字符串转换为时间 yyyy-mm-dd */ public static date strtodate(string strdate) { simpledateformat formatter = new simpledateformat("yyyy-mm-dd"); parseposition pos = new parseposition(0); date strtodate = formatter.parse(strdate, pos); return strtodate; } /** * 两个时间之间的天数 * * @param date1 * @param date2 * @return */ public static long getdays(string date1, string date2) { if (date1 == null || date1.equals("")) return 0; if (date2 == null || date2.equals("")) return 0; // 转换为标准时间 simpledateformat myformatter = new simpledateformat("yyyy-mm-dd"); java.util.date date = null; java.util.date mydate = null; try { date = myformatter.parse(date1); mydate = myformatter.parse(date2); } catch (exception e) { } long day = (date.gettime() - mydate.gettime()) / (24 * 60 * 60 * 1000); return day; } // 计算当月最后一天,返回字符串 public string getdefaultday(){ string str = ""; simpledateformat sdf=new simpledateformat("yyyy-mm-dd"); calendar lastdate = calendar.getinstance(); lastdate.set(calendar.date,1);//设为当前月的1 号 lastdate.add(calendar.month,1);//加一个月,变为下月的1 号 lastdate.add(calendar.date,-1);//减去一天,变为当月最后一天 str=sdf.format(lastdate.gettime()); return str; } // 上月第一天 public string getpreviousmonthfirst(){ string str = ""; simpledateformat sdf=new simpledateformat("yyyy-mm-dd"); calendar lastdate = calendar.getinstance(); lastdate.set(calendar.date,1);//设为当前月的1 号 lastdate.add(calendar.month,-1);//减一个月,变为下月的1 号 //lastdate.add(calendar.date,-1);//减去一天,变为当月最后一天 str=sdf.format(lastdate.gettime()); return str; } //获取当月第一天 public string getfirstdayofmonth(){ string str = ""; simpledateformat sdf=new simpledateformat("yyyy-mm-dd"); calendar lastdate = calendar.getinstance(); lastdate.set(calendar.date,1);//设为当前月的1 号 str=sdf.format(lastdate.gettime()); return str; } // 获得本周星期日的日期 public string getcurrentweekday() { weeks = 0; int mondayplus = this.getmondayplus(); gregoriancalendar currentdate = new gregoriancalendar(); currentdate.add(gregoriancalendar.date, mondayplus+6); date monday = currentdate.gettime(); dateformat df = dateformat.getdateinstance(); string premonday = df.format(monday); return premonday; } //获取当天时间 public string getnowtime(string dateformat){ date now = new date(); simpledateformat dateformat = new simpledateformat(dateformat);//可 以方便 地修改日期格式 string hehe = dateformat.format(now); return hehe; } // 获得当前日期与本周日相差的天数 private int getmondayplus() { calendar cd = calendar.getinstance(); // 获得今天是一周的第几天,星期日是第一天,星期二是第二天...... int dayofweek = cd.get(calendar.day_of_week)-1; //因为按中国礼拜一 作为第一 天所以这里减1 if (dayofweek == 1) { return 0; } else { return 1 - dayofweek; } } //获得本周一的日期 public string getmondayofweek(){ weeks = 0; int mondayplus = this.getmondayplus(); gregoriancalendar currentdate = new gregoriancalendar(); currentdate.add(gregoriancalendar.date, mondayplus); date monday = currentdate.gettime(); dateformat df = dateformat.getdateinstance(); string premonday = df.format(monday); return premonday; } //获得相应周的周六的日期 public string getsaturday() { int mondayplus = this.getmondayplus(); gregoriancalendar currentdate = new gregoriancalendar(); currentdate.add(gregoriancalendar.date, mondayplus + 7 * weeks + 6); date monday = currentdate.gettime(); dateformat df = dateformat.getdateinstance(); string premonday = df.format(monday); return premonday; } // 获得上周星期日的日期 public string getpreviousweeksunday() { weeks=0; weeks--; int mondayplus = this.getmondayplus(); gregoriancalendar currentdate = new gregoriancalendar(); currentdate.add(gregoriancalendar.date, mondayplus+weeks); date monday = currentdate.gettime(); dateformat df = dateformat.getdateinstance(); string premonday = df.format(monday); return premonday; } // 获得上周星期一的日期 public string getpreviousweekday() { weeks--; int mondayplus = this.getmondayplus(); gregoriancalendar currentdate = new gregoriancalendar(); currentdate.add(gregoriancalendar.date, mondayplus + 7 * weeks); date monday = currentdate.gettime(); dateformat df = dateformat.getdateinstance(); string premonday = df.format(monday); return premonday; } // 获得下周星期一的日期 public string getnextmonday() { weeks++; int mondayplus = this.getmondayplus(); gregoriancalendar currentdate = new gregoriancalendar(); currentdate.add(gregoriancalendar.date, mondayplus + 7); date monday = currentdate.gettime(); dateformat df = dateformat.getdateinstance(); string premonday = df.format(monday); return premonday; } // 获得下周星期日的日期 public string getnextsunday() { int mondayplus = this.getmondayplus(); gregoriancalendar currentdate = new gregoriancalendar(); currentdate.add(gregoriancalendar.date, mondayplus + 7+6); date monday = currentdate.gettime(); dateformat df = dateformat.getdateinstance(); string premonday = df.format(monday); return premonday; } private int getmonthplus(){ calendar cd = calendar.getinstance(); int monthofnumber = cd.get(calendar.day_of_month); cd.set(calendar.date, 1);//把日期设置为当月第一天 cd.roll(calendar.date, -1);//日期回滚一天,也就是最后一天 maxdate=cd.get(calendar.date); if(monthofnumber == 1){ return -maxdate; }else{ return 1-monthofnumber; } } //获得上月最后一天的日期 public string getpreviousmonthend(){ string str = ""; simpledateformat sdf=new simpledateformat("yyyy-mm-dd"); calendar lastdate = calendar.getinstance(); lastdate.add(calendar.month,-1);//减一个月 lastdate.set(calendar.date, 1);//把日期设置为当月第一天 lastdate.roll(calendar.date, -1);//日期回滚一天,也就是本月最后一天 str=sdf.format(lastdate.gettime()); return str; } //获得下个月第一天的日期 public string getnextmonthfirst(){ string str = ""; simpledateformat sdf=new simpledateformat("yyyy-mm-dd"); calendar lastdate = calendar.getinstance(); lastdate.add(calendar.month,1);//减一个月 lastdate.set(calendar.date, 1);//把日期设置为当月第一天 str=sdf.format(lastdate.gettime()); return str; } //获得下个月最后一天的日期 public string getnextmonthend(){ string str = ""; simpledateformat sdf=new simpledateformat("yyyy-mm-dd"); calendar lastdate = calendar.getinstance(); lastdate.add(calendar.month,1);//加一个月 lastdate.set(calendar.date, 1);//把日期设置为当月第一天 lastdate.roll(calendar.date, -1);//日期回滚一天,也就是本月最后一天 str=sdf.format(lastdate.gettime()); return str; } //获得明年最后一天的日期 public string getnextyearend(){ string str = ""; simpledateformat sdf=new simpledateformat("yyyy-mm-dd"); calendar lastdate = calendar.getinstance(); lastdate.add(calendar.year,1);//加一个年 lastdate.set(calendar.day_of_year, 1); lastdate.roll(calendar.day_of_year, -1); str=sdf.format(lastdate.gettime()); return str; } //获得明年第一天的日期 public string getnextyearfirst(){ string str = ""; simpledateformat sdf=new simpledateformat("yyyy-mm-dd"); calendar lastdate = calendar.getinstance(); lastdate.add(calendar.year,1);//加一个年 lastdate.set(calendar.day_of_year, 1); str=sdf.format(lastdate.gettime()); return str; } //获得本年有多少天 private int getmaxyear(){ calendar cd = calendar.getinstance(); cd.set(calendar.day_of_year,1);//把日期设为当年第一天 cd.roll(calendar.day_of_year,-1);//把日期回滚一天。 int maxyear = cd.get(calendar.day_of_year); return maxyear; } private int getyearplus(){ calendar cd = calendar.getinstance(); int yearofnumber = cd.get(calendar.day_of_year);//获得当天是一年中的第几天 cd.set(calendar.day_of_year,1);//把日期设为当年第一天 cd.roll(calendar.day_of_year,-1);//把日期回滚一天。 int maxyear = cd.get(calendar.day_of_year); if(yearofnumber == 1){ return -maxyear; }else{ return 1-yearofnumber; } } //获得本年第一天的日期 public string getcurrentyearfirst(){ int yearplus = this.getyearplus(); gregoriancalendar currentdate = new gregoriancalendar(); currentdate.add(gregoriancalendar.date,yearplus); date yearday = currentdate.gettime(); dateformat df = dateformat.getdateinstance(); string preyearday = df.format(yearday); return preyearday; } //获得本年最后一天的日期 * public string getcurrentyearend(){ date date = new date(); simpledateformat dateformat = new simpledateformat("yyyy");//可以方 便地修 改日期格式 string years = dateformat.format(date); return years+"-12-31"; } //获得上年第一天的日期 * public string getpreviousyearfirst(){ date date = new date(); simpledateformat dateformat = new simpledateformat("yyyy");//可以方 便地修 改日期格式 string years = dateformat.format(date); int years_value = integer.parseint (years); years_value--; return years_value+"-1-1"; } //获得上年最后一天的日期 public string getpreviousyearend(){ weeks--; int yearplus = this.getyearplus(); gregoriancalendar currentdate = new gregoriancalendar(); currentdate.add(gregoriancalendar.date,yearplus+maxyear*weeks+(maxyear-1)); date yearday = currentdate.gettime(); dateformat df = dateformat.getdateinstance(); string preyearday = df.format(yearday); getthisseasontime(11); return preyearday; } //获得本季度 public string getthisseasontime(int month){ int array[][] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12}}; int season = 1; if(month>=1&&month<=3){ season = 1; } if(month>=4&&month<=6){ season = 2; } if(month>=7&&month<=9){ season = 3; } if(month>=10&&month<=12){ season = 4; } int start_month = array[season-1][0]; int end_month = array[season-1][2]; date date = new date(); simpledateformat dateformat = new simpledateformat("yyyy");//可以方 便地修 改日期格式 string years = dateformat.format(date); int years_value = integer.parseint(years); int start_days =1;//years+"-"+string.valueof(start_month)+"- ";//getlastdayofmonth(years_value,start_month); int end_days = getlastdayofmonth(years_value,end_month); string seasondate = years_value+"-"+start_month+"-"+start_days+";"+years_value+"-"+end_month+"-"+end_days; return seasondate; } /** * 获取某年某月的最后一天 * @param year 年 * @param month 月 * @return 最后一天 */ private int getlastdayofmonth(int year, int month) { if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { return 31; } if (month == 4 || month == 6 || month == 9 || month == 11) { return 30; } if (month == 2) { if (isleapyear(year)) { return 29; } else { return 28; } } return 0; } /** * 是否闰年 * @param year 年 * @return */ public boolean isleapyear(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); }