Date

Any Idea's on the code behind how to prompt the user to enter date

Page 1 of 1

6 Replies - 1023 Views - Last Post: 30 April 2010 - 10:38 AM Rate Topic: -----

#1 ronakcshah.23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 15-January 10

Date

Posted 28 April 2010 - 07:00 PM

if(arrival_month == depart_month){
            String arrival_day1 = JOptionPane.showInputDialog("Enter your arrival day");
            arrival_day = Integer.parseInt(arrival_day1);

            String depart_day1 = JOptionPane.showInputDialog("Enter your Departure day");
            depart_day = Integer.parseInt(depart_day1);

            total_days_residing = depart_day - arrival_day;

        }//end of if
        else if(arrival_month > depart_month){
            JOptionPane.showMessageDialog(null, "Error! Please Try Again");
        }//end of else-if
        else if (arrival_month <= depart_month){
            String arrival_day1 = JOptionPane.showInputDialog("Enter your arrival date");
            arrival_day = Integer.parseInt(arrival_day1);
            while(arrival_day1.isEmpty()){
            String arrival_day11 = JOptionPane.showInputDialog("Enter your arrival date");
            arrival_day = Integer.parseInt(arrival_day11);
            }
            String depart_day1 = JOptionPane.showInputDialog("Enter your arrival date");
            depart_day = Integer.parseInt(depart_day1);
            while(depart_day1.isEmpty()){
            String depart_day11 = JOptionPane.showInputDialog("Enter your arrival date");
            arrival_day = Integer.parseInt(depart_day11);
            }
            
            //Sets Number of days in a month for the user's Arrival Month
            if((arrival_month == 1)||(arrival_month == 3)||(arrival_month == 5)||(arrival_month == 7)||(arrival_month == 8)||(arrival_month == 10)||(arrival_month == 12)){
                no_of_days_in_arrival_month = 31;
            }
            else if(arrival_month == 2){
                no_of_days_in_arrival_month = 28;
            }
            else{
                no_of_days_in_arrival_month = 30;
            }
            //Sets Number of Days in a month for the user's Departure Month
            if((depart_month == 1)||(depart_month == 3)||(depart_month == 5)||(depart_month == 7)||(depart_month == 8)||(depart_month == 10)||(depart_month == 12)){
                no_of_days_in_depart_month = 31;
            }
            else if(depart_month == 2){
                no_of_days_in_depart_month = 28;
            }
            else{
                no_of_days_in_depart_month = 30;
            }
            //Calculating Number of Days the user is going to stay
            total_days_residing = (no_of_days_in_depart_month - (no_of_days_in_depart_month - depart_day) + (no_of_days_in_arrival_month - arrival_day)) ;
            
        }//End of else



I want to reduce this code using date function and also i want to learn how to use that.
I got a different class prompting month.

Is This A Good Question/Topic? 0
  • +

Replies To: Date

#2 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: Date

Posted 28 April 2010 - 07:18 PM

you can use the Date class in java as well as the Calendar class
Calendar is an abstract class
for getting its instance
we can write
Calendar c = Calendar.getInstance();

this is in java.util package
Was This Post Helpful? 0
  • +
  • -

#3 immeraufdemhund  Icon User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: Date

Posted 28 April 2010 - 07:20 PM

Calendar Method

that is some useful reading to help you get starting. Pay particular attention to roll, set, and get.

Get will be very useful for you.

OH...and you can also experiment with the split method for strings.
try this.
String a = "12/10/2010";
String[] dates = a.split("/");
for (int b=0;b<dates.length;b++)
      System.out.println(dates[b]);
a = "12/10-2010";
dates = a.split("/-");
for (int b=0;b<dates.length;b++)
      System.out.println(dates[b]);



you could use that for your JOptionPane and enter a full date with any format and use Calendar.set(int year, int month, int date) method . 0=january by the way.

try that and see what happens

This post has been edited by immeraufdemhund: 28 April 2010 - 07:31 PM

Was This Post Helpful? 0
  • +
  • -

#4 ronakcshah.23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 15-January 10

Re: Date

Posted 28 April 2010 - 07:52 PM

View Postpdkharkar, on 28 April 2010 - 06:18 PM, said:

you can use the Date class in java as well as the Calendar class
Calendar is an abstract class
for getting its instance
we can write
Calendar c = Calendar.getInstance();

this is in java.util package


ok, then what am i to do?
Was This Post Helpful? 0
  • +
  • -

#5 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: Date

Posted 28 April 2010 - 07:58 PM

when you print the object c using the println method then you can get the current date and time
System.out.println(c);

or in GUI we can use the JOPtionPane
JOptionPane.showMessageDialog(this, c);

you will get current date and time instead of using the code that you have written
hope this is clear to you :)
happy coding :)
Was This Post Helpful? 0
  • +
  • -

#6 ronakcshah.23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 15-January 10

Re: Date

Posted 28 April 2010 - 08:03 PM

View Postpdkharkar, on 28 April 2010 - 06:58 PM, said:

when you print the object c using the println method then you can get the current date and time
System.out.println(c);

or in GUI we can use the JOPtionPane
JOptionPane.showMessageDialog(this, c);

you will get current date and time instead of using the code that you have written
hope this is clear to you :)
happy coding :)


Yea i got how to print the current date and time :) but i want to the user to input date and i want to store that date in a date format like when we book online for instance a hotel the arrival date??? i am having problems in parsing ..

This post has been edited by ronakcshah.23: 28 April 2010 - 08:28 PM

Was This Post Helpful? 0
  • +
  • -

#7 immeraufdemhund  Icon User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: Date

Posted 30 April 2010 - 10:38 AM

It looks like you got the parsing part correct. It just changes a string into a integer. You might want to consider changing from a JOptionPane to a JFrame with a JFormattedTextField for your input. see if you can get that to work for you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1