Finding a new Date


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Finding a new Date

  1. #1
    Join Date
    Aug 2003
    Posts
    313

    Finding a new Date

    I have beek working on a program that is going to do math with dates, and for some reason I can't get the Date/Timestamp stuff working. I want to accept the month day year and a number of days to add to it and determine the new month day and year. Here is what I have:
    Code:
    Date d1 = new Date(new GregorianCalendar(y,m-1,d).getTime().getTime() + n*1000*60*60*24);
            System.out.println(d1.getMonth()+1+"/"+d1.getDate()+"/"+(d1.getYear()+1900));
    for the life of me I can't figure out what is going on. Can someone help? Thanks a lot.
    ~evlich

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    eee.. you do make it hard for yourself...
    Code:
    import java.text.*;
    import java.util.*;
    
    public class t1{
      public static void main(String[] argv)throws Exception{
        String oldDate = argv[0];
      
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        Date d = sdf.parse(oldDate);
      
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(d);
      
        gc.roll(gc.DAY_OF_YEAR, Integer.parseInt(argv[1]));  //most important!
      
        String newDate = sdf.format( gc.getTime() );
      
        System.out.print(argv[1]+" days after "+oldDate+" is "+newDate);
      }
    }
    edit; code updates to a fully functioning class
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  3. #3
    Join Date
    Aug 2003
    Posts
    313
    That code will only modify the day field, it doesnt' update month or year fields. How would i combine the other fields in the addition as well. Thanks a lot.
    ~evlich

  4. #4
    Join Date
    Feb 2004
    Posts
    808
    Originally posted by evlich
    That code will only modify the day field, it doesnt' update month or year fields. How would i combine the other fields in the addition as well. Thanks a lot.
    to some extent, you are correct. im sure that it would have taken you less time to read the api:
    http://java.sun.com/j2se/1.4.2/docs/...nCalendar.html

    and see that you can substitute add() for roll() to realise reliable addition (roll does not increment larger fields unless specifically told to do so), than the time it took to make your post..
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center