java 根据当前时间获得一周日期
生活随笔
收集整理的這篇文章主要介紹了
java 根据当前时间获得一周日期
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
//?用來全局控制?上一周,本周,下一周的周數(shù)變化
????private?static?int?weeks?=?0;
???
????//?獲得當(dāng)前日期與本周一相差的天數(shù)
????private?int?getMondayPlus()?{
????????Calendar?cd?=?Calendar.getInstance();
????????//?獲得今天是一周的第幾天,星期日是第一天,星期二是第二天......
????????int?dayOfWeek?=?cd.get(Calendar.DAY_OF_WEEK);
????????if?(dayOfWeek?==?1)?{
????????????return?-6;
????????}?else?{
????????????return?2?-?dayOfWeek;
????????}
????}
????//?獲得上周星期一的日期
????public?String?getPreviousMonday()?{
????????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?getCurrentMonday()?{
????????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?getNextMonday()?{
????????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;
????}
????//?獲得相應(yīng)周的周日的日期
????public?String?getSunday()?{
????????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;
????}
????private?static?int?weeks?=?0;
???
????//?獲得當(dāng)前日期與本周一相差的天數(shù)
????private?int?getMondayPlus()?{
????????Calendar?cd?=?Calendar.getInstance();
????????//?獲得今天是一周的第幾天,星期日是第一天,星期二是第二天......
????????int?dayOfWeek?=?cd.get(Calendar.DAY_OF_WEEK);
????????if?(dayOfWeek?==?1)?{
????????????return?-6;
????????}?else?{
????????????return?2?-?dayOfWeek;
????????}
????}
????//?獲得上周星期一的日期
????public?String?getPreviousMonday()?{
????????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?getCurrentMonday()?{
????????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?getNextMonday()?{
????????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;
????}
????//?獲得相應(yīng)周的周日的日期
????public?String?getSunday()?{
????????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;
????}
總結(jié)
以上是生活随笔為你收集整理的java 根据当前时间获得一周日期的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 各种数据库连接jdbc
- 下一篇: BaseAction