Results 1 to 4 of 4
  1. #1
    vodkanJava is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default Java Date Calculator

    hey guys , total java noob here but am trying write a program that will count days from a certain inputted date(thorugh Joptionpane) here's the code
    I know I'm prob doing a millions things wrong so a point in right direction would be greatly appreciated.
    basically this takes in a date and adds firstly 182(26 weeks) and then 294(42 weeks) to the date inputted. It keeps adding to sys date and not the date inputted



    import java.util.Calendar;
    import java.util.Date;
    import java.text.SimpleDateFormat;
    import javax.swing.JOptionPane;

    public class func_cal {

    public static void main(String[] args) throws Exception {


    Calendar cal;
    Calendar cal2;
    cal = Calendar.getInstance();
    cal2 = Calendar.getInstance();
    String answer = JOptionPane.showInputDialog("Please enter the Date you wish to start from");
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    Date date = formatter.parse(answer);
    cal.add(Calendar.DATE, 182);
    JOptionPane.showMessageDialog(null,"The date in 26 weeks will be :" + cal.getTime());
    cal2.add(Calendar.DATE, 294);
    JOptionPane.showMessageDialog(null,"The date in 42 weeks will be :" + cal2.getTime());

    }

    }

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    you want to add to 'date' which is the variable which holds the input date, but you are adding the figures to 'cal' and 'cal2' instead which is just the current date.

    Java Code:
    Calendar cal;
    Calendar cal2;
    cal = Calendar.getInstance();
    cal2 = Calendar.getInstance();
    ...
    cal.add(Calendar.DATE, 182);
    cal2.add(Calendar.DATE, 294);
    you need to add the value to this variable:
    Java Code:
    Date date = formatter.parse(answer);

  3. #3
    vodkanJava is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    thanks for the reply ozzy...still a total new person to Java hence some not needed code in there. Sorry I should have said that I know that the "date" value holds the input date ...its adding the 182 and 294 figures to it I can't figure out how to do

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Try this...

    Java Code:
    Calendar cal1;
    Calendar cal2;
    Date date = formatter.parse(answer);
    cal.add(date, 182)
    cal2.add(date, 294);

    .add(int field, int amount) adds an int 'amount' to a date 'field'
    Calender.getInstance() gets the current date and time according to the system time

    Read the Calendar docs for more info on how to use Calendars and available methods:
    http://download.oracle.com/javase/1..../Calendar.html

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. Java calculator problem ???
    By danielmessick in forum Advanced Java
    Replies: 2
    Last Post: 03-13-2010, 06:53 PM
  3. Java Calculator
    By aapanju in forum New To Java
    Replies: 3
    Last Post: 04-17-2008, 05:33 AM
  4. Java calculator decimal
    By cart1443 in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 01:19 PM
  5. Create a Calculator in Java
    By Albert in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 08:01 AM

Posting Permissions

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