How to set Date Format in GXT RowEditing? - date

Straight to the problem.
I have a 6-columns grid, one of those 6 fields being a date.
While editing is enabled DateCell is not editable, as intended, but it changes its display format from "dd/mm/yyyy", as stated in ColumnConfig, to a non-specified anywhere full date format.
How to resolve this?
Obviously I can't treat dates as strings, as I thought at first, because I'm going to lose all the possible filtering on date.

Did you try something like that?
DateTimeFormat dtf = DateTimeFormat.getFormat("yyyyMMddHHmmss");
DateField df = new DateField();
df.setPropertyEditor(new DateTimePropertyEditor(dtf));
Cell c = new DateCell(DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss"));
grid.getColumnModel().getColumn(0).setCell(c);
editing.addEditor(df);
Hope it will help. It worked for me.
EDIT: Set column date format without making cell editable.
You should try that:
ColumnConfig<Plant, Date> date = editableGrid.getColumnModel().getColumn(3);
rowEditing.addRenderer(date, new AbstractSafeHtmlRenderer<Date>() {
#Override
public SafeHtml render(Date object) {
SafeHtmlBuilder builder = new SafeHtmlBuilder();
DateTimeFormat df = DateTimeFormat.getFormat("MM-dd-yyyy");
builder.appendHtmlConstant("<div qtip='" + Format.htmlEncode(object.toString()) + "'>" + df.format(object)
+ "</div>");
return builder.toSafeHtml();
}
});

Related

Display the correct dateformat in dateItem dynamicform smartgwt

I'm taking a record fron a grid to populate some item in a dynamicform. The problem is i cannot display dates in the right format for the form, it keeps displaying then as they are in the grid and whatever i do to format the date is just ignored by SmartGWT. I'm a bit confused. Below you'll find my code. Can anyone help me out?
dataTrade = new DateItem();
dataTrade.setTitle(Nove.getInstance().getConstants().dataTrade());
dataTrade.setName(RecordEditMovTitUploadDS.DATA_TRADE);
dataTrade.setWidth(100);
dataTrade.setAlign(Alignment.LEFT);
dataTrade.setUseTextField(true);
String dataT = movTitRecord.getAttribute(ListMovTitByValCodTitDetailDS.DATA_TRADE);
DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date dateTr = null;
try{
dateTr = dateTimeFormat.parse(dataT);
} catch(IllegalArgumentException e){
SC.say("Couldn't parse date");
}
DateTimeFormat dateTimeFormat2 = DateTimeFormat.getFormat("dd/MM/yyyy");
String dateTra= dateTimeFormat2.format(dateTr);
//dataTrade.setDisplayFormat(DateDisplayFormat.TOEUROPEANSHORTDATE);
//dataTrade.setDateFormatter(DateDisplayFormat.TOEUROPEANSHORTDATE);
dataTrade.setInputFormat("dd/MM/yyyy");
dataTrade.setDefaultValue(dateTra);
To be more specific in debug the date is correctly formatted, when i pass the value to setDefaultValue it is dd/MM/yyyy as it should but when i load the page the format is still yyyy-MM-dd HH:mm:ss.SSS and i cannot understand why...
Uncommenting this line should work:
dataTrade.setDateFormatter(DateDisplayFormat.TOEUROPEANSHORTDATE);
After adding this line, the date was displaying in the correct format. Also I was able to edit the form and input dates in the format dd/MM/yyyy.

how to increment 1 value in email address for uniqueness in selenium webdriver

I am writing test script for signup page and i need to put email address on each time. Can someone help me how to increment by value 1 in the email address as i execute the script for example, test#test.com and next time value should be test1#test.com. I an try with time stamp but not successfully work.
public class GetCurrentTimeStamp
{
public static void main( String[] args )
{
java.util.Date date= new java.util.Date();
System.out.println(new Timestamp(date.getTime()));
}
}
If you are trying to provide always unique email id, then you can use date with seconds as it keep changing also you can use
System.currentTimeMillis()
which gives number always unique. so you can append/concatenate it to email, i hope you know it.
You can use below code to get date
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date)); //2016/04/19 16:05:48
depends of simple date format provide 'yyyy/MM/dd HH:mm:ss' output will be displayed.
Thank You,
Murali
Use java.util.Date class instead of Timestamp and format it like so.
String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
String email= "test"+ timestamp + "#test.com";
Use below code:-
int num = 1; // Put this stament outside the for loop or put it as global variable.
Now use below code :-
num++;
String email= "test"+ num + "#test.com";
Hope it will help you :)

Save date as timestamp in CQ5

I need t use the CQ5 xtype 'datefield' since I need only the date and not time tombe entered by the author.
But the issue is that 'datefield' saves the date in JCR as a String and not as a timestap [ as it does when 'datetime' is used.]
Is there a work around to save the date as a timestamp ?
I don't think it is possible to save the date as timestamp using datefield, without meddling with the default script. But as a workaround, you can use datetime and set the property hideTime to true, to hide the time part, so that the author will not be able to author it.
The json for the config is shown below.
{ "fieldLabel":"Date",
"xtype":"datetime",
"hideTime":true,
"name":"./date",
"defaultValue":"now",
"jcr:primaryType":"cq:Widget"
}
You can add defaultValue to 'now', if you want current date to be initialized as the default, if not explicitly filled in by the author, else it can be ignored.
NOTE: The defaultValue: 'now' doesnt work for me in IE (i am using IE 11 and emulating the previous versions through dev tools), but it works fine in Chrome and Mozilla.
A rough workaround for your jsp:
<%#page import="java.text.SimpleDateFormat,java.util.Date"%>
<%
SimpleDateFormat displayDateFormat = new SimpleDateFormat("dd MMM yyyy");
String dateField = properties.get("nameofdatefield", "");
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
Date formattedDate = sdf.parse(dateField);
String formattedDateStr = displayDateFormat.format(formattedDate);
out.println('Example of formated string'+formattedDateStr);
%>
From the above, you can also convert it to a Date Object, depending on what you wish to manipulate.
Let me know if the above example helps

Oracle preparedstatement

I am trying to save this data into oracle datebase but (Member start Date and Member end date) giving an error i am using datepicker from JPallet for date and preparedstatemet allows me in pst.setString(6, startdate.getDate()); int or string. Please help me for solving this problem. I shall be very thankful to you.
Error Lines:
pst.setString(6, startdate.getDate());
pst.setString(7, enddate.getDate());
Full method:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
con = javadb.ConnectDb();
try{
String sql = "insert into membertable(id,name,membershipno,department,idno,"
+ "startdate,enddate,officeaddress,officephone,homeaddress,homephone,dateofbirth,"
+ "fieldofinterest,remarks) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
pst = (OraclePreparedStatement) con.prepareStatement(sql);
pst.setString(1, null);
pst.setString(2, name.getText());
pst.setString(3, membershipno.getText());
pst.setString(4, department.getText());
pst.setString(5, idno.getText());
pst.setString(6, startdate.getDate());
pst.setString(7, enddate.getDate());
pst.setString(8, officeaddress.getText());
pst.setString(9, officephone.getText());
pst.setString(10, homeaddress.getText());
pst.setString(11, homephone.getText());
pst.setString(12, dateofbirth.getText());
pst.setString(13, fieldofinterest.getText());
pst.setString(14, remarks.getText());
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "Book added successfully!");
}catch(SQLException e){
JOptionPane.showMessageDialog(null, e);
}
update_table();
}
And date picker code is here
private void startdateActionPerformed(java.awt.event.ActionEvent evt) {
}
And this is not the textfield so therefore i am facing the problem. And i want to correct and also don't want to change it because it is front end reqiurement. Because user don't wants to insert date manually. Thanks.
When inserting a date, don't use pst.setString(). Instead, you should use pst.setDate() (this is really the whole point of using PreparedStatements in the first place). Because setString is inserting a date string with some unknown format. setDate tells the database to expect a date and there is no date format conversion necessary.
Also, to do this, you'll need to use a java.sql.Date instead of (what I'm assuming your palette is returning) a java.util.Date.
So this
pst.setString(6, startdate.getDate());
pst.setString(7, enddate.getDate());
should look like this
pst.setDate(6, new java.sql.Date(startdate.getDate().getTime()));
pst.setDate(7, new java.sql.Date(enddate.getDate().getTime()));

How do I convert a Date to an int?

I have this
DateFormat dateFormat = new SimpleDateFormat("MM");
Date date = new Date();
I know the value as 10 but have got no idea how to print it out. How do I convert it to an int? If there even is a way?
date.getMonth()
it is deprecated - you should be using Calendar, but if you don't want to change, that'll do it. It is 0 indexed, so remember to change the resulting value appropriately.
Javadoc