Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-03-2007, 06:08 PM
Member
 
Join Date: Nov 2007
Posts: 35
java_fun2007 is on a distinguished road
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
Quote:
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) ;}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-03-2007, 06:52 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
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:

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:

Code:
ans = JOptionPane.showInputDialog(null, "Please enter date below:");
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-03-2007, 07:54 PM
Member
 
Join Date: Nov 2007
Posts: 35
java_fun2007 is on a distinguished road
thanks for replaying, do you mean like this?

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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-03-2007, 10:24 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-06-2007, 08:25 PM
Member
 
Join Date: Nov 2007
Posts: 35
java_fun2007 is on a distinguished road
Thank you very much JavaBean I got it now !
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Date/Time Servlet Java Tip Java Tips 0 01-14-2008 10:34 AM
File creating date/time bugger New To Java 1 11-11-2007 08:43 PM
how to get the current date and time valery New To Java 1 08-03-2007 07:05 PM
java.util.Date vs java.sql.Date Jack New To Java 1 07-09-2007 04:22 AM
Part time java Programmer-Indianapolis,IN pegitha Jobs Offered 0 05-08-2007 05:01 PM


All times are GMT +3. The time now is 04:18 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org