This question already has an answer here:
这个问题在这里已有答案:
- How can I increment a date by one day in Java? 25 answers
- 如何在Java中将日期增加一天? 25个答案
- How can I add business days to the current date in Java? 13 answers
- 如何在Java中将当前日期添加到工作日? 13个答案
I want two dates. 1) Current date in MM/dd/yy format 2) Modified date which will be the adition of five business days(Mon-Fri) to current date and it should be in MMM dd, yyyy format.
我想要两个约会。 1)MM / dd / yy格式的当前日期2)修改日期,即五个工作日(周一至周五)至当前日期的加法,应为MMM dd,yyyy格式。
So if my current is 9th june than currentDate should be 06/09/14 and modifiedDate should be Jun 13, 2014.
因此,如果我的当前时间是6月9日,那么currentDate应该是06/09/14,并且modifiedDate应该是2014年6月13日。
How to do this?
这个怎么做?
3 个解决方案
#1
9
This will add working days (Mon-Fri) and will present dates in the required format.
这将增加工作日(周一至周五),并以所需格式显示日期。
Date date=new Date();
Calendar calendar = Calendar.getInstance();
date=calendar.getTime();
SimpleDateFormat s;
s=new SimpleDateFormat("MM/dd/yy");
System.out.println(s.format(date));
int days = 5;
for(int i=0;i<days;)
{
calendar.add(Calendar.DAY_OF_MONTH, 1);
//here even sat and sun are added
//but at the end it goes to the correct week day.
//because i is only increased if it is week day
if(calendar.get(Calendar.DAY_OF_WEEK)<=5)
{
i++;
}
}
date=calendar.getTime();
s=new SimpleDateFormat("MMM dd, yyyy");
System.out.println(s.format(date));
Ref : https://*.com/a/15339851/3603806 and https://*.com/a/11356123/3603806
参考:https://*.com/a/15339851/3603806和https://*.com/a/11356123/3603806
#2
9
The notion of working days is not implemented in Java, it's too subject to interpretation (for example, many international companies have their own holidays). Code below uses isWorkingDay()
, which only returns false for weekends - add your holidays there.
工作日的概念不是用Java实现的,它太容易解释(例如,许多国际公司都有自己的假期)。下面的代码使用isWorkingDay(),它只在周末返回false - 在那里添加你的假期。
public class Test {
public static void main(String[] args) {
Calendar cal = new GregorianCalendar();
// cal now contains current date
System.out.println(cal.getTime());
// add the working days
int workingDaysToAdd = 5;
for (int i=0; i<workingDaysToAdd; i++)
do {
cal.add(Calendar.DAY_OF_MONTH, 1);
} while ( ! isWorkingDay(cal));
System.out.println(cal.getTime());
}
private static boolean isWorkingDay(Calendar cal) {
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == Calendar.SUNDAY || dayOfWeek == Calendar.SATURDAY)
return false;
// tests for other holidays here
// ...
return true;
}
}
#3
1
Here is the code sample to add dates. You may modify in order to you can only add business days.
这是添加日期的代码示例。您可以修改,以便您只能添加工作日。
SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd/yy");
SimpleDateFormat sdf2 = new SimpleDateFormat("MMM dd, yyyy");
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
System.out.println(sdf1.format(calendar.getTime()));
calendar.add(Calendar.DATE,6);
System.out.println(sdf2.format(calendar.getTime()));
#1
9
This will add working days (Mon-Fri) and will present dates in the required format.
这将增加工作日(周一至周五),并以所需格式显示日期。
Date date=new Date();
Calendar calendar = Calendar.getInstance();
date=calendar.getTime();
SimpleDateFormat s;
s=new SimpleDateFormat("MM/dd/yy");
System.out.println(s.format(date));
int days = 5;
for(int i=0;i<days;)
{
calendar.add(Calendar.DAY_OF_MONTH, 1);
//here even sat and sun are added
//but at the end it goes to the correct week day.
//because i is only increased if it is week day
if(calendar.get(Calendar.DAY_OF_WEEK)<=5)
{
i++;
}
}
date=calendar.getTime();
s=new SimpleDateFormat("MMM dd, yyyy");
System.out.println(s.format(date));
Ref : https://*.com/a/15339851/3603806 and https://*.com/a/11356123/3603806
参考:https://*.com/a/15339851/3603806和https://*.com/a/11356123/3603806
#2
9
The notion of working days is not implemented in Java, it's too subject to interpretation (for example, many international companies have their own holidays). Code below uses isWorkingDay()
, which only returns false for weekends - add your holidays there.
工作日的概念不是用Java实现的,它太容易解释(例如,许多国际公司都有自己的假期)。下面的代码使用isWorkingDay(),它只在周末返回false - 在那里添加你的假期。
public class Test {
public static void main(String[] args) {
Calendar cal = new GregorianCalendar();
// cal now contains current date
System.out.println(cal.getTime());
// add the working days
int workingDaysToAdd = 5;
for (int i=0; i<workingDaysToAdd; i++)
do {
cal.add(Calendar.DAY_OF_MONTH, 1);
} while ( ! isWorkingDay(cal));
System.out.println(cal.getTime());
}
private static boolean isWorkingDay(Calendar cal) {
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == Calendar.SUNDAY || dayOfWeek == Calendar.SATURDAY)
return false;
// tests for other holidays here
// ...
return true;
}
}
#3
1
Here is the code sample to add dates. You may modify in order to you can only add business days.
这是添加日期的代码示例。您可以修改,以便您只能添加工作日。
SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd/yy");
SimpleDateFormat sdf2 = new SimpleDateFormat("MMM dd, yyyy");
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
System.out.println(sdf1.format(calendar.getTime()));
calendar.add(Calendar.DATE,6);
System.out.println(sdf2.format(calendar.getTime()));