Solved

ADD 180 Days to Current Date

Posted on 2004-08-23
21
821 Views
Last Modified: 2010-05-18
I use
SimpleDateFormat sdf_new = new SimpleDateFormat("MM/dd/yyyy");
String date = sdf_new.format(new Date());

to display the current date in a jsp page.

How can I add 180 days to the resultant value so that the date value would equal 180 days from the current date?

RCMB
0
Comment
Question by:rcmb
  • 9
  • 7
  • 2
  • +3
21 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 11873929
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 180);
sdf.format(cal.getTime());
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 11873942
SimpleDateFormat sdf_new = new SimpleDateFormat("MM/dd/yyyy");
GregorianCalendar cal = new GregorianCalendar();
cal.set(Calendar.DATE, cal.get(Calendar.DATE) + 180);
String date = sdf_new.format(cal.getTime());
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 11873944
oh yeah... cal.add is better ;)
0
 
LVL 19

Expert Comment

by:ramazanyich
ID: 11873991
Calendar cal = Calendar.getInstance();
Date d = new Date();
cal.setTime(d);
cal.add(Calendar.DAY_OF_YEAR,180);
String date = sdf_new.format(cal.getTime());
0
 
LVL 12

Author Comment

by:rcmb
ID: 11874072
CEHJ,

I get the following error

cal.add(Calendar.DATE, 180);
          ^

and
 

sdf.format(cal.getTime());
               ^

am I missing an import or something?

RCMB
0
 
LVL 12

Author Comment

by:rcmb
ID: 11874111
Here is my actual code now -- please point me in the correct direction.

SimpleDateFormat sdf_new = new SimpleDateFormat("MM/dd/yyyy");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 180);
sdf.format(cal.getTime());
String date = sdf_new.format(new Date());

RCMB
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11874121
import java.util.Calendar;
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11874136
>>sdf.format(cal.getTime());

needs to be assigned to something of course:

String yourDate = sdf.format(cal.getTime());
out.println(yourDate);
0
 
LVL 12

Author Comment

by:rcmb
ID: 11874152
I already have import java.util.* in my import statement.

RCMB
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11874205
Please show your (exact) current code and errors
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 86

Expert Comment

by:CEHJ
ID: 11874224
>>String date = sdf_new.format(new Date());

The above line of course just shows the current date
0
 
LVL 12

Author Comment

by:rcmb
ID: 11874274
Here is my exact code:

SimpleDateFormat sdf_new = new SimpleDateFormat("MM/dd/yyyy");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE,180);
sdf.format(cal.getTime());
String date = sdf_new.format(cal.getTime());


My errors are:

Generated servlet error:
Compiling 1 source file
C:\jboss-3.2.3\server\default\work\MainEngine\localhost\personnel\jsp\prd_eaos_jsp.java:29 <idenitifer> expected
cal.add(Calendar.DATE,180);
          ^
C:\jboss-3.2.3\server\default\work\MainEngine\localhost\personnel\jsp\prd_eaos_jsp.java:30 <idenitifer> expected
sdf.format(cal.getTime());
               ^
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11874318
Try

java.util.Calendar cal = java.util.Calendar.getInstance();
cal.add(java.util.Calendar.DATE, 180);
0
 
LVL 12

Author Comment

by:rcmb
ID: 11874800
Okay,

I have tried about every possible combination I can come up with. I am running JBoss 3.2.3 and not matter what I do the system fails on cal.add(

For some reason it gives me an identifier error.

I have searched the web and my syntext is correct (just as all of you have provided).

I tried just doing this
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE,180);

and it fails with the same error code.

I tried adding the java.util.... to the beginning of the code and it does not change the error.

VBScript is so much easier :-(

Any ideas?

RCMB
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11874833
If you remove all code concerning Calendar, do the errors disappear?
0
 
LVL 12

Author Comment

by:rcmb
ID: 11874919
Yes - When I just have the SimpleDateFormat sdf_new...... and String date = .....

The page opens fine with the current date in the field box.

Curtis
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11875160
Better post your jsp, including headers
0
 
LVL 92

Expert Comment

by:objects
ID: 11877237
Try this page:



<%@ page language="java" contentType="text/html" %>
<%@ page import="java.text.*" %>
<%@ page import="java.util.*" %>


<%
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE,180);
%>

<%= sdf.format(cal.getTime()) %>
0
 

Expert Comment

by:bozhidar
ID: 11877561
Try this one:

Calendar cal = GregorianCalendar.getInstance();
instead of:
Calendar cal = Calendar.getInstance();

and the rest of the code remains the same.
0
 
LVL 12

Author Comment

by:rcmb
ID: 11951522
Okay - thanks to all for the help but CEHJ's initial answer was all that was needed. The real problem was I did not have the Java bin folder in my system path. Once I added that my errors disappeared and all worked.

Thanks to all for your patience.

R/Curtis
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11951538
:-)
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
JUnit 4 @Before and @BeforeClass differences 3 48
simple java question 3 44
github account with ecipse 1 42
split string containing \r\n in Java 46 38
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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 about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

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

20 Experts available now in Live!

Get 1:1 Help Now