0

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;
    }
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
shayan
  • 29
  • 6
  • what's the type of c and what's the implementation of Client? – SMA Dec 08 '14 at 09:29
  • can you log your exception and add it to your post? – user902383 Dec 08 '14 at 09:29
  • c is an object of client object, public class Client extends Person implements Serializable { Client(){ this.payBill=0; } private String pattern = "yyyy-MM-dd HH:mm:ss"; private long payBill; private Room room; private Calendar arrDate,resTilDate; @almasshaikh – shayan Dec 08 '14 at 09:31
  • 1
    this code should work as long as you provide the correct input string. the strings must respect your pattern. so enter dates as 2014-05-16 13:30:00. the problem is sc.next(), you need sc.nextLine(), because sc.next() will split on the first space – MihaiC Dec 08 '14 at 09:32
  • @MihaiC but it doesn't – shayan Dec 08 '14 at 09:33
  • @shayan check and try answer – MihaiC Dec 08 '14 at 09:34
  • @shayan Please add code into question. Paste whole client class not just first few lines. – SMA Dec 08 '14 at 09:35
  • Print the String before conversion, will let you know whether actual date is available or not. – Sivakumar Dec 08 '14 at 09:39

1 Answers1

2

Replace sc.next() with sc.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:

public class Test4 {
    static String pattern = "yyyy-MM-dd HH:mm:ss";

    public static void main(String[] args) {
        Calendar c = Calendar.getInstance();
        final Scanner input = new Scanner(System.in);
        System.out.println("input date: ");
        String a = input.nextLine();
        c = strToCal(a);
        System.out.println(c.getTime());
    }


    static Calendar strToCal(String s) {
        try {
            DateFormat df = new SimpleDateFormat(pattern);
            Calendar cal = Calendar.getInstance();
            cal.setTime(df.parse(s));
            return cal;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
}

with next():

input date:
2014-05-16 13:30:00
java.text.ParseException: Unparseable date: "2014-05-16"
        at java.text.DateFormat.parse(Unknown Source)

with nextLine():

input date:
2014-05-16 13:30:00
Fri May 16 13:30:00 EEST 2014
MihaiC
  • 1,618
  • 1
  • 10
  • 14
  • that doesn't work either,when i put nextLine() instead of next() it won't get the input and prints the parse exception error – shayan Dec 08 '14 at 09:38
  • i've posted entire example code. It works with nextLine(). Are you using nextInt() somewhere before this? If so, create two different scanners, one for int and one for strings. nextLine() doesn't work correctly if you invoke nextInt() before – MihaiC Dec 08 '14 at 09:42