i want to get arrival date of a client in string and pass it as a parameter to strToCal method,this method returns an Calendar object with that date,but it wouldn't work,id get parse exception error:
static String pattern = "yyyy-MM-dd HH:mm:ss";
System.out.println("enter arrival date ("+ pattern +"):\n" );
c.setArrDate(strToCal(sc.next(),c));
System.out.println("enter departure date ("+ pattern +"):\n");
c.setResTilDate(strToCal(sc.next(),c));
static Calendar strToCal(String s, Client c) throws ParseException {
try{
DateFormat df = new SimpleDateFormat(pattern);
Calendar cal = Calendar.getInstance();
cal.setTime(df.parse(s));
return cal;
} catch(ParseException e){
System.out.println("somethings wrong");
return null;
}
Replace
sc.next()
withsc.nextLine();
because
sc.next()
will split on the first space and your input string won't be of the correct pattern.Edit I've tried this code:
with
next()
:with
nextLine()
: