Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to get a java date which is greater than the current date.

Posted on 2002-03-04
7
268 Views
Last Modified: 2010-03-31
Hello,
 Requirement is,I need to get a date which is x number of days after the current date,in MM/dd/yyyy format.
Any help would be appreciated
Priyan
0
Comment
Question by:priyan
7 Comments
 
LVL 3

Accepted Solution

by:
saxaboo earned 100 total points
ID: 6838834
import java.util.*;

...

int x = 10;
Calendar rightNow = Calendar.getInstance();
rightNow.add(Calendar.DAY_OF_YEAR,x);
Date d = rightNow.getTime();

//d now contains "now+x days"
//let's format it
java.text.SimpleDateFormat f = new java.text.SimpleDateFormat("MM/dd/yyyy");
String s = f.format(d);

//do something with s

I didn't compile this snippet, but 've used the technique many times

-S
0
 

Expert Comment

by:ebrahim_mkr
ID: 6838835
the following code will add a 30 days over the current day of today try it

  Calendar calender1 = Calendar.getInstance();
  Calendar calender2 = Calendar.getInstance();

  calender2.set(0, 0, 30, 0, 0, 0);

  java.util.Date date= new java.util.Date(calender1.getTime().getTime() + calender2.getTime().getTime());


at last the variable date will contain the desired date
0
 
LVL 4

Expert Comment

by:Neutron
ID: 6838848
more or less like the others, only with a difference in understanding what you need. this is for free :o)


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

        try
        {
            String sDate = "12/28/2347";
           
            SimpleDateFormat formatter = new SimpleDateFormat( "MM/dd/yyyy" );
            formatter.setCalendar( new GregorianCalendar() );
            formatter.parse( sDate );
           
            Calendar calendar = formatter.getCalendar();
           
            System.out.println( formatter.format( calendar.getTime() ) );
            calendar.add( Calendar.DATE, 7 );
            System.out.println( formatter.format( calendar.getTime() ) );
        }
        catch( Exception e )
        {
            System.out.println( "Error: "+e.getMessage() );
        }


Greetings,
    </ntr> :)
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 4

Expert Comment

by:Neutron
ID: 6838866
more or less like the others, only with a difference in understanding what you need. this is for free :o)


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

        try
        {
            String sDate = "12/28/2347";
           
            SimpleDateFormat formatter = new SimpleDateFormat( "MM/dd/yyyy" );
            formatter.setCalendar( new GregorianCalendar() );
            formatter.parse( sDate );
           
            Calendar calendar = formatter.getCalendar();
           
            System.out.println( formatter.format( calendar.getTime() ) );
            calendar.add( Calendar.DATE, 7 );
            System.out.println( formatter.format( calendar.getTime() ) );
        }
        catch( Exception e )
        {
            System.out.println( "Error: "+e.getMessage() );
        }


Greetings,
    </ntr> :)
0
 
LVL 92

Expert Comment

by:objects
ID: 6840659
I don't think the proposed answer will work as it does not take the timezone into account properly.
The other two suggestions seem like better approaches.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6884016
QUESTION IS LOCKED, IF IT DOES NOT HELP YOU, PLEASE REJECT IT AND ADD COMMENTS AS TO WHAT ELSE IS NEEDED.

ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you. You must tell the participants why you wish to do this, and allow for Expert response.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question. Again, please comment to advise the other participants why you wish to do this.

For special handling needs, please post a zero point question in the link below and include the question QID/link(s) that it regards.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Please click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer but does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item in the future to seek help.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20016306.html
http://www.experts-exchange.com/questions/Q.20054366.html
http://www.experts-exchange.com/questions/Q.20065488.html
http://www.experts-exchange.com/questions/Q.20094725.html
http://www.experts-exchange.com/questions/Q.20095208.html
http://www.experts-exchange.com/questions/Q.20130003.html
http://www.experts-exchange.com/questions/Q.20186718.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20064506.html
http://www.experts-exchange.com/questions/Q.20099389.html
http://www.experts-exchange.com/questions/Q.20272988.html

PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643

Moderators will finalize this question if still open in 7 days, by either moving this to the PAQ (Previously Asked Questions) at zero points, deleting it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange

P.S.  For any year 2000 questions, special attention is needed to ensure the first correct response is awarded, since they are not in the comment date order, but rather in Member ID order.
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6973608
Force-accepted by
Netminder
CS Moderator
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
javap not working 8 58
Tomcat: Unable to run tomcat service. 2 23
printf performancy 11 67
web project error add remove 1 27
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question