Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2002-03-04
7
Medium Priority
?
301 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 400 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

916 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