Solved

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

Posted on 2002-03-04
7
285 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
[X]
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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

624 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