[英]Read date in Java with Scanner
好吧,我正在嘗試在同一行中使用掃描儀讀取字符串。 例如,我想閱讀:2013 年 12 月 12 日星期一,這需要放入一個字符串變量中以幫助我打印它。
在我的代碼中有這樣的:
sale.setDate(sc.next());
使用命令sc.next()
我不能以我提到的形式輸入日期,而只能以如下形式輸入:mmddyy 或 mm/dd/yyyy
我怎樣才能讀取像“2013 年 12 月 12 日星期一”這樣的整個字符串?
我對 sc.next sc.nextLine 等感到困惑。
對於掃描儀:http ://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html next() 和 hasNext() 方法首先跳過與分隔符模式匹配的任何輸入,然后嘗試返回下一個令牌。 nextLine 將此掃描器推進到當前行並返回被跳過的輸入。
使用日期 DateFormat 將字符串解析為日期; 我想 setDate 需要一個日期
Scanner scanner = new Scanner("Monday 12 December 2013,a, other fields");
scanner.useDelimiter(",");
String dateString = scanner.next();
//System.out.println(dateString);
DateFormat formatter = new SimpleDateFormat("EEEE dd MMM yyyy");
Date date = formatter.parse(dateString);
//System.out.println(date);
sale.setDate(sc.next());
即使我也遇到了同樣的問題,但之后可以使用下面提到的代碼解決。 希望能幫助到你。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
public class Datinput {
public static void main(String args[]) {
int n;
ArrayList<String> al = new ArrayList<String>();
Scanner in = new Scanner(System.in);
n = in.nextInt();
String da[] = new String[n];
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sdf.setLenient(false);
Date date[] = new Date[n];
in.nextLine();
for (int i = 0; i < da.length; i++) {
da[i] = in.nextLine();
}
for (int i = 0; i < da.length; i++) {
try {
date[i] = sdf.parse(da[i]);
} catch (ParseException e) {
e.printStackTrace();
}
}
in.close();
}
}
另一種方法是利用 Java 8 中的LocalDateTime
和DateTimeFormatter
。 在這里找到這個例子:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(yyyy-MM-dd HH:mm:ss.SSS);
LocalDateTime dateTime = LocalDateTime.parse(s.next(), formatter);
System.out.println(dateTime.format(formatter));
檢查鏈接以獲得深入的解釋!
嘗試這個..
public void readDate() throws Exception{
String dateFormat = "dd/MM/yyyy";
Scanner scanner = new Scanner(System.in);
setDate(new SimpleDateFormat(dateFormat).parse(scanner.nextLine()));
}
public void setDate(Date date){
// do stuff
}
根據需要更改 dateFormat ..
您必須使用 nextLine 來獲取帶有中間空格的單詞。
next() 只能讀取輸入直到空格。 它無法讀取由空格分隔的兩個單詞。 此外,next() 在讀取輸入后將光標置於同一行。
nextLine() 讀取輸入,包括單詞之間的空格(即,它讀取到行尾 \\n)。 讀取輸入后,nextLine() 將光標定位在下一行。
Scanner sc=new Scanner(System.in);
System.out.println("enter string for c");
String c=sc.next();
System.out.println("c is "+c);
System.out.println("enter string for d");
String d=sc.next();
System.out.println("d is "+d);
輸出:
enter string for c
abc def
c is abc
enter string for d
d is defabc def|
在第一種情況下 abc |def //光標停留在 c 之后
在第二種情況下 abc def| //光標停留在for
我發現這個問題很有趣,但沒有完全正確的答案,所以這里有一個簡單的解決方案,可以輕松讀取和解析來自Scanner
的Date
。
最好的方法是創建一個方法來為你做這件事,因為Scanner
是最終的,我們只能做一個工具箱而不是一個子類。 但這很容易使用:
public static Date readDate(Scanner sc, String format){
return new SimpleDateFormat(format).parse(sc.nextLine());
}
這將接受格式和掃描儀,不要在這里管理它,因為打開Scanner
而不關閉它會很麻煩(但如果關閉它,則無法再次打開它)。
Scanner sc = new Scanner(System.in);
ToolBox.readDate(sc, "YYYY-MM-DD");
ToolBox.readDate(sc, "YYYY-MM-DD HH:mm:ss");
ToolBox.readDate(sc, "YYYY-MM-DD");
sc.close();
這可以通過不每次都重新創建SimpleDateFormat
來改進,但是 Scanner 並沒有被調用那么多(您受到用戶輸入的限制)所以這不是那么重要。
試試這個
final Scanner sc = new Scanner(System.in);
final String timeString = sc.next();
final LocalTime time = LocalTime.parse(timeString);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.