我真的不知道如何解释这个,但基本上用户会被问到多少次,比如他们说2.它会要求用户以数字形式输入月,日和年。然后它会再次这样做,因为他们说2.它在(2014年1月8日,星期六)格式中为他们放入的每个日期吐出一个日期。所以我想要的是:
There were X dates on Sunday
There were X dates on Monday
There were X dates on Tuesday
There were X dates on Wednesday
There were X dates on Thursday
There were X dates on Friday
There were X dates on Saturday
如何让java识别星期几,然后添加一个,以便我可以替换上面的X.
我每天都有变量,例如int saturday。我知道我必须做周六++;在某个地方,但我不知道在哪里。我尝试了一个开关和案例,但它不知道
case Monday:
因为星期一不在我的代码中,我使用了简单的日期格式。
这有意义吗?我应该发布我的代码吗?警告它像300行。
正如Pshemo所说:“简而言之,我希望用户说出发生了多少”事件“,传递了他们的日期并打印了一周中每天发生的事件数量”
答案 0 :(得分:2)
尝试使用SimpleDateFormat
格式化E
,day.equals("Monday")
将输出星期几。将该日期作为字符串与您的工作日进行比较,例如{{1}}。
对SimpleDateFormat的引用:http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
答案 1 :(得分:2)
一周中的几天是java.util.Calendar
中的int常量。您可以使用以下方式找到它:
int dayOfTheWeek = c.get(Calendar.DAY_OF_WEEK);
答案 2 :(得分:2)
以下是使用Joda-Time 2.3库的示例代码。
星期几是基于时区的解释。在Joda-Time中,DateTime对象实际上知道它自己的时区,不像java.util.Date,似乎有时区但没有。考虑一下您是希望每个DateTime对象使用自己的时区来确定星期几,还是希望将对象转换为公共时区进行比较。
DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );
java.util.List<DateTime> dateTimes = new java.util.ArrayList<DateTime>();
dateTimes.add( new DateTime( 2014, 1, 2, 3, 4, 5, timeZone ) ); // Thursday
dateTimes.add( new DateTime( 2014, 1, 3, 3, 4, 5, timeZone ) ); // Friday
dateTimes.add( new DateTime( 2014, 1, 9, 3, 4, 5, timeZone ) ); // Thursday
System.out.println( "Date-Time objects…" );
System.out.println( dateTimes );
// Make a list of 7 elements, one element per each day of week.
// Each element stores a count of events occurring on that day of week.
// Using standard ISO 8601 week, Monday first, Sunday last.
java.util.List<Integer> days = new java.util.ArrayList<Integer>( 7 );
for ( int i = 0; i < 7; i++ ) {
days.add( new Integer( 0 ) ); // Initial all counts to zero.
}
for ( DateTime dateTime : dateTimes ) {
int dayOfWeekNumber = dateTime.getDayOfWeek(); // Retrieve day-of-week number, 1-7. Monday is first.
int index = ( dayOfWeekNumber - 1 ); // Index/Zero-based counting, so subtract 1 from ordinal.
Integer oldIntegerCount = days.get( index ); // Retrieve the previous count for this day-of-week.
Integer newIntegerCount = ( oldIntegerCount + 1 ); // Increment old count to new count object.
days.set( index, newIntegerCount ); // Replace old Integer object with freshly incremented Integer object.
}
// Report results.
// Joda-Time does not have a convenient list of days of week to iterate. So the following code is a bit goofy.
// The java.time.* package in Java 8 does have the nice feature of a fancy Enum for days-of-week.
for ( int i = 0; i < days.size(); i++ ) {
LocalDate date = new LocalDate();
date = date.withDayOfWeek( i + 1 ); // Add one to transform index into ordinal.
System.out.println( "There were " + days.get( i ) + " dates on " + date.dayOfWeek().getAsText() );
}
跑步时......
Date-Time objects…
[2014-01-02T03:04:05.000+01:00, 2014-01-03T03:04:05.000+01:00, 2014-01-09T03:04:05.000+01:00]
There were 0 dates on Monday
There were 0 dates on Tuesday
There were 0 dates on Wednesday
There were 2 dates on Thursday
There were 1 dates on Friday
There were 0 dates on Saturday
There were 0 dates on Sunday