Convert String to Date in Java and add months
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
/**
*
* @author Karthik Prabhu
*/
public class DateDemo
{
public static void main(String[] args)
{
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
try
{
Date today = df.parse("1/1/2010");
System.out.println("Date = " + df.format(today));
Calendar c=Calendar.getInstance();
c.setTime(today);
c.add(Calendar.DAY_OF_MONTH,90); //add 90 days
String str=df.format(c.getTime());
System.out.println(str);
} catch (ParseException e)
{
e.printStackTrace();
}
}
}
No comments:
Post a Comment