Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

calendar date to string

Hi,

I have a setter method(setBeginDate) that changed by some one from calendar date object to String object.

now i have to change my unit test to reflect that by changing to String.

articleVisibleData.setBeginDate(setDateMonthYear(30, 1, 2015));

How i edit method above so that i get String

earlier method is

            public Calendar setDateMonthYear(int day, int month, int year) {
            Calendar cal = GregorianCalendar.getInstance();
            cal.clear();
            cal.set(Calendar.DAY_OF_MONTH, day);
            cal.set(Calendar.MONTH, month);
            cal.set(Calendar.YEAR, year);
            return cal;
      }

now i am trying like below but wont work


      public String setDateMonthYear(int day, int month, int year) {
            String str="";
            str= str.concatenate(date).concatenate(month).concatenate(year)
            return str;
      }


earlier from test case i used to call like

articleVisibleData.setDate(setDateMonthYear(30, 1, 2015));

please advise
ASKER CERTIFIED SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

i found link like
http://www.journaldev.com/692/how-to-convert-string-to-date-and-date-to-string-in-java

i tried like

      public String setDateMonthYear(int day, int month, int year) {
      DateFormat df=new SimpleDateFormat("MM/dd/yyyy");            
            Date endDay = Calendar.getInstance().getTime();
            String reportDate = df.format(endDay);
            return reportDate;
      }

but how to pass year month date to it?
articleVisibleData.setDate(setDateMonthYear(30, 1, 2015));
Avatar of dpearson
dpearson

I'm not clear on what output you expect from your method?

Have you tried this obvious solution?

            public String setDateMonthYear(int day, int month, int year) {
            Calendar cal = GregorianCalendar.getInstance();
            cal.clear();
            cal.set(Calendar.DAY_OF_MONTH, day);
            cal.set(Calendar.MONTH, month);
            cal.set(Calendar.YEAR, year);
            return cal.toString() ;

Open in new window

Avatar of gudii9

ASKER

oh let me try
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

you mean like below

      public GregorianCalendar setDateMonthYear(int day, int month, int year) {
            /*Calendar cal = GregorianCalendar.getInstance();
            cal.clear();
            cal.set(Calendar.DAY_OF_MONTH, day);
            cal.set(Calendar.MONTH, month);
            cal.set(Calendar.YEAR, year);
            return cal;*/
            return new GregorianCalendar(year, month, day);
      }

but setter is expecting String though not GregorianCalendar


Below toString approach also did not work



      public GregorianCalendar setDateMonthYear(int day, int month, int year) {
            Calendar cal = GregorianCalendar.getInstance();
            cal.clear();
            cal.set(Calendar.DAY_OF_MONTH, day);
            cal.set(Calendar.MONTH, month);
            cal.set(Calendar.YEAR, year);
            return cal.toString();
            
      }

please advise
Avatar of gudii9

ASKER

reading through below but not able to completely get what they are saying
http://stackoverflow.com/questions/1653129/converting-a-calendar-date-to-a-string
You said you were going to try this:

            public String setDateMonthYear(int day, int month, int year) {
            Calendar cal = GregorianCalendar.getInstance();
            cal.clear();
            cal.set(Calendar.DAY_OF_MONTH, day);
            cal.set(Calendar.MONTH, month);
            cal.set(Calendar.YEAR, year);
            return cal.toString() ;

Open in new window


Did you?  Did it work?

If not, what exactly failed?
If it failed, you should probably post the test code that is calling this method...

Doug
return new GregorianCalendar(year, month, day).toString();

Open in new window

to fit your current requirement
In case it's not clear CEHJ's and what I posted are equivalent - his is just better code and I just copied yours :)

Doug
Avatar of gudii9

ASKER

Did you?  Did it work?

it did not work.

i was told to just hard code everywhere date like
"20170516" etc which worked obviously
Avatar of gudii9

ASKER

i am curious why below approach did not work

 public String setDateMonthYear(int day, int month, int year) {
            Calendar cal = GregorianCalendar.getInstance();
            cal.clear();
            cal.set(Calendar.DAY_OF_MONTH, day);
            cal.set(Calendar.MONTH, month);
            cal.set(Calendar.YEAR, year);
            return cal.toString() ;
i am curious why below approach did not work

Did you try running the method?  The toString() from calendar is far from this format: "20170516"

Perhaps give this little guy a spin?
	public String setDateMonthYear(int day, int month, int year) {
		String str = String.format("%4d%02d%02d", year, month, day) ;
		return str ;
	}

Open in new window

Avatar of gudii9

ASKER

let me try
it did not work.
What does that actually mean?
Avatar of gudii9

ASKER

it did not work.
What does that actually mean?

assertion failied as jnit thought left hand side thing different from right hand side object
A String is a String ;)
Avatar of gudii9

ASKER

Perhaps give this little guy a spin?
      public String setDateMonthYear(int day, int month, int year) {
            String str = String.format("%4d%02d%02d", year, month, day) ;
            return str ;
      }


that little guy worked like champ

      
public String setDateMonthYear(int day, int month, int year) {
		String str = String.format("%4d%02d%02d", year, month, day) ;
		return str ;
	}

Open in new window


above do i supposed to make as below instead?

	public String setDateMonthYear(int day, int month, int year) {
		String str = String.format("%2d%02d%02d", year, month, day) ;
		return str ;
	}

Open in new window



as i am calling like

articleVisibleData.setBeginDate(setDateMonthYear(30, 1, 2015));
//as 30 is date then followed by 1 which is january month foloowed by year 2015??
Avatar of gudii9

ASKER

one weird thing is even after commenting below line in my test

articleVisibleData.setBeginDate(setDateMonthYear(30, 1, 2015));

my test passed which makes me think this BeginDate is optional field not mandatory field. How to double check it some where in interface document?
assertion failied as jnit thought left hand side thing different from right hand side object
Why, if that was the problem, would it matter how the String was generated?
Can't help you debug the rest of your code/interfaces.

But in your example of a hard coded string: "20170516"
the year (2017) is 4 digits long.

Hence %4d for the first value - the year - in the format string.

My advice - now that you have a function that is passing your tests, stop messing with it :)

Doug
Avatar of gudii9

ASKER

public String setDateMonthYear(int day, int month, int year) {
            String str = String.format("%4d%02d%02d", year, month, day) ;
            return str ;
      }

above worked for me