Results 1 to 5 of 5
  1. #1
    java_fun2007 is offline Member
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0

    Default Time and Date in Java

    hello,

    I want to ask the user to enter a date and time that he want. I don't know how to do that??

    I just know how to set any value I want of a date or time, but you know I can't do the same when asking the user to enter.
    how can I ask the user to enter any value he wants? please give me a coding example I don't understand java well.
    I'm a newbie the only thing I know about time and date is this:

    Thanks
    import java.util.*;
    import java.sql.Time;
    class Datess
    {

    public static void main(String s[])
    {
    Date d=new Date();
    System.out.println(d) ;

    Calendar c=new GregorianCalendar(1985,9,15);
    Date d1=c.getTime();
    System.out.println(d1) ;
    c.set(2007,9,1);
    Date d2=c.getTime();
    System.out.println(d2) ;

    Time t=new Time(12,50,0);
    System.out.println(t) ;}

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    You can ask the user to enter the date in a predefined format. Then you can palce it into a String and pass it to your GregorianCalendar calls as you show in your code block.

    Here is how you can get a String from the user via console:

    Java Code:
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String input = in.readLine();
    If your application has a GUI, you can also use following method:

    Java Code:
    ans = JOptionPane.showInputDialog(null, "Please enter date below:");

  3. #3
    java_fun2007 is offline Member
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0

    Default

    thanks for replaying, do you mean like this?

    Java Code:
    System.out.println("Enter a date: ");
    String d1;
    d1 = Stdin.readLine();
    Calendar g=new GregorianCalendar(d1);
    Date d11=g.getTime();
    System.out.println(d11) ;
    it doesn't work :(

  4. #4
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    Java Code:
    Calendar c=new GregorianCalendar(1985,9,15);
    As far as i see from the above code you have , you need to send year, month and day separately to the constructor. To do that you will need to use substring() method of String class to divide the user input into year, month and day.

    Check javadoc of String for details of using substring method.

  5. #5
    java_fun2007 is offline Member
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0

Similar Threads

  1. java.util.Date vs java.sql.Date
    By Jack in forum New To Java
    Replies: 5
    Last Post: 10-28-2010, 02:59 PM
  2. Date/Time Servlet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-14-2008, 09:34 AM
  3. File creating date/time
    By bugger in forum New To Java
    Replies: 1
    Last Post: 11-11-2007, 07:43 PM
  4. how to get the current date and time
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-03-2007, 06:05 PM
  5. Part time java Programmer-Indianapolis,IN
    By pegitha in forum Jobs Offered
    Replies: 0
    Last Post: 05-08-2007, 04:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •