i have taken two fields date of issue and date of maturity like this

try {
	   tDateofIssue=new JFormattedTextField(new MaskFormatter("##-##-####"));
    	catch (Exception ex) {

	try {
	tDateofMaturity=new JFormattedTextField(new MaskFormatter("##-##-####"));
    	catch (Exception ex) {

and taken both the field of date/time type in database(ms access) , to retrieve date from date from database i use a method DatetoString, it is also given below

static String DateToString(Date d){
		java.util.Formatter f=new java.util.Formatter();    	
    	return f.format("%td-%tm-%tY",d,d,d).toString();

now the problem is this i want to increment date of issue through a method so that i get date of maturity automatically, but i m unable to increment date as it is string, so plz suggest some thing

Recommended Answers

All 4 Replies

Because your dates are in a known fixed format, it's easy to use the SimpleDateFormat class to convert the String to a Date, and vice-versa. Java Dates are essentially millisecs since some arbitrary zero point, so you can do arithmetic on them. For more interesting date manipulation convert to a GregorianCalendar object, which gives you methods for adding numbers of days/weeks/months to a date while automatically dealing with the variable number of days in a month, leap years etc.

i do as you told and here is the code

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class PracDate {

  public static void main(String[] args) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-yyyy");
    GregorianCalendar gc = new GregorianCalendar();
    java.util.Date d = sdf.parse("12-11-2003");
    System.out.println("Issue Date = " + sdf.format(d));
    int dayBefore = gc.get(Calendar.DAY_OF_YEAR);
    gc.roll(Calendar.DAY_OF_YEAR, +42);
    int dayAfter = gc.get(Calendar.DAY_OF_YEAR);
    System.out.println("No Of Days =" +dayAfter);
    //if (dayAfter>30){
       gc.roll(Calendar.MONTH, +1);

    int n= gc.getActualMaximum(Calendar.DAY_OF_MONTH);


   // gc.get(Calendar.DATE);
    java.util.Date nextDate = gc.getTime();

    System.out.println("maturity Date = " + sdf.format(nextDate));



and here is the output it is giving
Issue Date = 12-11-2003
No Of Days =54
maturity Date = 23-11-2004

the problem is that it is not incrementing month

plz help i need it urgently


You may want to try using "MM" in your date string to represent the month, rather that "mm" which represents minutes ;)


From the java documentation:

M	Month in year	Month	July; Jul; 07
m	Minute in hour	Number	30
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.