Results 1 to 12 of 12

Thread: accepting a DATE input

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    103

    accepting a DATE input

    what's the best way to accept a date input from a user? can i use Keyboard.readString, and somehow parse the string input as a date? if so, how?

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Use a java.util.GregorianCalendar to construct a java.util.Date from various components.
    To get the components, read in a string (however you do that, Keyboard.readString sounds like some programming class) and use a java.util.StringTokenizer to seperate them, then use Integer.valueOf to get an int.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Just to add. Wether you end up using the GregorianCalendar class or Calendar.getInstance() these classes are not intended to present the fields in a form suitable to the end user. java.text.DateFormat would take care of that issue.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Argh, I knew there was something like DateFormat, but I couldn't find it in java.util...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You can try DateFormat.parse
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    103
    i cant seem to get a string to "parse" into a date...

    String f = new String ("25/12/2000");

    Date df = new Date(f);

    no?

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    What is the result?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Posted by quipy

    i cant seem to get a string to "parse" into a date...

    String f = new String ("25/12/2000");

    Date df = new Date(f);

    no?
    Date(java.lang.String) has been deprecated

  10. #10
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I really don't think that there is a way to parse a string into a date or construct a date object from a string. All approaches seem to lead to dead ends.
    Code:
     public class DateExample{ 
      public static void main(String[] args){
       try{
        // Date to String
        Date today = new Date(); //current time and date  
        DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
        // Styles FULL LONG MEDIUM SHORT,DEFAULT
        String d = df.format(today);
        System.out.println(d);   
       
        /*
         String to Date
         Should be the same as Date(java.lang.String)
         which is deprecated but no compiler warning  
         is generated
      
       DateFormat df2 = DateFormat.getDateInstance();
       Date jan = df2.parse("01/02/03"); 
       System.out.println(jan);
       */
        
      /*
        parse(java.lang.String) in java.util.Date 
        has been deprecated
    
        long date = Date.parse("01/02/03");
        Date jandate = new Date(date);
        System.out.println(jandate);   
      
      */ 
       }catch(Exception e){;}      
      } 
     }

  11. #11
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Another dead end.
    Code:
     import java.util.Date;
     import java.util.StringTokenizer;  
    
     public class DateExample{ 
      public static void main(String[] args){
       try{
       int counter = 0; 
       int[] i = new int[3]; 
       String date = new String("01/02/03");
       StringTokenizer st = new StringTokenizer(date, "/");
       while(st.hasMoreTokens()){
         i[counter++] = Integer.parseInteger(st.nextToken()); 
       }
     
       // Date(int,int,int) in java.util.Date has been deprecated
       Date d = new Date(i[0],i[1],i[2]);
       System.out.println(d);
    
       }catch(Exception e){;}      
      } 
     }

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The DateFormat approach should work.
    The StringTokenizer approach can be combined with a GregorianCalendar or other Calendar subclass.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width