博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[SoapUI] 各种日期计算
阅读量:5367 次
发布时间:2019-06-15

本文共 17866 字,大约阅读时间需要 59 分钟。

import java.util.*;import java.text.SimpleDateFormat;// current dateString dateNew = today()// tomorrowString tomorrow = tomorrow()//  yesterdayString yesterday = yesterday()// the day before yesterdayString theDayBeforeYesterday = theDayBeforeYesterday()// Last Market CloseString lastMarketClose = lastMarketClose()log.info "------> lastMarketClose : "+lastMarketClose// Last Market Close -2 daysString lastMarketCloseMinus2Days = lastMarketCloseMinus2Days()// Two years ago of Last Market Close -2 daysString twoYearsAgoOfLastMarketCloseMinus2Days = twoYearsAgoOfLastMarketCloseMinus2Days(lastMarketCloseMinus2Days)// yesterday of Last Market CloseString yesterdayOfLastMarketClose = yesterdayOfLastMarketClose(lastMarketClose)// a month ago of Last Market CloseString aMonthAgoOfLastMarketClose = aMonthAgoOfLastMarketClose(lastMarketClose)// three months ago of Last Market CloseString threeMonthsAgoOfLastMarketClose = threeMonthsAgoOfLastMarketClose(lastMarketClose)// three years ago of Last Market CloseString threeYearsAgoOfLastMarketClose = threeYearsAgoOfLastMarketClose(lastMarketClose)log.info "------> threeYearsAgoOfLastMarketClose : "+threeYearsAgoOfLastMarketClose// the last day of last weekString theLastDayOfLastWeek = theLastDayOfLastWeek()// the last day of  last monthString theLastDayOfLastMonth = theLastDayOfLastMonth()// Get the same day of last monthString theSameDayOfLastMonth = theSameDayOfLastMonth()// The first day of the quarterString theFirstDayOfTheQuarter = theFirstDayOfTheQuarter()// The last day of last quarterString theLastDayOfLastQuarter = theLastDayOfLastQuarter(theFirstDayOfTheQuarter)//  a year ago after The last day of last quarterString aYearAgoAfterTheLastDayOfLastQuarter = aYearAgoAfterTheLastDayOfLastQuarter(theFirstDayOfTheQuarter)// The first day of last quarterString theFirstDayOfLastQuarter = theFirstDayOfLastQuarter(theFirstDayOfTheQuarter)// The last day of two quarters agoString theLastDayOfTwoQuartersAgo = theLastDayOfTwoQuartersAgo(theLastDayOfLastQuarter)// The first day of six months ago after two quarters String theFirstDayOfSixMonthsAgoAfterTwoQuarters = theFirstDayOfSixMonthsAgoAfterTwoQuarters(theLastDayOfTwoQuartersAgo)// yesterday of  three months agoString yesterdayOfThreeMonthsAgo = yesterdayOfThreeMonthsAgo()// The first day of  two months agoString theFirstDayOfTwoMonthsAgo = theFirstDayOfTwoMonthsAgo()// The last day of  two months agoString theLastDayOfTwoMonthsAgo = theLastDayOfTwoMonthsAgo()// The first day of  three months agoString theFirstDayOfThreeMonthsAgo = theFirstDayOfThreeMonthsAgo()// The first day of  six months agoString theFirstDayOfSixMonthsAgo = theFirstDayOfSixMonthsAgo()// The first day of this yearString theFirstDayOfThisYear = theFirstDayOfThisYear()// The last day of last yearString theLastDayOfLastYear = theLastDayOfLastYear()// tomorrow of  last yearString tomorrowOfLastYear = tomorrowOfLastYear()// the first day of this month last yearString theFirstDayOfThisMonthLastYear = theFirstDayOfThisMonthLastYear()// the first day of last yearString theFirstDayOfLastYear = theFirstDayOfLastYear()// the same day of this month three years agoString theSameDayOfThisMonthThreeYearsAgo = theSameDayOfThisMonthThreeYearsAgo()// the first day of this month three years agoString theFirstDayOfThisMonthThreeYearsAgo = theFirstDayOfThisMonthThreeYearsAgo()// the first day of three years agoString theFirstDayOfThreeYearsAgo = theFirstDayOfThreeYearsAgo()// Set the project level property : currentDate,yesterDay,lastMonth , its value will updated with dateNewtestRunner.testCase.testSuite.project.setPropertyValue( "currentDate", dateNew )testRunner.testCase.testSuite.project.setPropertyValue( "tomorrow", tomorrow )testRunner.testCase.testSuite.project.setPropertyValue( "yesterday", yesterday )testRunner.testCase.testSuite.project.setPropertyValue( "theDayBeforeYesterday", theDayBeforeYesterday )testRunner.testCase.testSuite.project.setPropertyValue( "lastMarketClose", lastMarketClose )testRunner.testCase.testSuite.project.setPropertyValue( "lastMarketCloseMinus2Days", lastMarketCloseMinus2Days )testRunner.testCase.testSuite.project.setPropertyValue( "twoYearsAgoOfLastMarketCloseMinus2Days", twoYearsAgoOfLastMarketCloseMinus2Days )testRunner.testCase.testSuite.project.setPropertyValue( "threeYearsAgoOfLastMarketClose", threeYearsAgoOfLastMarketClose )testRunner.testCase.testSuite.project.setPropertyValue( "theDayBeforeYesterday", theDayBeforeYesterday )testRunner.testCase.testSuite.project.setPropertyValue( "theLastDayOfLastWeek", theLastDayOfLastWeek )testRunner.testCase.testSuite.project.setPropertyValue( "yesterdayOfLastMarketClose", yesterdayOfLastMarketClose )testRunner.testCase.testSuite.project.setPropertyValue( "aMonthAgoOfLastMarketClose", aMonthAgoOfLastMarketClose )testRunner.testCase.testSuite.project.setPropertyValue( "threeMonthsAgoOfLastMarketClose", threeMonthsAgoOfLastMarketClose )testRunner.testCase.testSuite.project.setPropertyValue( "theFirstDayOfTheQuarter", theFirstDayOfTheQuarter )testRunner.testCase.testSuite.project.setPropertyValue( "theLastDayOfLastQuarter", theLastDayOfLastQuarter )testRunner.testCase.testSuite.project.setPropertyValue( "aYearAgoAfterTheLastDayOfLastQuarter", aYearAgoAfterTheLastDayOfLastQuarter )testRunner.testCase.testSuite.project.setPropertyValue( "theFirstDayOfLastQuarter", theFirstDayOfLastQuarter )testRunner.testCase.testSuite.project.setPropertyValue( "theLastDayOfTwoQuartersAgo", theLastDayOfTwoQuartersAgo )testRunner.testCase.testSuite.project.setPropertyValue( "theFirstDayOfSixMonthsAgoAfterTwoQuarters", theFirstDayOfSixMonthsAgoAfterTwoQuarters )testRunner.testCase.testSuite.project.setPropertyValue( "theSameDayOfLastMonth", theSameDayOfLastMonth )testRunner.testCase.testSuite.project.setPropertyValue( "theLastDayOfLastMonth", theLastDayOfLastMonth )testRunner.testCase.testSuite.project.setPropertyValue( "yesterdayOfThreeMonthsAgo", yesterdayOfThreeMonthsAgo )testRunner.testCase.testSuite.project.setPropertyValue( "theFirstDayOfTwoMonthsAgo", theFirstDayOfTwoMonthsAgo )testRunner.testCase.testSuite.project.setPropertyValue( "theLastDayOfTwoMonthsAgo", theLastDayOfTwoMonthsAgo )testRunner.testCase.testSuite.project.setPropertyValue( "theFirstDayOfThreeMonthsAgo", theFirstDayOfThreeMonthsAgo )testRunner.testCase.testSuite.project.setPropertyValue( "theFirstDayOfSixMonthsAgo", theFirstDayOfSixMonthsAgo )testRunner.testCase.testSuite.project.setPropertyValue( "theFirstDayOfThisYear", theFirstDayOfThisYear )testRunner.testCase.testSuite.project.setPropertyValue( "theLastDayOfLastYear", theLastDayOfLastYear )testRunner.testCase.testSuite.project.setPropertyValue( "tomorrowOfLastYear", tomorrowOfLastYear )testRunner.testCase.testSuite.project.setPropertyValue( "theFirstDayOfThisMonthLastYear", theFirstDayOfThisMonthLastYear )testRunner.testCase.testSuite.project.setPropertyValue( "theFirstDayOfLastYear", theFirstDayOfLastYear )testRunner.testCase.testSuite.project.setPropertyValue( "theSameDayOfThisMonthThreeYearsAgo", theSameDayOfThisMonthThreeYearsAgo )testRunner.testCase.testSuite.project.setPropertyValue( "theFirstDayOfThisMonthThreeYearsAgo", theFirstDayOfThisMonthThreeYearsAgo )testRunner.testCase.testSuite.project.setPropertyValue( "theFirstDayOfThreeYearsAgo", theFirstDayOfThreeYearsAgo )//**********************************************************************Methods**************************************************************************def today(){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Calendar lastDate = Calendar.getInstance();	str = sdf.format(lastDate.getTime());	return str;}def yesterday(){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Calendar lastDate = Calendar.getInstance();	lastDate.add(Calendar.DATE, -1);	str = sdf.format(lastDate.getTime());	return str;}def theDayBeforeYesterday(){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Calendar lastDate = Calendar.getInstance();	lastDate.add(Calendar.DATE, -2);	str = sdf.format(lastDate.getTime());	return str;}def theLastDayOfLastMonth(){	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;}def theSameDayOfLastMonth(){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Calendar lastDate = Calendar.getInstance();	lastDate.roll(Calendar.MONTH, -1);	str = sdf.format(lastDate.getTime());	return str;}def tomorrow(){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Calendar lastDate = Calendar.getInstance();	lastDate.add(Calendar.DATE, 1);	str = sdf.format(lastDate.getTime());	return str;}def yesterdayOfThreeMonthsAgo(){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Calendar lastDate = Calendar.getInstance();	lastDate.add(Calendar.MONTH, -3);	lastDate.add(Calendar.DATE, -1);	str = sdf.format(lastDate.getTime());	return str;}def theFirstDayOfTwoMonthsAgo(){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Calendar lastDate = Calendar.getInstance();	lastDate.add(Calendar.MONTH, -2);	lastDate.set(Calendar.DATE, 1);	str = sdf.format(lastDate.getTime());	return str;}def theLastDayOfTwoMonthsAgo(){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Calendar lastDate = Calendar.getInstance();	lastDate.add(Calendar.MONTH, -2);	lastDate.set(Calendar.DATE, 1);	lastDate.roll(Calendar.DATE, -1);	str = sdf.format(lastDate.getTime());	return str;}def theFirstDayOfThreeMonthsAgo(){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Calendar lastDate = Calendar.getInstance();	lastDate.add(Calendar.MONTH, -3);	lastDate.set(Calendar.DATE, 1);	str = sdf.format(lastDate.getTime());	return str;}def theFirstDayOfSixMonthsAgo(){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Calendar lastDate = Calendar.getInstance();	lastDate.add(Calendar.MONTH, -6);	lastDate.set(Calendar.DATE, 1);	str = sdf.format(lastDate.getTime());	return str;}def tomorrowOfLastYear(){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Calendar lastDate = Calendar.getInstance();	lastDate.add(Calendar.YEAR, -1);	lastDate.add(Calendar.DATE, 1);	str = sdf.format(lastDate.getTime());	return str;}def theSameDayOfThisMonthThreeYearsAgo(){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Calendar lastDate = Calendar.getInstance();	lastDate.add(Calendar.YEAR, -3);	str = sdf.format(lastDate.getTime());	return str;}def theFirstDayOfThisMonthThreeYearsAgo(){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Calendar lastDate = Calendar.getInstance();	lastDate.add(Calendar.YEAR, -3);	lastDate.set(Calendar.DATE, 1);	str = sdf.format(lastDate.getTime());	return str;}def theFirstDayOfThreeYearsAgo(){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Calendar lastDate = Calendar.getInstance();	lastDate.add(Calendar.YEAR, -3);	lastDate.set(Calendar.MONTH, 0);	lastDate.set(Calendar.DATE, 1);	str = sdf.format(lastDate.getTime());	return str;}def theFirstDayOfThisYear(){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Calendar lastDate = Calendar.getInstance();	lastDate.set(Calendar.MONTH, 0);	lastDate.set(Calendar.DATE, 1);	str = sdf.format(lastDate.getTime());	return str;}def theLastDayOfLastYear(){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Calendar lastDate = Calendar.getInstance();	lastDate.add(Calendar.YEAR, -1);	lastDate.set(Calendar.MONTH, 11);	lastDate.set(Calendar.DATE, 31);	str = sdf.format(lastDate.getTime());	return str;}def theFirstDayOfTheQuarter(){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Calendar lastDate = Calendar.getInstance();	int i = 0	int j = 0	int month = lastDate.get(Calendar.MONTH)	if (month >= 1 && month <= 3) {		j = 0;	}	if (month >= 4 && month <= 6) {		j = 3;	}	if (month >= 7 && month <= 9) {		j = 6;	}	if (month >= 10 && month <= 12) {		j = 9;	}	lastDate.set(Calendar.MONTH, j);	lastDate.set(Calendar.DATE, 1);	str = sdf.format(lastDate.getTime());	return str;}def theLastDayOfLastQuarter(String theFirstDayOfTheQuarter){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Date date = sdf.parse(theFirstDayOfTheQuarter)	Calendar lastDate = Calendar.getInstance();	lastDate.setTime(date);	lastDate.add(Calendar.MONTH, -1);	lastDate.roll(Calendar.DATE, -1);	str = sdf.format(lastDate.getTime());	return str;}def aYearAgoAfterTheLastDayOfLastQuarter(String theFirstDayOfTheQuarter){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Date date = sdf.parse(theFirstDayOfTheQuarter)	Calendar lastDate = Calendar.getInstance();	lastDate.setTime(date);	lastDate.add(Calendar.YEAR, -1);	str = sdf.format(lastDate.getTime());	return str;}def theFirstDayOfLastQuarter(String theFirstDayOfTheQuarter){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Date date = sdf.parse(theFirstDayOfTheQuarter)	Calendar lastDate = Calendar.getInstance();	lastDate.setTime(date);	lastDate.add(Calendar.MONTH, -3);	str = sdf.format(lastDate.getTime());	return str;}def theLastDayOfTwoQuartersAgo(String theLastDayOfLastQuarter){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Date date = sdf.parse(theLastDayOfLastQuarter)	Calendar lastDate = Calendar.getInstance();	lastDate.setTime(date);	lastDate.add(Calendar.MONTH, -3);	lastDate.set(Calendar.DATE, 1);	lastDate.roll(Calendar.DATE, -1);	str = sdf.format(lastDate.getTime());	return str;}def theFirstDayOfSixMonthsAgoAfterTwoQuarters(String theLastDayOfTwoQuartersAgo){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Date date = sdf.parse(theLastDayOfTwoQuartersAgo)	Calendar lastDate = Calendar.getInstance();	lastDate.setTime(date);	lastDate.add(Calendar.MONTH, -8);	lastDate.set(Calendar.DATE, 1);	str = sdf.format(lastDate.getTime());	return str;}def theFirstDayOfThisMonthLastYear(){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Calendar lastDate = Calendar.getInstance();	lastDate.add(Calendar.YEAR, -1);	lastDate.set(Calendar.DATE, 1);	str = sdf.format(lastDate.getTime());	return str;}def theFirstDayOfLastYear(){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Calendar lastDate = Calendar.getInstance();	lastDate.add(Calendar.YEAR, -1);	lastDate.set(Calendar.MONTH,0)	lastDate.set(Calendar.DATE, 1);	str = sdf.format(lastDate.getTime());	return str;}def theLastDayOfLastWeek(){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Calendar lastDate = Calendar.getInstance();	lastDate.add(Calendar.WEEK_OF_MONTH,-1);	lastDate.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);	str = sdf.format(lastDate.getTime());	return str;}def lastMarketClose(){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Calendar lastDate = Calendar.getInstance();	String ss = lastDate.get(Calendar.DAY_OF_WEEK)	if ("1".equals(ss)){		lastDate.add(Calendar.WEEK_OF_MONTH,-1);		lastDate.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY)	}	if ("7".equals(ss)){		lastDate.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY)	}	str = sdf.format(lastDate.getTime());	return str;}def lastMarketCloseMinus2Days(){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Calendar lastDate = Calendar.getInstance();	lastDate.add(Calendar.DATE, -2);	String ss = lastDate.get(Calendar.DAY_OF_WEEK)	if ("1".equals(ss) ){		lastDate.add(Calendar.WEEK_OF_MONTH,-1);		lastDate.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY)	}	if ("7".equals(ss)){		lastDate.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY)	}	str = sdf.format(lastDate.getTime());	return str;}def twoYearsAgoOfLastMarketCloseMinus2Days(String lastMarketCloseMinus2Days){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Date date = sdf.parse(lastMarketCloseMinus2Days)	Calendar lastDate = Calendar.getInstance();	lastDate.setTime(date);	lastDate.add(Calendar.YEAR, -2)	lastDate.add(Calendar.DATE, 1);	str = sdf.format(lastDate.getTime());	return str;}def yesterdayOfLastMarketClose(String lastMarketClose){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Date date = sdf.parse(lastMarketClose)	Calendar lastDate = Calendar.getInstance();	lastDate.setTime(date);	lastDate.add(Calendar.DATE, -1);	str = sdf.format(lastDate.getTime());	return str;}def aMonthAgoOfLastMarketClose(String lastMarketClose){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Date date = sdf.parse(lastMarketClose)	Calendar lastDate = Calendar.getInstance();	lastDate.setTime(date);	lastDate.add(Calendar.MONTH, -1)	lastDate.add(Calendar.DATE, 1);	str = sdf.format(lastDate.getTime());	return str;}def threeMonthsAgoOfLastMarketClose(String lastMarketClose){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Date date = sdf.parse(lastMarketClose)	Calendar lastDate = Calendar.getInstance();	lastDate.setTime(date);	lastDate.add(Calendar.MONTH, -3)	lastDate.add(Calendar.DATE, 1);	str = sdf.format(lastDate.getTime());	return str;}def threeYearsAgoOfLastMarketClose(String lastMarketClose){	String str = "";	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	Date date = sdf.parse(lastMarketClose)	Calendar lastDate = Calendar.getInstance();	lastDate.setTime(date);	lastDate.add(Calendar.YEAR, -3)	lastDate.add(Calendar.DATE, 1);	str = sdf.format(lastDate.getTime());	return str;}

  

转载于:https://www.cnblogs.com/MasterMonkInTemple/p/7299801.html

你可能感兴趣的文章
PHP实现html字符实体转汉字
查看>>
洛谷 P1203 [USACO1.1]坏掉的项链Broken Necklace
查看>>
MongoDB之bson的介绍
查看>>
.NET Core IdentityServer4实战 第三章-使用EntityFramework Core进行持久化配置
查看>>
sql面试题一 学生成绩
查看>>
【深度学习大讲堂】首期第一讲:人工智能的ABCDE 第一部分:从人工智能和计算机视觉说起...
查看>>
2016/2/21 JavaScript简介
查看>>
关于Javascript调用asp.Net后台代码
查看>>
springcloud-06-feign的使用
查看>>
代码经验总结(未完)
查看>>
BZOJ1925: [Sdoi2010]地精部落
查看>>
ReportViewer实例教程(转载)
查看>>
php中文文件is_file检验失败(编码问题)
查看>>
也说Socket
查看>>
[置顶] 文章索引::”机器学习方法“,"深度学习方法",“三十分钟理解”原创系列...
查看>>
Storm概念学习系列之核心概念(Tuple、Spout、Blot、Stream、Stream Grouping、Worker、Task、Executor、Topology)(博主推荐)...
查看>>
Python 爬虫工程师必学 App数据抓取实战
查看>>
SQLServer数据库设计规范
查看>>
Visual C++中使用ADO访问数据库
查看>>
MySQL Python教程(3)
查看>>