Discussion Locked

Calculating with Date

0 helpful
2 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I am searching a possibility in Java to the following:

Give me the date of today minus 3 days
Give me the date of today plus 4 days

Is there a way to do it without manually calculate
-which month is it (has it 30/31/28 days)
-is it a leap year
etc etc

thanks in advance

2 replies

by mario.widmer

public class Hauptfenster
{
public static void main(String[] args)
{
sys cal = new GregorianCalendar();
showDate(cal); //aktueller Tag
cal.add(Calendar.DATE, -1); //minus 1 Tag
showDate(cal); //ergibt

}

public static void showDate(Calendar cal)
{
String day = "", month="", year="";
day += cal.get(Calendar.DATE) + ".";
if (day.length()<3) day="0"+day; //wenn tag <10 stelle 0 vornedran
month += (cal.get(Calendar.MONTH)+1) + ".";
if (month.length()<3) month="0"+month; //wenn monat <10 stelle 0 vornedran
year += cal.get(Calendar.YEAR);
System.out.println(day+month+year);
}
}

This discussion is closed to new replies.