2

Currently in Object-Oriented Programming here. I'm required to prompt a user to input several particulars, including a Date of Birth in the DD/MM/YYYY format. Then, these particulars will be used to construct a Customer object to add to an ArrayList. The Date of Birth must be in a Calendar type to be used to construct a new Customer object.

Thing is, I don't have a lot of idea around Calendar. Here are my questions:

  1. How would I get a user input in a Calendar type?
  2. If what I just said is not achievable, what would?
  3. How do I prompt the user for a String date and then convert it to a Calendar?

I (kinda) know how to use date formatting, though I've no idea how to work around for a user input. Most of the stuffs I found on google and stack overflow doesn't relate to a user input (or it did and I just can't get my head around it). Here's my current code in question:

Scanner inputReg = new Scanner(System.in);

DateFormat df = new SimpleDateFormat("DD/MM/YYYY"); 

String IC = "";
String name = "";
String date = "";
Calendar dob = Calendar.getInstance();
String tel = "";

System.out.print("Enter Date of Birth (DD/MM/YYYY ): ");
date = inputReg.nextLine();

dob.setTime(date); //and here I'm stuck

System.out.println(dob);
darhdevil
  • 23
  • 1
  • 1
  • 5

4 Answers4

1

This works :

DateFormat df = new SimpleDateFormat("d/M/yyyy");
  df.getCalendar().setLenient(false);......

and when you come out of the parsing

Customer c = new Customer(IC, name, df.getCalendar(), tel);

Complete Code without Name etc

Scanner inputReg = new Scanner(System.in);
          **DateFormat df = new SimpleDateFormat("d/M/yyyy");
          df.getCalendar().setLenient(false);**
          System.out.print("Enter Date of Birth (DD/MM/YYYY): ");

          Date theDate = null;
          try {
              String date=inputReg.nextLine();
              System.out.println("date="+date+"@");
            **theDate = df.parse(date);**
        } catch (ParseException e) {
            e.printStackTrace();
        }

       ;
       Calendar dob=df.getCalendar(); // Set this inside Customer
       inputReg.close();
user1428716
  • 2,078
  • 2
  • 18
  • 37
  • That's not answering my question, unfortunately. I need the entered Date of Birth (via input.nextLine() and stuffs) to be converted into a Calendar class so I can use it to create a Customer object. – darhdevil Jan 18 '15 at 08:23
  • So...how do I incorporate a user input into it? – darhdevil Jan 18 '15 at 08:28
  • error: java.text.ParseException: Unparseable date: "10/10/2001" EDIT: Scratch that, that was my bad. Another error though: Exception in thread "main" java.util.NoSuchElementException – darhdevil Jan 18 '15 at 08:41
  • we cannot spoon feed u line by line code - this code works - u need to work the through why this exception came .. As a stand alone program it works fine .. – user1428716 Jan 18 '15 at 10:59
  • I'm not asking for anyone to spoon feed me the codes. I'm not even saying "code this for me", I'm asking for what might be wrong and why. On the contrary, codes are shoved to me without any real explanation of what they do, and they don't even answer the actual question. – darhdevil Jan 18 '15 at 11:07
0

What you need is probably something like this:

Scanner inputReg = new Scanner(System.in);
DateFormat df = new SimpleDateFormat("DD/MM/YYYY"); 

String IC = "";
String name = "";
String date = "";
Calendar dob = Calendar.getInstance();
String tel = "";

System.out.print("Enter Date of Birth (DD/MM/YYYY ): ");
date = inputReg.nextLine();

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
cal.setTime(sdf.parse(date));

System.out.println(dob);
Ofer Lando
  • 814
  • 7
  • 12
  • Error comes out as : java:89: error: unreported exception ParseException; must be caught or declared to be thrown Also, that doesn't really let me know how to convert a user input... – darhdevil Jan 18 '15 at 07:40
  • The above is a snippet of what you need to do for the conversion, so yes - you need to add the proper error handling (and it's up to you to decide if you want to catch the error here or throw it for someone else to handle). As for "how to convert a user input"- that's exactly what this snippet does - it converts the user input (in the dd-mm-yyyy format) to a Java Calendar object - isn't this what you asked for? – Ofer Lando Jan 18 '15 at 08:44
  • I'm not really sure how to incorporate that in my code. – darhdevil Jan 18 '15 at 08:46
  • See my edited answer which combines your code with my suggestion. – Ofer Lando Jan 18 '15 at 10:36
  • Hmm, there's an error (VehicleRental.java:105: error: unreported exception ParseException; must be caught or declared to be thrown) for cal.setTime(sdf.parse(date)); – darhdevil Jan 18 '15 at 10:43
  • The code you provided is only a snippet of your full class therefore any code anyone can provide here refers only to what you provided, so - again - you need to add all the error handling and decide whether you want to add try/catch in this code itself, or add a "throws" statement to whichever method you implemented the code in. – Ofer Lando Jan 18 '15 at 11:33
0

Here is a version which returns a "date" object.

Scanner inputReg = new Scanner(System.in);

    System.out.print("Enter Date of Birth (DD/MM/YYYY): ");

    Date theDate = null;
    try {
        theDate = new SimpleDateFormat("ddMMyyyy").parse(inputReg.nextLine().replaceAll("/", ""));
    } catch (ParseException e) {
        e.printStackTrace();
    }

    System.out.println(theDate);
    inputReg.close();
EDToaster
  • 3,160
  • 3
  • 16
  • 25
  • Well, code can compile but when ran, and a date (10/10/1000) is given, this large line appears: Thu Oct 10 00:00:00 CST 1000 java.util.GregorianCalendar[time=1421567082875,areFieldsSet=tr...(and trails on). Then, this error appears: Exception in thread "main" java.util.NoSuchElementException – darhdevil Jan 18 '15 at 07:45
  • are you using the date variable for anything? – EDToaster Jan 18 '15 at 07:48
  • As a matter of fact, yes. I removed the data variable, though the same error persists. – darhdevil Jan 18 '15 at 07:50
  • Can you post the whole stack trace? maybe on pastebin or something – EDToaster Jan 18 '15 at 07:51
  • can you comment out: System.out.println(dob); Customer c = new Customer(IC, name, dob, tel); customerList.add(c); – EDToaster Jan 18 '15 at 07:56
  • Done, though error persists. Unfortunately, Customer c = new Customer...and so on are required. so it'll be...very bad for them to be a problem. – darhdevil Jan 18 '15 at 08:01
  • Sorry I have no idea... My program works perfectly on my computer XD – EDToaster Jan 18 '15 at 08:01
0

Try this..hope it helps..

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class ToCalender {

    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
        try {
            cal.setTime(sdf.parse("18/01/2015"));
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println("dob : " + cal.getTime());
    }

}
Vignesh
  • 364
  • 2
  • 11
  • 19
  • That compiles and runs without errors. I've tweaked it for user input (which is the main problem I'm facing). However. I'm not actually looking to show an input date in print - I need the input date to be in a Calendar form so I can create a Customer object with it. – darhdevil Jan 18 '15 at 07:59
  • If you have a calender datatype defined in customer class correctly, your code should work..Customer c = new Customer(IC, name, dob, tel); customerList.add(c); – Vignesh Jan 18 '15 at 08:13
  • What? I'm not looking for it to print out correctly, I'm looking for it the input date of birth to be in a Calendar class. – darhdevil Jan 18 '15 at 08:22