求教Java 获得当前日期的上月时间

求教Java 获得当前日期的上月时间,第1张

利用Calendar类,示例代码如下:

public class Main {

    public static void main(String[] args) {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");

        Date date = new Date();

        Systemoutprintln("当前时间是:" + dateFormatformat(date));

        Calendar calendar = CalendargetInstance();

        calendarsetTime(date); // 设置为当前时间

        calendarset(CalendarMONTH, calendarget(CalendarMONTH) - 1); // 设置为上一个月

        date = calendargetTime();

        Systemoutprintln("上一个月的时间: " + dateFormatformat(date));

    }

}

输出结果:

SimpleDateFormat fmt =new SimpleDateFormat("yyyy-MM-dd");

Date cDate = null;

// 与当前日期相差的天数,如果为+的话则当前日期往后+days天,否则往前-days天

int days = -7;

try {

String pattern = "yyyy-MM-dd";//获取的日期格式

SimpleDateFormat df = new SimpleDateFormat(pattern);

Date today = new Date();//获取当前日期

String currentDate = dfformat(today);//获取当前日期的字符串

Calendar cal=CalendargetInstance();

cDate = fmtparse(currentDate);

calsetTime(cDate);

caladd(CalendarDAY_OF_YEAR,days); //将日期+days获取你想要的日期

currentDate = fmtformat(calgetTime());

Systemoutprintln(currentDate);

} catch (ParseException e) {

eprintStackTrace();

}

//0、日期输出格式

SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd,HH:mm:ss");

//1、获取当前日期 方式一

Date b = new Date();

Systemoutprintln(fformat(b));

//2、获取当前日期 方式二

Calendar c = CalendargetInstance();

//可以手动设置日期

//cset(2011, CalendarJANUARY, 31);

Systemoutprintln(fformat(cgetTime()));

//3、获取当前日期增加两个月后的日期,

cadd(CalendarMONTH, 2);

Systemoutprintln(fformat(cgetTime()));

var myDate = new Date();

myDatetoLocaleDateString();可以获取当前日期

myDatetoLocaleTimeString(); 可以获取当前时间

扩展:

myDategetYear(); //获取当前年份(2位)

myDategetFullYear(); //获取完整的年份(4位,1970-)

myDategetMonth(); //获取当前月份(0-11,0代表1月)

myDategetDate(); //获取当前日(1-31)

myDategetDay(); //获取当前星期X(0-6,0代表星期天)

myDategetTime(); //获取当前时间(从197011开始的毫秒数)

myDategetHours(); //获取当前小时数(0-23)

myDategetMinutes(); //获取当前分钟数(0-59)

myDategetSeconds(); //获取当前秒数(0-59)

myDategetMilliseconds(); //获取当前毫秒数(0-999)

myDatetoLocaleString( ); //获取日期与时间

获取本周一

 public static Date getNowWeekMonday(Date date) {    

            Calendar cal = CalendargetInstance();    

             calsetTime(date);    

                  

             caladd(CalendarDAY_OF_MONTH, -1); //解决周日会出现 并到下一周的情况    

            calset(CalendarDAY_OF_WEEK, CalendarMONDAY);    

                 

303            return calgetTime();    

        }

获取上周一

public static Date getLastWeekMonday(Date date) {    

             Date a = DateUtilsaddDays(date, -1);    

            Calendar cal = CalendargetInstance();    

            calsetTime(a);    

            caladd(CalendarWEEK_OF_YEAR, -1);// 一周    

            calset(CalendarDAY_OF_WEEK, CalendarMONDAY);    

                  

             return calgetTime();    

        }

获取上周日

public static Date  getLastWeekSunday(Date date) {    

                 

            Date a = DateUtilsaddDays(date, -1);    

           Calendar cal = CalendargetInstance();    

         calsetTime(a);    

           calset(CalendarDAY_OF_WEEK, 1);    

                 

          return calgetTime();    

        }

代码里面有用到 lapachecommon-ang包 你需要下载下 就可以使用

迟来的答案

1周末版本(不含节假日判断)

注意:最下面是使用的 递归算法

/

  获得收益时间(获取当前天+1天,周末不算)

 

  @param date

             任意日期

  @return the income date

  @throws NullPointerException

              if null == date

 /

private Date getIncomeDate(Date date) throws NullPointerException{

if (null == date){

throw new NullPointerException("the date is null or empty!");

}

//对日期的 *** 作,我们需要使用 Calendar 对象

Calendar calendar = new GregorianCalendar();

calendarsetTime(date);

//+1天

calendaradd(CalendarDAY_OF_MONTH, +1);

//判断是星期几

int dayOfWeek = calendarget(CalendarDAY_OF_WEEK);

Date incomeDate = calendargetTime();

if (dayOfWeek == 1 || dayOfWeek == 7){

//递归

return getIncomeDate(incomeDate);

}

return incomeDate;

}

测试方法:

@Test

public void testGetIncomeDate() throws ParseException{

String pattern = "yyyy-MM-dd HH:mm:ss";

SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

Systemoutprintln(simpleDateFormatformat(getIncomeDate(new Date())));

Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-07-31 13:33:05"))));

Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-08-01 13:33:05"))));

Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-08-02 13:33:05"))));

Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-08-03 13:33:05"))));

}

输出结果:

2014-08-01 13:48:09

2014-08-01 13:33:05

2014-08-04 13:33:05

2014-08-04 13:33:05

2014-08-04 13:33:05

注意:返回的是 时间+1的时间,精度是到毫秒 纳秒,如果有特殊需求,需要自己再处理下

2周末+节假日版本

/

  获得收益时间(获取当前天+1天,周末不算)

 

  @param date

             任意日期

  @return the income date

  @throws NullPointerException

              if null == date

 /

private Date getIncomeDate(Date date) throws NullPointerException{

if (null == date){

throw new NullPointerException("the date is null or empty!");

}

//对日期的 *** 作,我们需要使用 Calendar 对象

Calendar calendar = new GregorianCalendar();

calendarsetTime(date);

//+1天

calendaradd(CalendarDAY_OF_MONTH, +1);

Date incomeDate = calendargetTime();

if (isWeekend(calendar) || isHoliday(calendar)){

//递归

return getIncomeDate(incomeDate);

}

return incomeDate;

}

/

  判断一个日历是不是周末

 

  @param calendar

             the calendar

  @return true, if checks if is weekend

 /

private boolean isWeekend(Calendar calendar){

//判断是星期几

int dayOfWeek = calendarget(CalendarDAY_OF_WEEK);

if (dayOfWeek == 1 || dayOfWeek == 7){

return true;

}

return false;

}

/

  一个日历是不是节假日

 

  @param calendar

             the calendar

  @return true, if checks if is holiday

 /

private boolean isHoliday(Calendar calendar){

String pattern = "yyyy-MM-dd";

SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

String dateString = simpleDateFormatformat(calendargetTime());

//节假日 这个可能不同地区,不同年份 都有可能不一样,所以需要有个地方配置, 可以放数据库, 配置文件,环境变量 等等地方

//这里以配置文件 为例子

ResourceBundle resourceBundle = ResourceBundlegetBundle("holidayConfig");

String holidays = resourceBundlegetString("holiday");

String[] holidayArray = holidayssplit(",");

boolean isHoliday = orgapachecommonslangArrayUtilscontains(holidayArray, dateString);

return isHoliday;

}

配置文件:

holiday=2014-10-01,2014-10-02,2014-10-03,2014-10-04,2014-10-05,2014-10-06,2014-10-07

测试方法 :

/

  Testenclosing_type

 

  @throws ParseException

              the parse exception

 /

@Test

public void testGetIncomeDate() throws ParseException{

String pattern = "yyyy-MM-dd HH:mm:ss";

SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

Systemoutprintln(simpleDateFormatformat(getIncomeDate(new Date())));

Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-07-31 13:33:05"))));

Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-08-01 13:33:05"))));

Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-08-02 13:33:05"))));

Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-08-03 13:33:05"))));

Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-09-30 13:33:05"))));

Systemoutprintln(simpleDateFormatformat(getIncomeDate(simpleDateFormatparse("2014-10-02 13:33:05"))));

}

结果:

2014-08-01 15:14:59

2014-08-01 13:33:05

2014-08-04 13:33:05

2014-08-04 13:33:05

2014-08-04 13:33:05

2014-10-08 13:33:05

2014-10-08 13:33:05

int y,m,d,h,mi,s;

Calendar cal=CalendargetInstance();

y=calget(CalendarYEAR);

m=calget(CalendarMONTH);

d=calget(CalendarDATE);

h=calget(CalendarHOUR_OF_DAY);

mi=calget(CalendarMINUTE);

s=calget(CalendarSECOND);

Systemoutprintln("现在时刻是"+y+"年"+m+"月"+d+"日"+h+"时"+mi+"分"+s+"秒");

以上就是关于求教Java 获得当前日期的上月时间全部的内容,包括:求教Java 获得当前日期的上月时间、java 获得当前日期,而不要当前时间的代码、java如何获取当前日期并计算出2个月后的日期等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9795410.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-02
下一篇2023-05-02

发表评论

登录后才能评论

评论列表(0条)

    保存