如何在JAVA中显示当前星期的日期? [英] How to show the dates of the current week in JAVA?

查看:97
本文介绍了如何在JAVA中显示当前星期的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果今天是星期一至星期五之间,我希望它看起来像这样:
2011年1月11日-星期一
2011年1月12日星期二
2011年1月13日星期三
2011年1月14日星期四
星期五-2011年1月15日

如果今天是星期六(1月16日)或星期日,我希望它显示下周
2011年1月18日星期一-
2011年1月19日星期二
2011年1月20日星期三
2011年1月21日星期四
星期五-2011年1月22日


我已经尝试了两个小时的日历课程​​,并且仍在继续.我真的很感谢任何帮助.

非常感谢!

Cris

If today is between monday to friday I want it to appear like this:
Monday - January 11 2011
Tuesday - January 12 2011
Wednesday - January 13 2011
Thursday - January 14 2011
Friday - January 15 2011

If today is Saturday (16 January) or Sunday I want it to show the next week
Monday - January 18 2011
Tuesday - January 19 2011
Wednesday - January 20 2011
Thursday - January 21 2011
Friday - January 22 2011


I''ve been trying to do it with the calendar class for two hours and still going at it. I''d really appreciate any kind of help.

Thanks a lot!

Cris

推荐答案

您将必须编写自己的特定方法来自定义此逻辑.编写一个使用常规日期类的方法,并可以将您带到该结果.重要步骤:
1.根据提供的数据查找当日
2.看看是否是星期六/星期日.如果是这样,则根据一周中的当前日期,移至下周.
3.如果不是星期六/星期日,则选择当前星期.

现在,可以选择多种方式来编写逻辑.一种最简单的方法是直接添加/减去日期.由于一周中没有几天是常数(7),因此这不是问题.
尝试!
You will have to write specific methods of your own that will customize this logic. Write a method that uses general date class and can lead you to this result. Important steps in it:
1. Find the current day based on the data supplied
2. See if it''s Saturday/Sunday. If so, then based on current day of week, move forward to next week.
3. If it was not Saturday/Sunday then pick the current week.

Now, for picking, there can be various ways to write the logic. One simplest would be to directly, add/subtract the days. Since no of days in a week is constant factor (7), this shouldn''t be an issue.
Try!


这是我被召唤去做的一堂课;

Here is a class i knocked up to do this;

import java.util.*;

public class myCalendar {
	private Calendar c;
	private List<String> output;
	
	public myCalendar()
	{
		c = Calendar.getInstance();
		
		output =  new ArrayList<String>();
	}
	
	public List<String> getCalendar()
	{
		//Get current Day of week and Apply suitable offset to bring the new calendar
		//back to the appropriate Monday, i.e. this week or next
		switch (c.get(Calendar.DAY_OF_WEEK))
		{
		case Calendar.SUNDAY:
			c.add(Calendar.DATE, 1);
			break;
			
		case Calendar.MONDAY:
			//Don't need to do anything on a Monday
			//included only for completeness
			break;
			
		case Calendar.TUESDAY:
			c.add(Calendar.DATE,-1);
			break;
			
		case Calendar.WEDNESDAY:
			c.add(Calendar.DATE, -2);
			break;
			
		case Calendar.THURSDAY:
			c.add(Calendar.DATE,-3);
			break;
				
		case Calendar.FRIDAY:
			c.add(Calendar.DATE,-4);
			break;
			
		case Calendar.SATURDAY:
			c.add(Calendar.DATE,2);
			break;
		}
		
		//Add the Monday to the output
		output.add(c.getTime().toString());
		for (int x = 1; x <5; x++)
		{
			//Add the remaining days to the output
			c.add(Calendar.DATE,1);
			output.add(c.getTime().toString());
		}
		return this.output;
	}
}



然后可以通过以下方式调用此方法:



You can then call this by;

myCalendar c = new myCalendar();
System.out.print(c.getCalendar());



控制台中的输出将是日期列表;
[2011年1月10日格林尼治标准时间2011年1月11日星期二,格林尼治标准时间2011年1月11日23:30:17,格林尼治标准时间2011年1月12日23:30:17,格林尼治标准时间1月13日23:30:17,格林尼治标准时间1月14日23: 2011年格林尼治标准时间30:17]

如您所见,所有基础知识都已存在.您只需要弄清楚如何回传日期.即作为日期或日历或字符串等.



The output in the console will be a list of dates;
[Mon Jan 10 23:30:17 GMT 2011, Tue Jan 11 23:30:17 GMT 2011, Wed Jan 12 23:30:17 GMT 2011, Thu Jan 13 23:30:17 GMT 2011, Fri Jan 14 23:30:17 GMT 2011]

As you can see the basics are all there. You just need to sort out how you want to pass back the dates. i.e. as Dates or as Calendars or as strings etc. etc.


这篇关于如何在JAVA中显示当前星期的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆