我在创建一些应该动态打印过去5周内最后一天的日期的代码时遇到了一些麻烦。
示例输出;
current date is 3/27/2014
End date of week 1 = 3/21/2014
End date of week 2 = 3/14/2014
End date of week 3 = 3/7/2014
End date of week 4 = 2/28/2014
End date of week 5 = 2/21/2014.
感谢任何帮助。提前谢谢..
非常感谢大家......很抱歉让人感到困惑..实际上我想要的代码是我准备好找到从当前日期开始的最后4周结束日期..
public class dateValid6 {
public static void main(String[] args) {
int curDay = 22;
int curMonth = 3;
int curYear = 2014;
System.out.println("Current date = " + curMonth+"/"+curDay+"/"+curYear);
// if current day of month is between 30 to 25
if (curDay == 27 || curDay == 26 || curDay == 25 || curDay == 24 || curDay == 23 || curDay == 22)
{
int curDayw1 = curDay - 6;
String compDatew1 = (curMonth+"/"+curDayw1+"/"+curYear);
System.out.println("Compared Date for Week 1 = "+compDatew1);
int curDayw2 = curDayw1 - 7;
String compDatew2 = (curMonth+"/"+curDayw2+"/"+curYear);
System.out.println("Compared Date for Week 2 = "+compDatew2);
int curDayw3 = curDayw2 - 7;
String compDatew3 = (curMonth+"/"+curDayw3+"/"+curYear);
System.out.println("Compared Date for Week 3 = "+compDatew3);
int curDayw4 = 0;
if (curMonth == 3)
{
curDayw4 = curDayw3 - 7;
if(curDayw4 == 0)
{
curDayw4 = 28;
}
else if(curDayw4 == -1)
{
curDayw4 = 27;
}
else if(curDayw4 == -2)
{
curDayw4 = 26;
}
else if(curDayw4 == -3)
{
curDayw4 = 25;
}
else if(curDayw4 == -4)
{
curDayw4 = 24;
}
else if(curDayw4 == -5)
{
curDayw4 = 23;
}
}
if (curMonth == 5 || curMonth == 7 || curMonth == 10 || curMonth == 12)
{
curDayw4 = curDayw3 - 6;
if(curDayw4 == 0)
{
curDayw4 = 30;
}
else if(curDayw4 == -1)
{
curDayw4 = 29;
}
else if(curDayw4 == -2)
{
curDayw4 = 28;
}
else if(curDayw4 == -3)
{
curDayw4 = 27;
}
else if(curDayw4 == -4)
{
curDayw4 = 26;
}
else if(curDayw4 == -5)
{
curDayw4 = 25;
}
}
if (curMonth == 1 || curMonth == 2 || curMonth == 4 || curMonth == 6 || curMonth == 8 || curMonth == 9 || curMonth == 11)
{
curDayw4 = curDayw3 - 6;
if(curDayw4 == 0)
{
curDayw4 = 31;
}
else if(curDayw4 == -1)
{
curDayw4 = 30;
}
else if(curDayw4 == -2)
{
curDayw4 = 29;
}
else if(curDayw4 == -3)
{
curDayw4 = 28;
}
else if(curDayw4 == -4)
{
curDayw4 = 27;
}
else if(curDayw4 == -5)
{
curDayw4 = 26;
}
}
curMonth = curMonth - 1;
String compDatew4 = (curMonth+"/"+curDayw4+"/"+curYear);
System.out.println("Compared Date for Week 5 = "+compDatew4);
}
}
}
我需要完成此操作,但最少使用代码行和本月的任何日期..非常感谢....
答案 0 :(得分:1)
欢迎使用StackOverflow。
以下示例创建一个new GregorianCalendar();
,代表今天。然后,我们以您请求的格式创建SimpleDateFormat
。之后,我们操纵c
将其设置为周末(在本例中为星期日),然后在循环中一次删除一周,并通过我们的sdf
打印结果。
public class Test {
public static void main(String[] args) {
GregorianCalendar c = new GregorianCalendar();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
c.set(GregorianCalendar.DAY_OF_WEEK, GregorianCalendar.SUNDAY);
System.out.println("End date of this week = "+sdf.format(c.getTime()));
for(int i = 1 ; i <=5 ; i++){
c.set(GregorianCalendar.WEEK_OF_YEAR, c.get(GregorianCalendar.WEEK_OF_YEAR)-1);
System.out.println("End date of "+i+" week ago = "+sdf.format(c.getTime()));
}
}
}
输出:
End date of this week = 03/30/2014
End date of 1 week ago = 03/23/2014
End date of 2 week ago = 03/16/2014
End date of 3 week ago = 03/09/2014
End date of 4 week ago = 03/02/2014
End date of 5 week ago = 02/23/2014
这是一个完整的工作示例,因此您可以将其复制到名为Test.java
的新Java文件中,它将运行。我希望这有帮助。