这可能很容易,但我尝试了方框中的每个技巧,但找不到解决方案。经历了很多教程。
问题: 你有两个月了。但是对于要求,开始日应该是星期一,返回日期应该是星期日。 我被要求使用以下模式编写函数。
写一个函数 int solution(int year,String startMonth,String endMonth,String firstDayOfYear)
所以调用方法如下: int解决方案(2014年,“五月”,“六月”,“星期三”)
因此,例如2014年4月1日至2014年5月31日,它应该在2014年4月7日至2014年5月25日之间返回,因此需要7周。
我被要求不使用日历API!
有谁可以帮我解决这个问题?我在下面写了下面的样本代码:
package holidayplanner4;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* @author kanirmal
*/
public class HolidayPlanner4 {
/**
* @param args the command line arguments
*/
public static int solution(int year, String startMonth, String endMonth, String firstDayOfYear)
{
//String date1 = ""
SimpleDateFormat solFormat = new SimpleDateFormat("dd MMM yyyy");
String datestartMonth = 01+" "+startMonth+" "+year;
String dateendMonth = 31+" "+endMonth+" "+year;
System.out.println("Start Day:"+datestartMonth+"\tEnd Day:"+dateendMonth);
int daysBetween = 0;
try
{
Date dateBefore = solFormat.parse(datestartMonth);
Date dateAfter = solFormat.parse(dateendMonth);
long difference = dateAfter.getTime() - dateBefore.getTime();
System.out.println("Start Date:"+dateBefore+"\tEnd Date:"+dateAfter);
daysBetween = (int) (difference / (1000*60*60*24));
//daysBetween = daysBetween/7;
/* one can also convert the milliseconds to days using below method
* float daysBetween =
* TimeUnit.DAYS.convert(difference, TimeUnit.MILLISECONDS)
*/
// System.out.println("Number of Days between dates: "+daysBetween);
}
catch(ParseException e)
{
System.out.println("Exception caught:"+e);
}
return daysBetween;
}
public static void main(String[] args)
{
// TODO code application logic here
int year = 2014;
int no_of_weeks = 0;
String start_month = "May";
String end_month = "June";
String firstDayOfYear = "Wednesday";
no_of_weeks = solution(year,start_month,end_month,firstDayOfYear);
System.out.println("Total number of weeks spent during the
vacation:"+no_of_weeks);
}
}
答案 0 :(得分:0)
我得到了一位同事的帮助。我们尚未获得实际所需的输出,但我们接近解决方案,因此我将其标记为接近。请在下面找到我的最终代码。
import java.util.ArrayList;
import java.util.HashMap;
/**
*
* @author kanirmal
*/
public class HolidayPlanner {
public static boolean leapYear(int year) {
boolean leap = false;
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
leap = true;
} else {
leap = false;
}
} else {
leap = true;
}
} else {
leap = false;
}
return leap;
}
public static int solution(int year, String startMonth, String endMonth,
String firstDayOfYear) {
ArrayList<String> days = new ArrayList<>();
days.add("mon");
days.add("twe");
days.add("wed");
days.add("thu");
days.add("fri");
days.add("sat");
days.add("sun");
ArrayList<String> month = new ArrayList<>();
month.add("january");
month.add("february");
month.add("march");
month.add("april");
month.add("may");
month.add("june");
month.add("july");
month.add("august");
month.add("september");
month.add("october");
month.add("november");
month.add("december");
HashMap<String, Integer> mapMonth = new HashMap<>();
mapMonth.put("january", 31);
if (leapYear(year)) {
mapMonth.put("february", 29);
} else {
mapMonth.put("february", 28);
}
mapMonth.put("march", 31);
mapMonth.put("april", 30);
mapMonth.put("may", 31);
mapMonth.put("june", 30);
mapMonth.put("july", 31);
mapMonth.put("august", 31);
mapMonth.put("september", 30);
mapMonth.put("october", 31);
mapMonth.put("november", 30);
mapMonth.put("december", 31);
int startMonthDays = 0;
for (int i = 0; i <= month.indexOf(startMonth); i++) {
startMonthDays += mapMonth.get(month.get(i));
}
int endMonthDays = 0;
for (int i = 0; i <= month.indexOf(endMonth); i++) {
endMonthDays += mapMonth.get(month.get(i));
}
int startDayNo = ((days.indexOf(firstDayOfYear) - 1) + ((startMonthDays % 7) - 1)) % 7;
int endDayNo = ((days.indexOf(firstDayOfYear) - 1) + ((endMonthDays % 7) - 1) + mapMonth.get(endMonth)) % 7;
int totalLeaveDays = mapMonth.get(startMonth)
+ (endMonthDays - startMonthDays);
if (startDayNo != 0) {
totalLeaveDays = totalLeaveDays - startDayNo;
}
if (endDayNo != 6) {
totalLeaveDays = totalLeaveDays - endDayNo;
}
int totalWeek = totalLeaveDays / 7;
return totalWeek;
}
public static void main(String args[])
{
int year = 2018;
int no_of_weeks = 0;
String start_month = "may";
String end_month = "june";
String firstDayOfYear = "mon";
no_of_weeks = solution(year,start_month,end_month,firstDayOfYear);
System.out.println("Total number of weeks spent during the vacation:"+no_of_weeks);
System.out.println("solution::" + solution(2014, "may", "june", "wed"));
}
}