控制台输入年月,怎样获取年份月份。

解决方案 »

  1.   

    控制台只能输入基本类型的数据 你这种情况应该是字符串型的 要在程序里转化 main方法有个args[]s数组 通过这个获取
      

  2.   

    给个例子, 循环接受输入, 可能不太完善,你自己再用calendar完善以下就可以了。import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;public class YearMonth {
    public static void main(String[] args) throws IOException, ParseException{
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
    while (true){
    System.out.println("\nPlease input year and month like 1983-05 : ");
    String line = reader.readLine();
    Date d = format.parse(line);
    System.out.println("The year is:"+d.getYear());
    System.out.println("The month is:"+d.getMonth());

    }
    }
    }
      

  3.   

     做人做到底,都给你补全了:public class YearMonth {
    public static void main(String[] args) throws IOException, ParseException{
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
    while (true){
    System.out.println("\nPlease input year and month like 1983-05 : ");
    String line = reader.readLine();
    Date d = format.parse(line);
    Calendar cal =new GregorianCalendar();
    cal.setTime(d);
    int day = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    System.out.println("The amout of day in month is: " + day);
    }
    }
    }
      

  4.   

    getActualMaximum这个方法我都不知道
    我用set和roll来计算的天数,你用的这个方法要简单多了。非常感谢。