Results 1 to 6 of 6

Thread: Problem calculating date

  1. #1
    Join Date
    Dec 2009
    Posts
    296

    Problem calculating date

    Hello,
    I have a problem with a date calculation in fact I have the following code.
    Code:
    String str = "";
    
        Integer d = new Integer(d);
        mn = Integer new Integer(months);
        Integer yr = new Integer(years);
    
        System.out.System.out.println("d"+ d);
        System.out.System.out.println("mn"mn +);
        System.out.System.out.println("yr"+ yr);
        Calendar nwdt = new GregorianCalendar(yr.intValue(),
                                                 mn.intValue(),
                                                 d.intValue());
        System.out.System.out.println("nwdt"+ nwdt);
        System.out.System.out.println("periodiciteMois" + PeriodiciteMois);
        nwdt.add(Calendar.MONTH, periodiciteMois);
    
        str = Utilities.makeString(nwdt.get(Calendar.DAY_OF_MONTH),2) + "." +
                                       Utilities.makeString(nwdt.get(Calendar.MONTH),2) + "." +
                                       Utilities.makeString(nwdt.get(Calendar.YEAR),4);
    
        System.out.System.out.println("str"+ str);
    Please check the code, and check if it is correct, if not then please guide me with the correct.

  2. #2
    Join Date
    Nov 2009
    Posts
    335

    Re: Problem calculating date

    Hello,
    Here is the code for doing the same, please see if this helps you.
    Code:
    newDate.add(Calendar.DATE, -1);
    int mn = newDate.get(Calendar.MONTH); mn + +;
    int dy = newDate.get(Calendar.DAY_OF_MONTH);
    int yr = newDate.get(Calendar.YEAR);
    If this works for me , see if this works for you.

  3. #3
    Join Date
    Nov 2009
    Posts
    330

    Re: Problem calculating date

    Hello,
    Currently, when I want to do calculations on dates, I create like you:
    Code:
    Calendar nwdt = new GregorianCalendar(year.intValue(),
                                                 month.intValue(),
                                                 day.intValue());
    Then, if I want the same date in 15 days, I do:
    Code:
    nwdt.add(Calendar.DATE, 15);
    And then I extract the day, month and year of the new date:
    Code:
    int Month = nwdt.get(Calendar.MONTH); Month + +;
    int Day = nwdt.get(Calendar.DAY_OF_MONTH);
    int Year = nwdt.get(Calendar.YEAR);
    I suggest this, not knowing exactly and functionally what you're looking to do with your dates.

  4. #4
    Join Date
    Nov 2009
    Posts
    347

    Re: Problem calculating date

    Hello,
    You can try alternatively using the following
    Code:
    Calendar nwdt = new GregorianCalendar(year.intValue(),
                                                 month.intValue()-1,
                                                 day.intValue());
    And when you want to retrieve the month you adds' +1'. Months are calculated, they go from 0 to 11. I hope you are understanding the things now. If you need any more help then please do post back.

  5. #5
    Join Date
    Nov 2009
    Posts
    446

    Re: Problem calculating date

    Hello,
    You can try the following code:
    Code:
    java.text.SimpleDateFormat s = new java.text.SimpleDateFormat("dd.MM.yyyy");
    java.util.Calendar nwdt = java.util.Calendar.getInstance();
    / / parse (...) method converts String to java.sql.Date
    nwdt.setTime(s.parse("08.12.2004"));
    int periodiciteMois = 12;
    nwdt.add(java.util.Calendar.MONTH, periodiciteMois);
    / / format (...) method transforms the java.sql.Date to String format jj.mm.yyyy
    String = str s.format(nwdt.getTime());
    System.out.System.out.println("str"+ str);
    Just try it and if you have any more problem then post back with the error you get.

  6. #6
    Join Date
    Dec 2009
    Posts
    192

    Re: Problem calculating date

    Hello,
    I am new to java but I have a code which may interest you. Have a look at it
    Code:
    import java.util.*;
    public class Now {
       public static void main(String[] args) {
          Date nw = new Date();
          long nwlg = nw.getTime();
          System.out.println("Value is " + nwlg);
       }
    }
    Hope this will help you and you will solve your problem.

Similar Threads

  1. Problem in date format
    By Vodka in forum Software Development
    Replies: 5
    Last Post: 19-02-2010, 04:38 AM
  2. Problem calculating duration in java
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 05-02-2010, 04:56 AM
  3. Problem with Php date function
    By HAMAKO in forum Software Development
    Replies: 1
    Last Post: 21-04-2009, 12:38 PM
  4. Calculating date in Excel
    By MarceloQuad in forum Windows Software
    Replies: 2
    Last Post: 16-03-2009, 10:56 PM
  5. Replies: 27
    Last Post: 20-01-2008, 06:32 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,987,653.83745 seconds with 17 queries