get date 30 days ago for today

HiI would like to be able to get todays date, but 30 days ago in the past.Heres an example:2005/09/30 (year/month/day)I would also like to print/show this on the webpage too :)ThanksTom
[234 byte] By [t0m_t4yl0ra] at [2007-10-2 9:38:09]
# 1
Don't get it at all. Please reorganize your sentences.
JavaWorkera at 2007-7-16 23:44:16 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
// todays dateCalendar rightNow = Calendar.getInstance();// todays date -30 daysrightNow.add(Calendar.DATE, -5);-S
slenzia at 2007-7-16 23:44:16 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
this is what I meant.rightNow.add(Calendar.DATE, -30);
slenzia at 2007-7-16 23:44:16 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
kewl! :DNow i dont just want the date to change, i would like the month or even the year to roll back to 30 days ago if required, so for example i would like 30 days in the past from todays date: 2006/01/16 which would return 2005/12/14Thanks
t0m_t4yl0ra at 2007-7-16 23:44:16 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
See for example Date manipulation stuff in Coldtags suite: http://www.servletsuite.com/jsp.htm
dnamiota at 2007-7-16 23:44:16 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
HeyOk thanks for that, would rather have the jsp code than installing a jar and tld.ThanksTom
t0m_t4yl0ra at 2007-7-16 23:44:16 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

Hi friends,i am getting the problem form last 10 days,but i didn't get the solution for that problem.

My problem is

In the Struts application i have created a select box(combo box) the text in the select box is large.when i want to go to select the text .the total text will appear. it is just like mouse over on the text.

i know how to create tool tip.for the select box i am having idea for that.

plese find that problem

urvardhana at 2007-7-16 23:44:16 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
Thats a javascript problem (client side) which has nothing to do with my post, go and post your own! :@Any1 else have a solution for me (not another problem)Tom
t0m_t4yl0ra at 2007-7-16 23:44:16 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

Well you saw how I subtracted 30 days from the currect date by using the following...

rightNow.add(Calendar.DATE, -30);

You can add and sutract from the month and year fields as well.

rightNow.add(Calendar.MONTH, 2); // add two months

rightNow.add(Calendar.YEAR, -1); // subtract one year

slenzia at 2007-7-16 23:44:16 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

One point that wasn't mentioned--the code below *automatically* takes care of rolling over the date. You don't need to keep track of leap years, months/years changing etc (amazing what sun's libraries can do, heh. I just discovered I don't have to write my own splitstring function)

Also, you mentioned earlier that you didn't want to include any jars...I don't quite follow what you're saying. For a jsp, all you need at the top is: <%@ page import="java.util.GregorianCalendar" %>

Then just get an instance of the class & use the add method.

> Well you saw how I subtracted 30 days from the

> currect date by using the following...

> > rightNow.add(Calendar.DATE, -30);

>

>

> You can add and sutract from the month and year

> fields as well.

> > rightNow.add(Calendar.MONTH, 2); // add two months

> rightNow.add(Calendar.YEAR, -1); // subtract one

> year

>

radtad82a at 2007-7-16 23:44:16 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11

hmmm none of that code seems to work, even with <%@ page import="java.util.GregorianCalendar" %> included

the problem with that code is it will nock back each date, month and year all individual, i want it all to work together as the following:

for example i would like 30 days in the past from todays date:

2006/01/16

which would return

2005/12/14

So basically i set i want to go back 30 days, but the month AND year also change depending on the date if we need to roll back to the previous month or even the previous year

Thanks

Tom

t0m_t4yl0ra at 2007-7-16 23:44:16 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12
find related java stuff here... http://www.javapeople.blogspot.com
dipalia at 2007-7-16 23:44:16 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 13

that doesnt help.

five minutes of somebodys time would be fantastic in a script that would work to do the above.

it seems not many people like to help others who are new to jsp as i have seen in many posts, which probably proves a point and why many prefer php and the community that comes with it.

shame really

t0m_t4yl0ra at 2007-7-16 23:44:16 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 14

The solution was given to you so I'm not sure what your problem is.

It appears to me that you have simply demonstrated what all new programmers do in this forum; you want someone to write your code for you and your too **** lazy to read the API for yourself.

http://java.sun.com/j2se/1.5.0/docs/api/index.html

The following code rolls back the date by 30 days and at the same time automatically calculates the correct year and month.

import java.util.*;

import java.text.*;

public class DateTest {

public static void main(String[] args){

DateTest d = new DateTest();

d.doWork();

}

public DateTest(){}

public void doWork(){

System.out.println("Peforming date rollback...");

// get current data & time

GregorianCalendar gcal = new GregorianCalendar();

System.out.println("Date: " + formatDate(gcal.getTime(),"MM/dd/yyyy HH:mm:ss a"));

// roll back 30 days

gcal.add(Calendar.DATE,-30);

System.out.println("Date: " + formatDate(gcal.getTime(),"MM/dd/yyyy HH:mm:ss a"));

}

// formats a date, see SimpleDateFormat class for futher information

private String formatDate(Date d, String format){

if (d != null) {

SimpleDateFormat sdf = new SimpleDateFormat(format);

FieldPosition pos = new FieldPosition(0);

StringBuffer buf = new StringBuffer();

buf = sdf.format(d,buf,pos);

return buf.toString();

}

return "";

}

}

slenzia at 2007-7-16 23:44:16 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 15

slenzi

I know it is irritating when new java developers need to be spoon fed but it does really help sometimes.

Fantastic answer.

I have been battling with Java Dates and wondering how to convert between Date and GregorianDate etc.

This is a great response.

Tkman

tkmana at 2007-7-20 20:20:30 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...