我正在尝试编写一个程序,它将采用两个参数:工作日和年份。然后,程序应打印出用户指定的工作日落在给定年份中每个月的第一天的次数。我可以做一个月,但我如何迭代所有十二个月。
这是我的代码:
public class dateCheck {
public static void main(String[] args) {
String weekday="Sunday";
int year=2017;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, 01);
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.DAY_OF_MONTH, 1);
Date firstDayOfMonth = cal.getTime();
DateFormat sdf = new SimpleDateFormat("EEEEEEEE");
System.out.println("First Day of Month: " + sdf.format(firstDayOfMonth));
if(weekday.equals(sdf.format(firstDayOfMonth))){
System.out.println("This day falls on the first of the month");
}
else
System.out.println("This day does not fall on the first of the month");
}
}
我想我可以使用for循环,但我不知道如何迭代几个月。我是Java新手。任何帮助表示赞赏。感谢。
答案 0 :(得分:5)
如果您能够使用Java 8,则没有理由继续使用Calendar
。这是使用new Date and Time api的版本:
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.TextStyle;
import java.util.Locale;
public class WeekdayCheck {
public static void main(String[] args) {
String weekday = "Sunday";
int year = 2017;
int occurrences = 0;
for (Month month : Month.values()) {
LocalDate date = LocalDate.of(year, month, 1);
DayOfWeek dayOfWeek = date.getDayOfWeek();
if (dayOfWeek.getDisplayName(TextStyle.FULL, Locale.ENGLISH).equals(weekday)) {
occurrences++;
}
}
System.out.println(weekday + " is the first day of a month " + occurrences + " times in " + year);
}
}
更新
初始版本使用dayOfWeek.getDisplayName
与您的比较一致。更明智的选择(如评论中所指出的)也是将用户输入转换为DayOfWeek
实例。
这是一种修改后的方法,目前适用于“sunday”,“Sunday”或“sUnDaY”等输入,但可以通过增强toDayOfWeek
方法调整更复杂的逻辑:
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
public class WeekdayCheck {
public static void main(String[] args) {
String userInput = "Sunday";
DayOfWeek inputDayOfWeek = toDayOfWeek(userInput);
int inputYear = 2017;
int occurrences = 0;
for (Month month : Month.values()) {
LocalDate date = LocalDate.of(inputYear, month, 1);
DayOfWeek dayOfWeek = date.getDayOfWeek();
if (dayOfWeek == inputDayOfWeek) {
occurrences++;
}
}
System.out.println(userInput + " is the first day of a month " + occurrences + " times in " + inputYear);
}
private static DayOfWeek toDayOfWeek(String dayString) {
return DayOfWeek.valueOf(dayString.toUpperCase());
}
}
答案 1 :(得分:1)
这是我的方法。我用两种方法。一个循环遍历所有月份,一个循环使用它,如果条件为真,则返回一个布尔值。条件与用户查找的条件相同的工作日。如果为真,则使字段计数器增加1。 我使用Calendar.set()方法首先将其设置为年初,然后使用Calender.roll(),每次迭代提前一个月。 我希望这是有道理的
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class MyCalendar {
private Calendar calendar;
private int counter;
private DateFormat frm;
public static void main(String[] args) {
MyCalendar cal = new MyCalendar();
int answer = cal.firstPerMonth(2017, Calendar.SUNDAY);
System.out.println("total" + answer);
}
public MyCalendar() {
calendar = Calendar.getInstance();
frm = new SimpleDateFormat("yyyy MMM dd 'is a' EEE");
}
public int firstPerMonth(int year, int dayOfWeek) {
counter = 0;
calendar.set(2017, 0, 1);
for(int x = 0; x < 12; x++) {
if(checkIfSameDay(dayOfWeek)) {
counter++;
}
calendar.roll(Calendar.MONTH, 1);
}
return counter;
}
public boolean checkIfSameDay(int dayOfWeek) {
if(dayOfWeek == calendar.get(Calendar.DAY_OF_WEEK)) {
System.out.println(frm.format(calendar.getTime()));
return true;
}
else return false;
}
}
答案 2 :(得分:0)
这是你连续12个月迭代的方式(这是因为Calendar.JANUARY
,所有剩余的月份从0开始只是连续int
:
String weekday = "Sunday";
int year = 2017;
for (int month = Calendar.JANUARY; month <= Calendar.DECEMBER; month++) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, 01);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.DAY_OF_MONTH, 1);
Date firstDayOfMonth = cal.getTime();
DateFormat sdf = new SimpleDateFormat("EEEEEEEE");
System.out.println("First Day of Month: " + sdf.format(firstDayOfMonth));
if (weekday.equals(sdf.format(firstDayOfMonth))) {
System.out.println("This day falls on the first of the month");
} else
System.out.println("This day does not fall on the first of the month");
}
答案 3 :(得分:0)
尝试这样的事情:不要传递Calendar.JANUARY
,而是传递月份的整数值。
public class dateCheck {
public static void main(String[] args) {
String weekday = "Sunday";
int year = 2017;
int month = 0;
for (month = 0; month < 12; month++) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, 01);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.DAY_OF_MONTH, 1);
Date firstDayOfMonth = cal.getTime();
DateFormat sdf = new SimpleDateFormat("EEEEEEEE");
System.out.println("First Day of Month " + month.toString() + ": " + sdf.format(firstDayOfMonth));
if (weekday.equals(sdf.format(firstDayOfMonth))) {
System.out.println("This day falls on the first of the month");
} else
System.out.println("This day does not fall on the first of the month");
}
}
}
答案 4 :(得分:0)
此方法使用.roll(...)
Calendar
方法。它演示了如何增加cal
实例。
final static DateFormat sdf = new SimpleDateFormat("EEEEEEEE");
static boolean isOnFirst(String weekday, Calendar month)
{
String dom = sdf.format(month.getTime());
return (weekday.equalsIgnoreCase(dom));
}
public static void main(String[] args)
{
// test data: could be obtained from the user
String weekday = "Sunday";
int year = 2017;
// construct the initial instance
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, 1);
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.YEAR, year);
// our (poorly named) counter for how often it appears
int num = 0;
// loop over all of the months in the year
for (int i = 0; i < 12; ++i) {
// if we are on the first day of the month, increment our counter
if (isOnFirst(weekday, cal)) {
++num;
}
// advance by one month
cal.roll(Calendar.MONTH, 1);
}
// output results
System.out.println(weekday + " appears " + num
+ " times on the first day of the month in " + year);
}
输出:
周日在2017年的第一天出现2次
答案 5 :(得分:0)
试试这个:
String weekday = "Sunday";
int year = 2017;
int numOfWeekDayAsFirstDayInMonth = 0;
for (int i = 0; i < 11; i++) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, 01);
cal.set(Calendar.MONTH, i);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.DAY_OF_MONTH, 1);
Date firstDayOfMonth = cal.getTime();
DateFormat sdf = new SimpleDateFormat("EEEEEEEE");
if (weekday.equals(sdf.format(firstDayOfMonth))) {
numOfWeekDayAsFirstDayInMonth++;
}
}
System.out.printf("Week day %s occured %d times in year %s as first day of all monthes", weekday, numOfWeekDayAsFirstDayInMonth, year);
输出:
Week day Sunday occured 2 times in year 2017 as first day of all monthes