Solved

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

Posted on 2002-03-04
7
239 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now