How to retrieve date only from date cells [duplicate] - java

Currently i have my code as
bean.setREPO_DATE(row.getCell(16).getDateCellValue());
it works fine if cell is formatted as date in excel.
However it also converts some integer or long like 1234 or 5699 to date. I know the reason behind this too.
However i want to apply a check before executing above line. Something like this
if(row.getCell(16).isOfDateFormat){
bean.setREPO_DATE(row.getCell(16).getDateCellValue());
}
Please Guide me..
Thanks in Advance !

Try this,
use import org.apache.poi.ss.usermodel.DateUtil;
if(DateUtil.isCellDateFormatted(cell))
{
cell.getDateCellValue();
}

Related

Issue with selecting date from calendar in Selenium

I'm trying to select current date from the calendar on this website "www.makemytrip.com".
Using these 2 lines of code:
driver.findElement(By.xpath("//label[#for='departure']")).click();
To open the calendar and to select date:
driver.findElement(By.cssSelector(".DayPicker-Day.DayPicker-Day--selected.DayPicker-Day--today")).click();
The first one is working fine as it opens up the calendar but cssSelector is not responding. I've tried various variations but still it remains unresponsive.
Try this xpath strategy:
//div[#class='DayPicker-Day DayPicker-Day--today']
Now, i write in python, so you may translate my code to Java, since most of it remains same.
time.sleep (Thread.sleep in Java) here is optional Ideally you should use WebdriverWait instead of Thread.sleep. But just to show you, I used it.
driver.get('https://www.makemytrip.com/')
time.sleep(3)
driver.find_element(By.XPATH, "//label[#for='departure']").click()
time.sleep(1)
dx = driver.find_element(By.XPATH, "//div[#class='DayPicker-Day DayPicker-Day--today']")
print(dx.text)
dx.click()
Here is the output:
17 is the date and the other value is currency.
17
₹ 9,418
Process finished with exit code 0

Java: String equals not working for innerHTML text

I am using Selenium in java for testing a search wizard where I need to click on a current date. I am trying to traverse through all the dates in the calendar and if matches the current date, I click it.
The problem here is that the dates are hidden so I have to get the innerHTML attribute, then extract the date number using substring and then comparing it with Calendar.getInstance().get(Calendar.DAY_OF_MONTH) but my condition is failing every time. Have tried str.trim() and also tried to remove all the unicode characters but nothing worked. Below is the code:
WebElement day_of_month = AutoUtils.findElementByTagName(day, "span");
if(day_of_month!=null){
String innerHtml = day_of_month.getAttribute("innerHTML");
// innerHtml is "23<span class="buzz-message">Great price</span><i></i>"
// where 23 is the date i need
String exact_date = innerHtml.substring(0,innerHtml.indexOf("<span"));
exact_date = exact_date.replaceAll("\\P{Print}", ""); // have also tried exact_date.trim();
if(exact_date.equals(Calendar.getInstance().get(Calendar.DAY_OF_MONTH))){
day_of_month.click();
} else{
System.out.Println("not found"); //gets printed every time
}
}
Can somebody help?
You have to convert the int value (day of month) to String:
if (exact_date.equals(String.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_MONTH)))) {
...
}
try using innerText instead of innerHTML . innerText only gives you what is visible to the user . innerHTML gives even html and javascripts inside it.
You should be able to get "23" only and do a easy comparison.
Also, if you can show the html , it might be easy for us to help you .

Printing dynamic amount of elements on one report with Jasper reports

I'm having trouble printing following kind of structure with JasperReports Studio/DynamicJasper:
Date (1.1.2015)
-action1 (text)
-action2
.
.
.
-actionN
-image1 (image-file)
-image1 text (text)
-image2
-image2 text
.
.
.
-imageN
-imageN text
Date (2.1.2015)
-action1 (text)
-action2
.
.
.
-actionN
-image1 (image-file)
-image1 text (text)
-image2
-image2 text
.
.
.
-imageN
-imageN text
So the original idea is to print all the actions and images attached to one day, and then do the same for the next day until there are no days in the database.Basically all of this data should be printed as one report all of the described content going to detail band(s) one day after another.
Basically there can be any amount of days, and then any amount of actions and images related to each day. It would be preferred that all of this content would be printed like there was only one column: first the list of actions and then the list of images just below the actions list.
At first I was trying to implement this Jasper Studio as that was the tool I have used so far for all the other reports I have created.
Soon I realized that, this kind of structure seems not the be possible to be created with Studio, at least as far as I see it.
Problem basically is that there seems to be no way of adding dynamic amount of tables to the report created with Studio.
So that's why I started to investigate DynamicJasper.
Feel free to correct me if I'm wrong, but could this kind of structure be implemented with DynamicJasper by using DynamicJasper's subreporting capabilities?
It looks like I definitely need at least two subreport definitions if I want data to be presented like it would look like #being printed on just one column".
This is because there will be two totally different kind of datatypes printed on these columns: actions are texts and images are image files.
So basically in my DynamicJasper code I would just iterate all of my dates coming from database and create 2 subreports for each day so i would end up creating 2*(the amount of days) subreports and concatenate all of these to my reportbuilder.
Does this sound like something that could work?
To be exact, I think I would print the description texts of the images on adjacent column next to images just to make things easier....
In general the solution is this: "in order to print report element(s) in a loop any amount of times in one report, place the element(s) to the detail band of the report and jasper will automatically print this set of elements as many times as there are beans in the collection passed for the report as main datasource/dataAdapter."
Lets do it with 2 subreport (maybe I would have done it without and used grouping) but with subreport the answer is shorter and you where already thinking about it.
If data is on a database..
In main report execute the query to find all your dates in order that you prefer.
In detail band put the date field formatted as you wish.
Below date field include a sub report, pass the date as parameter and in subreport execute query to find all actions on that date. (in its detail band put action field)
Below action subreport include a sub report, pass the date as parameter and in subreport execute query to find all images on that date. (in its detail band put image path and text)
Your done!, no use for any jr:table components...
Why would I do it with grouping?, because this way I could reduce number of queries that needs to be executed... but how gives about that for now ; )
EDIT: OP added comment that he like to use beans
The beans would be something like this (I have removed setters to decrease code posted)
Main bean the DateBean
public class DateBean {
private Date date;
private List<ActionBean> actions;
private List<ImageBean> images;
public Date getDate() {
return date;
}
public List<ActionBean> getActions() {
return actions;
}
public List<ImageBean> getImages() {
return images;
}
}
The ActionBean
public class ActionBean {
private String action;
public String getAction() {
return action;
}
}
The ImageBean
This is the almost the same has the ActionBean, so no need to post code.
Steps
Fill up the DateBean with actions and images and put it into a List<DateBean> datesBeans
Pass a new JRBeanCollectionDataSource(datesBeans) as datasource to the main report.
In detail band put the field $F{date} with some formatting
Date (1.1.2015)
Below, include a subreport for the actions and pass as datasource to subreport new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{actions}) note the class of $F{actions} needs to be java.util.List.
In subreport's detail band put the $F{action} (hence all actions past in datasource will be automatically printed, since they are in subreport's detail band).
-action1 (text)
-action2 . . .
-actionN
6-7. Repeat step 4 and 5 but with the ImageBean in another subreport
-image1 (image-file)
-image1 text (text)
-image2
-image2 text . . .
-imageN
-imageN text
And thats it...
Since the $F{date} and subreport's are in the detail band the output will be repeated on the main datasource hence the new JRBeanCollectionDataSource(datesBeans) for all our DateBean's

Display tag show user's local time in browser Jsp

I am using display tag for table details i want to change date & time to user's local browser setting, Data in DB is UMT. IF user is from india then it will add +5:30 etc. How can i do this. I am stuck in this. Please help me.
Have you tried like this ??
Locale cLoc=request.getLocale();
Calendar cCal=Calendar.getInstance(cLoc);
TimeZone tz=cCal.getTimeZone();
//......
A bit late to the party, but still answer here in case anyone else needs it.
Display.tag uses MessageFormatColumnDecorator internally to apply MessageFormat formatter to all columns that have "format" parameter set.
So, your options are:
Create your own custom MessageFormatColumnDecorator. Unfortunatelly it is inined in the ColumnTag code and cannot be very easily replaced (decorators are chained), so you will have to compile your own copy of Display.Tag jar, with your custom decorator in it.
In your custom decorator use the following sample code to change MessageFormat's timezone:
TimeZone tz = //get your timezone here
Object [] formats = format.getFormats(); //'format' is MessageFormat instance
for (int i = 0; i < formats.length; i++) {
if (formats[i] instanceof SimpleDateFormat) {
((SimpleDateFormat)formats[i]).setTimeZone(tz);
}
}
Second option is to use the <fmt:formatDate> within Display.tag column tags to format the output.
Sample Code:
<display:table name="dateList" id="object">
<display:column title="Date">
<fmt:formatDate value="${object.date}" type="both" dateStyle="long" timeStyle="long"/>
</display:column>
</display:table>
You can set default request locale for <fmt:formatDate> or wrap them in <fmt:timeZone> tag to set a timezone for them to use.

Not able to input date using sendKeys in Selenium WebDriver

The date field is like a calendar and I'm not able to input the date using sendKeys of Selenium WebDriver.
But "type" in the date field was working fine before with Selenium RC.
I tried using "clear()" before "sendKeys()" but this gave the error:
Caught Exception: Element is read-only and so may not be used for actions
Command duration or timeout: 10.11 seconds
sendKeys() is working fine for other text input fields.
I tried isDisplayed() to check for the element and it comes as true. Even in the browser, when running the test, the cursor goes to the date fields but doesnt type any text into them.
Use Following Code for this...
((JavascriptExecutor)driver).executeScript ("document.getElementById('dateofbirth').removeAttribute('readonly',0);");
WebElement BirthDate= driver.findElement(By.id("dateofbirth"));
BirthDate.clear();
BirthDate.sendKeys("20-Aug-1985"); //Enter this date details with valid date format
I also faced the same issue.This is what the solution I found.This worked fine for me.
Just remove the read only attribute of the input field and then do just as other input fields.
((JavascriptExecutor) driver).executeScript("document.getElementsByName('date'[0].removeAttribute('readonly');");
WebElement dateFld = driver.findElement(By.id("date_completed"));
dateFld.clear();
dateFld.sendKeys("date Completed");
For future readers of this thread, the solution posted by #Flaburgan for issue
https://github.com/mozilla/geckodriver/issues/1070
was found to work with Firefox 63 on Win7-64
"For the record, it looks like send_keys with a correctly formatted ISO date (yyyy-mm-dd) works. So for your example, can you please try to call send_keys with (like) 2012-11-02?"
If You are using a jQuery date picker object, the field should be read-only and date have to be selected from calendar object. In that case You can use 'Select' class methods of Selenium Web Driver to choose a date.
Select date = new Select(driver.findElement(By.linkText("the date want to select")));
date.click();
I have done this and works great. Take care of the format. By this you can even get the value from the control.
var dob = element(by.id('dateOfBirth'))
dob.sendKeys('20-08-1985');
expect(element(by.id('dateOfBirth')).getAttribute('value')).toBe('2015-20-08');
Hope it helps.

Categories