Set date of jDateChooser - null value produced - java

I'm getting data from Microsoft Access and reading it into an object array and then displaying it on a list. I'm trying to set the date of a jDateChooser using the String from the object. The first time I try to set the date, nothing is set/selected. If I change the date on my GUI, save it to access and try to set the date again, then it works? Not sure why this is the case. Please help (:
String apUpdateDate = appointments[appointmentPos].getAppointmentDate();
try
{
Date updateDate = new SimpleDateFormat("dd/MM/yyyy").parse(apUpdateDate);
}
catch (ParseException ex)
{
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
}

Related

Input error when trying to set an integer in mysql Database. (IllegalArgumentException: null PK instance)

I am trying to input some data into a mysql database. However, when I try to run the program I get this error:java.lang.IllegalArgumentException: An instance of a null PK has been incorrectly provided for this find opertaion, I suspect this is due to event.setPriority(prist); however I am not experienced enough to tell why and how I can fix it.
try{ Calendar duedate = Calendar.getInstance();
Listevent event = new Listevent();
TimeTableAddEvent tb = new TimeTableAddEvent();
int prist = Integer.parseInt(PriTxt.getText());
event.setName(EventTxt.getText());
event.setDescription(DescTxt.getText());
event.setPriority(prist);
event.setDateofdue(DateTxt.getDate());
EntityManagerFactory emf = Persistence.createEntityManagerFactory("TimeEven DataBasePU");
ListeventJpaController lejc = new ListeventJpaController((emf));
lejc.create(event);
}catch(Exception e){
JOptionPane.showMessageDialog(null,e);
}

com.ibm.icu.text.DecimalFormat always throws ParseException

With the following code I only want to allow positive numbers. For some reason i am not even able to parse the strings correctly:
DecimalFormat dfNoNegative = new DecimalFormat("#,##0.00");
dfNoNegative.setNegativePrefix("");
try {
System.out.println(dfNoNegative.parse("123.00"));
} catch (ParseException e) {
System.out.println(e.getMessage());
System.out.println(e.getErrorOffset());
e.printStackTrace();
}
Error message and ErrorOffset:
Unparseable number: "123.00"
6
Can anyone guide me where I am mistaken? An example for a working String would be good as well
My mistake was to dfNoNegative.setNegativePrefix(""); to nothing (""). This doesn't work, because the String started directly with the number and 123 is not "", and therefore it fails. Basically this method overwrites what should be used as negative prefix (default is -). If you set it to ! as example, System.out.println(dfNoNegative.parse("!123.00")); would print -123.

JFormattedTextField set value and text null after inputting wrong format

I'm working with Java Application with JFormattedTextField. I have some problems that:
How to set JFormattedTextField with can accept "/" and "-"
I want to set jformattedtextfield value and text to null or "" after entering wrong format.
Example:
2.1.JFormattedTextField ftf .... with format ("yyyy-MM-dd")
2.2. Input right format: 1990-5-6
2.3. Leave ftf
2.4. Then focus ftf and input wrong format: 5-6-1990
2.5. Leave ftf then it recognize wrong format but return something else
But what I want is that I must be null or empty.
How can I do it?
Any suggestions would be appreciated!
This is my first answer on this forum and I hope I don't mess up. For the first question, you can use MaskFormatter. This will also ensure that the value is never entered wrong.
new JFormattedTextField(createFormatter("####-##-##"));
private static MaskFormatter createFormatter(String s) {
MaskFormatter formatter = null;
try {
formatter = new MaskFormatter(s);
formatter.setPlaceholderCharacter('0');
} catch (java.text.ParseException exc) {
System.err.println("formatter is bad: " + exc.getMessage());
System.exit(-1);
}
return formatter;
}

how to store date value using hibernate

#Temporal(javax.persistence.TemporalType.DATE)
private Date docDate;
public Date getDocDate() {
return docDate;
}
public void setDocDate(Date docDate) {
this.docDate = docDate;
}
but the error shows like this
Error setting expression 'docDate' with value '[Ljava.lang.String;#843f72'
ognl.MethodFailedException: Method "setDocDate" failed for object net.top.app.entity.document.Document#291a66 [java.lang.NoSuchMethodException: net.top.app.entity.document.Document_$$_javassist_4.setDocDate([Ljava.lang.String;)]
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1285)
at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:1474)
Your code is trying to pass a String to setDocDate(). Change it so as to pass a Date.
As per the Error , setDocDate() is having Date field as input type.
You are setting the docDate in UI it self, try to convert the field to Date and set it to docDate

Mongo ISODate query in Java

I have a mongo query to be executed :
query = { "dateField" : { "$gte" : ISODate('2011-11-10T07:45:32.962Z') } }
When I do a db.Collection.find(query) on the mongo shell, I am able to retrieve the results.
How could I query this using Java ? I tried constructing a String based on the Date parameter.
But in the process of building the String, it eventually gets passed as "ISODate('2011-11-10T07:45:32.962Z')" instead of ISODate('2011-11-10T07:45:32.962Z') (without the surrounding quotes).
What would be the best way to construct this query using the Java API ?
Thanks !
Use a regular Java Date--also I recommend the QueryBuilder:
Date d = new Date(); // or make a date out of a string...
DBObject query = QueryBuilder.start().put("dateField").greaterThanEquals(d).get();
I have search lot and spend more than hour in finding how to get data when having ISODate in mongo model.
As the default constructor for date is deprecated so it was not working in my case.
BasicDBObject searchQuery = new BasicDBObject("_id" , 1);
try {
searchQuery.append("timestamp",BasicDBObjectBuilder.start( "$gte",new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS\'Z\'").parse("2015-12-07T10:38:17.498Z")).get());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Categories