簡體   English   中英

java.util.Calendar

[英]java.util.Calendar

我的應用程序:

System.out.print("Please enter date (and time): ");
myTask.setWhen(
   input.nextInt(),
   input.nextInt(),
   input.nextInt(),
   input.nextInt(),
   input.nextInt());

我的二傳手:

public void setWhen(int year, int month, int date, int hourOfDay, int minute){
    this.year = year;
    this.month = month;
    this.date = date;
    this.hourOfDay = hourOfDay;
    this.minute = minute;

但是,當用戶准備輸入日期和時間時,它將引發異常。 另外,如果用戶輸入4/7/2013 1:30 pm(而不是4、7、2013、13、30),會發生什么? 謝謝。

代碼應該是這樣的(只是個好主意):

System.out.print("Please enter date (and time): ");
String inputText;
Scanner scanner = new Scanner(System.in);
inputText = scanner.nextLine();
scanner.close();
//parse input and parsed put as input parameter for your setwhen() method
setWhen(myOwnParserForInputFromConsole(inputText));

javadoc中nextInt引發“ InputMismatchException如果下一個標記與Integer正則表達式不匹配,或者超出范圍”。 這意味着您不能盲目調用nextInt並希望輸入將為int

您可能應該將輸入讀取為String並對該輸入執行檢查,以查看其是否有效。

public static void main(String[] args) throws IOException {
    final Scanner scanner = new Scanner(System.in);
    //read year
    final String yearString = scanner.next();
    final int year;
    try {
        year = Integer.parseInt(yearString);
        //example check, pick appropriate bounds
        if(year < 2000 || year > 3000) throw new NumberFormatException("Year not in valid range");
    } catch (NumberFormatException ex) {
        throw new RuntimeException("Failed to parse year.", ex);
    }
    final String monthString = scanner.next();
    final int month;
    try {
        month = Integer.parseInt(monthString);
        //example check, pick appropriate bounds
        if(month < 1 || month > 12) throw new NumberFormatException("Month not in valid range");
    } catch (NumberFormatException ex) {
        throw new RuntimeException("Failed to parse month.", ex);
    }
    //and the rest of the values
}

然后,當您擁有所有輸入並且已知它們有效時,請調用setWhen

顯然,您可以嘗試重新讀取數字,而不是引發異常。

使用標准的另一種方法:

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

public class Calend {

   public static void main( String[] args ) throws ParseException {
      Calendar cal = Calendar.getInstance();
      DateFormat formatter =
         DateFormat.getDateTimeInstance(
            DateFormat.FULL, DateFormat.FULL );
      DateFormat scanner;
      Date date;

      scanner = DateFormat.getDateTimeInstance(
         DateFormat.SHORT, DateFormat.SHORT );
      date = scanner.parse( "7/4/2013 21:01:05" );
      cal.setTime( date );
      System.out.println( formatter.format( cal.getTime()));

      scanner = DateFormat.getDateTimeInstance(
         DateFormat.MEDIUM, DateFormat.MEDIUM );
      date = scanner.parse( "7 avr. 2013 21:01:05" );
      cal.setTime( date );
      System.out.println( formatter.format( cal.getTime()));

      scanner = DateFormat.getDateTimeInstance(
         DateFormat.FULL, DateFormat.FULL );
      date = scanner.parse( "dimanche 7 avril 2013 21 h 01 CEST" );
      cal.setTime( date );
      System.out.println( scanner.format( cal.getTime()));
   }
}

如果要使用其他語言環境,則它在DateFormat中存在一個構造函數來處理它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM