Solved

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

Posted on 2002-03-04
7
252 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping1 challenge 7 73
Impossible to extract MSI from new JAVA releases 2 51
Checkbox and ListView in Android Layout 4 56
xampp tool 12 30
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

863 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

22 Experts available now in Live!

Get 1:1 Help Now