JAVA从用户输入的日期查找星期几

时间:2017-01-26 05:26:32

标签: java calendar

我有这个代码,用于查找用户输入日期但当前没有按预期工作的星期几。 另外,我需要帮助设置循环一次又一次地问问题,直到用户按“Control + Z”退出。 任何人都可以检查并指出我的问题。也有助于循环。

import java.util.Calendar;
import java.util.Scanner;

public class CalendarProject {

@SuppressWarnings({ "resource" })
public static void main(String[] args) {    
    Calendar c = Calendar.getInstance(); 
    Scanner input = new Scanner (System.in);

    //Enter the Day
    System.out.println("Enter the day in number:");
    int day1= input.nextInt( );

    //Enter the Month
    System.out.println("Enter the Month in number");
    int month= input.nextInt( );

    //Enter the Year
    System.out.println("Enter the Year in number format");
    int year= input.nextInt( );


    //Display the day
    int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);


    System.out.println(String.format("%d/%d/%d",day1,month,year));
    System.out.println(c.get(Calendar.DAY_OF_WEEK));


    if (dayOfWeek == Calendar.SUNDAY) {
        System.out.println("Sunday");
    }
    else if (dayOfWeek == Calendar.MONDAY) {
        System.out.println("Monday");
    }
    else if (dayOfWeek == Calendar.TUESDAY) {
            System.out.println("Tuesday");
    }
    else if (dayOfWeek == Calendar.WEDNESDAY) {
                System.out.println("Wednesday");
    }
    else if (dayOfWeek == Calendar.THURSDAY) {
        System.out.println("Thursday");
    }
    else if (dayOfWeek == Calendar.FRIDAY) {
        System.out.println("Friday");
    }
    else if (dayOfWeek == Calendar.SATURDAY) {
        System.out.println("Saturday");

   }
}
}

1 个答案:

答案 0 :(得分:1)

1.您从未在b = df1.values[:, :2].ravel() c = df1.country.values ip = df.ip_address.values srch = b.searchsorted(ip) // 2 mask = (ip >= b[0]) & (ip <= b[-1]) df.loc[mask, 'country'] = c[srch[mask]] 对象中设置输入日期,因此它永远不会按预期工作。因此,您需要使用Calender此处c.setTime(date);date对象 2.对于循环,您可以使用do while循环一次又一次地询问用户。
以下是您修改后的代码

Date

3。如果您使用的是Java 8,则可以使用public static void main(String[] args) { Calendar c; SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); Scanner input = new Scanner (System.in); try { do{ System.out.println("Enter the day in number:"); int day1= input.nextInt(); //Enter the Month System.out.println("Enter the Month in number"); int month= input.nextInt( ); //Enter the Year System.out.println("Enter the Year in number format"); int year= input.nextInt( ); Date date = formatter.parse((String.format("%d/%d/%d",day1,month,year))); System.out.println(date); c= Calendar.getInstance(); c.setTime(date); int day = c.get(Calendar.DAY_OF_WEEK); if (day == Calendar.SUNDAY) { System.out.println("Sunday"); } else if (day == Calendar.MONDAY) { System.out.println("Monday"); } else if (day == Calendar.TUESDAY) { System.out.println("Tuesday"); } else if (day == Calendar.WEDNESDAY) { System.out.println("Wednesday"); } else if (day == Calendar.THURSDAY) { System.out.println("Thursday"); } else if (day == Calendar.FRIDAY) { System.out.println("Friday"); } else if (day == Calendar.SATURDAY) { System.out.println("Saturday"); } System.out.println("To exit press CTRL+Z(Windows) or CTRL+D(LINUX), or any key to continue"); input.nextLine(); }while (input.hasNextLine()); }catch(NoSuchElementException n){ System.out.println("NoSuchElementException"); }catch(ParseException pe){ System.out.println("invalid date"); }finally { input.close(); System.out.println("exiting"); } } Enum删除所有if else块。只需按照以下行替换所有if else

java.time.DayOfWeek

我使用了三元运算符,因为DayOfWeek dayOfWeek = DayOfWeek.of((day+6)%7==0?7:(day+6)%7); System.out.println(dayOfWeek); DayOfWeek作为索引MONDAY的值,1Calender.DAY_OF_WEEk作为索引SUNDAY的值< / p>