gudii9
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.setBegi nDate(setD ateMonthYe ar(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.getInsta nce();
cal.clear();
cal.set(Calendar.DAY_OF_MO NTH, 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).conc atenate(mo nth).conca tenate(yea r)
return str;
}
earlier from test case i used to call like
articleVisibleData.setDate (setDateMo nthYear(30 , 1, 2015));
please advise
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.setBegi
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.getInsta
cal.clear();
cal.set(Calendar.DAY_OF_MO
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).conc
return str;
}
earlier from test case i used to call like
articleVisibleData.setDate
please advise
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I'm not clear on what output you expect from your method?
Have you tried this obvious solution?
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() ;
ASKER
oh let me try
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
you mean like below
public GregorianCalendar setDateMonthYear(int day, int month, int year) {
/*Calendar cal = GregorianCalendar.getInsta nce();
cal.clear();
cal.set(Calendar.DAY_OF_MO NTH, 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.getInsta nce();
cal.clear();
cal.set(Calendar.DAY_OF_MO NTH, day);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.YEAR, year);
return cal.toString();
}
please advise
public GregorianCalendar setDateMonthYear(int day, int month, int year) {
/*Calendar cal = GregorianCalendar.getInsta
cal.clear();
cal.set(Calendar.DAY_OF_MO
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.getInsta
cal.clear();
cal.set(Calendar.DAY_OF_MO
cal.set(Calendar.MONTH, month);
cal.set(Calendar.YEAR, year);
return cal.toString();
}
please advise
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
http://stackoverflow.com/questions/1653129/converting-a-calendar-date-to-a-string
You said you were going to try this:
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
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() ;
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();
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
Doug
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
ASKER
i am curious why below approach did not work
public String setDateMonthYear(int day, int month, int year) {
Calendar cal = GregorianCalendar.getInsta nce();
cal.clear();
cal.set(Calendar.DAY_OF_MO NTH, day);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.YEAR, year);
return cal.toString() ;
public String setDateMonthYear(int day, int month, int year) {
Calendar cal = GregorianCalendar.getInsta
cal.clear();
cal.set(Calendar.DAY_OF_MO
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 ;
}
ASKER
let me try
it did not work.What does that actually mean?
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 ;)
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 ;
}
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 ;
}
as i am calling like
articleVisibleData.setBegi//as 30 is date then followed by 1 which is january month foloowed by year 2015??nDate(setD ateMonthYe ar(30, 1, 2015));
ASKER
one weird thing is even after commenting below line in my test
articleVisibleData.setBegi nDate(setD ateMonthYe ar(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?
articleVisibleData.setBegi
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 objectWhy, 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
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
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
ASKER
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/yy
Date endDay = Calendar.getInstance().get
String reportDate = df.format(endDay);
return reportDate;
}
but how to pass year month date to it?
articleVisibleData.setDate