1,105,286 Community Members

problem with handling date

Member Avatar
harsimran05
Light Poster
25 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

try {
	   tDateofIssue=new JFormattedTextField(new MaskFormatter("##-##-####"));
	
	    tDateofIssue.setBounds(200,130,125,30);
	    c.add(tDateofIssue);
	    }
    	catch (Exception ex) {
    	     ex.printStackTrace();
	   }




	try {
	tDateofMaturity=new JFormattedTextField(new MaskFormatter("##-##-####"));
	tDateofMaturity.setBounds(200,400,125,30);
	c.add(tDateofMaturity);
	tDateofMaturity.addFocusListener(this);
	 }
    	catch (Exception ex) {
           ex.printStackTrace();
	   }

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

Member Avatar
JamesCherrill
... trying to help
10,362 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
1
 

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.

Member Avatar
harsimran05
Light Poster
25 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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");
    gc.setTime(d);
    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);

     //if(gc.MONTH==12)  
         gc.add(Calendar.YEAR,+1);


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

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

  }

}

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

the problem is that it is not incrementing month

Member Avatar
harsimran05
Light Poster
25 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

plz help i need it urgently

Member Avatar
JeoSaurus
Posting Whiz in Training
227 posts since Sep 2008
Reputation Points: 32 [?]
Q&As Helped to Solve: 36 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello!

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

SimpleDateFormat("dd-MM-yyyy")

From the java documentation:

M	Month in year	Month	July; Jul; 07
m	Minute in hour	Number	30
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: