How to obtain Date values from database then set it as a string. Can anyone tell me why the below code is not working properly? This is the portion giving trouble:
// Date d = rs.getDate("PatientBirthDate");
// DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
// String date = df.format(d);
while (rs.next()) {
pid.setText(String.valueOf(rs.getInt("PatientID")));
ssn.setText(rs.getString("PatientSSN"));
firstname.setText(rs.getString("PatientFname"));
lastname.setText(rs.getString("PatientLname"));
gender.setText(rs.getString("PatientGender"));
// dob.setText(date);
streetnum.setText(rs.getString("PatientStreetNo"));
streetname.setText(rs.getString("PatientStreetName"));
city.setText(rs.getString("PatientCity"));
state.setText(rs.getString("PatientState"));
zip.setText(rs.getString("PatientZip"));
homephone.setText(rs.getString("PatientHomePhone"));
cellphone.setText(rs.getString("PatientCellPhone"));
email.setText(rs.getString("PatientEmailAddress"));
inspolicy.setText(rs.getString("InsurancePolicyNumber"));
inscompany.setText(String.valueOf(rs.getInt("InsuranceCompID")));
}
Why you are reading the date outside the loop?
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
while (rs.next()) {
pid.setText(String.valueOf(rs.getInt("PatientID")));
ssn.setText(rs.getString("PatientSSN"));
firstname.setText(rs.getString("PatientFname"));
lastname.setText(rs.getString("PatientLname"));
gender.setText(rs.getString("PatientGender"));
Date d = rs.getDate("PatientBirthDate");
String date = df.format(d);
dob.setText(date);
streetnum.setText(rs.getString("PatientStreetNo"));
streetname.setText(rs.getString("PatientStreetName"));
city.setText(rs.getString("PatientCity"));
state.setText(rs.getString("PatientState"));
zip.setText(rs.getString("PatientZip"));
homephone.setText(rs.getString("PatientHomePhone"));
cellphone.setText(rs.getString("PatientCellPhone"));
email.setText(rs.getString("PatientEmailAddress"));
inspolicy.setText(rs.getString("InsurancePolicyNumber"));
inscompany.setText(String.valueOf(rs.getInt("InsuranceCompID")));
}
Related
I am facing difficulties while converting the below while loop to an arraylist so I can display the values in selectonemenu component. The while loop code is:
String date1 = "JAN-2015";
String date2 = "APR-2015";
DateFormat formater = new SimpleDateFormat("MMM-yyyy");
Calendar beginCalendar = Calendar.getInstance();
Calendar finishCalendar = Calendar.getInstance();
beginCalendar.setTime(formater.parse(date1));
finishCalendar.setTime(formater.parse(date2));
while (beginCalendar.before(finishCalendar)) {
String date = formater.format(beginCalendar.getTime()).toUpperCase();
System.out.println(date);
beginCalendar.add(Calendar.MONTH, 1);
}
ArrayList<String> dateSelections = new ArrayList<>();
while (beginCalendar.before(finishCalendar))
{
String date = formater.format(beginCalendar.getTime()).toUpperCase();
dateSelections.add(date);
beginCalendar.add(Calendar.MONTH, 1);
}
dateSelections should be populated with your possible selections at the end of the while loop.
Having trouble in the following code. The output is dateStr: 11-Jan-11. Can anyone tell me why the date is modified?
String dateStr="";
String actionCompletionDueDate = "16/11/2011";
DateFormat srcDf = new SimpleDateFormat("mm/dd/yyyy");
DateFormat destDf = new SimpleDateFormat("dd-MMM-yy");
if(actionCompletionDueDate != null && !actionCompletionDueDate.equals("")) {
// parse the date string into Date object
System.out.println("actionCompletionDueDate: " + actionCompletionDueDate);
Date actionCompletionDate = srcDf.parse(actionCompletionDueDate);
dateStr = destDf.format(actionCompletionDate);
System.out.println("dateStr: " + dateStr);
}
Change
DateFormat srcDf = new SimpleDateFormat("mm/dd/yyyy");
to
DateFormat srcDf = new SimpleDateFormat("dd/MM/yyyy");
OR pass a correct string (which respects your format) to your code
String actionCompletionDueDate = "11/16/2011";
and correct the format to DateFormat srcDf = new SimpleDateFormat("MM/dd/yyyy");
mm is for minutes and MM is for months
String actionCompletionDueDate = "16/11/2011";
Should be
String actionCompletionDueDate = "11/16/2011";
Change
DateFormat srcDf = new SimpleDateFormat("mm/dd/yyyy");
to
DateFormat srcDf = new SimpleDateFormat("MM/dd/yyyy");
small mm here corresponds to minute.
But if you print source date using,
System.out.println(actionCompletionDate.toString());
Output is :
Sun Jan 16 00:11:00 IST 2011
See, 11 minute in time.
And change source date too, to 11/16/2011.
I Have a bean , in that I have one property of date type.
private Date insurance_date;
public Date getInsurance_date() {
return insurance_date;
}
public void setInsurance_date(Date insurance_date) {
this.insurance_date = insurance_date;
}
But get method gives us a date with time so I wrote a formated method as,
public String getFormatedDoI() {
DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
String stirngDate = null ;
if(insurance_date != null)
stirngDate = df.format((insurance_date));
return stirngDate;
}
But the problem is I have date in db as 2014-05-21 , when I use getFormatedDoI() method it prints 21-00-2014. In fact for all the dates in place of month it displays 0. How can I get exact formated date.? and the database I am using is mysql. The date coming from MYSQL database.
Small mm is for minutes, capital MM is for month number, if you want to get month name you can use MMM, in your case use "dd-MM-yyy" instead of 'dd-mm-yyyy'.
public String getFormatedDoI() {
DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
String stirngDate = null ;
if(insurance_date != null)
stirngDate = df.format((insurance_date));
return stirngDate;
}
Should solve your problem :)
mm takes minute.
If you want month then use MM.
so instead of DateFormat df = new SimpleDateFormat("dd-mm-yyyy");
use : DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
See this documentation on date format.
try
DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
instead of
DateFormat df = new SimpleDateFormat("dd-mm-yyyy");
It should resolve
Use "dd-MM-yyyy" instead of "dd-mm-yyyy". The pattern "mm" stands for minutes not for month.
I want to create a method to validate a date by using SimpleDateFormat.
If the date is valid(e.g. 02/09/2012 or 2/09/2012 or 02/9/2012), this method should return true.
But if the format of the date is wrong(e.g. 02/09/201X) or logically wrong(e.g. 32/09/2012), this method should return false.
I try to write this method like this:
private boolean isValidDate(String date) {
DateFormat df1 = new SimpleDateFormat("dd-MM-yyyy");
DateFormat df2 = new SimpleDateFormat("d-MM-yyyy");
DateFormat df3 = new SimpleDateFormat("dd-M-yyyy");
Date d = null;
String s = null;
try {
d = df1.parse(date);
}catch (Exception e1) {
try{
d = df2.parse(date);
}catch (Exception e2) {
try {
d= df3.parse(date);
}catch (Exception e3) {
return false;
}
s = df3.format(d);
return date.equals(s);
}
s = df2.format(d);
return date.equals(s);
}
s = df1.format(d);
return date.equals(s);
}
But if I validate a date, for instance, 2/09/2012, it returns false (actually it should return true). I have no idea why... Can anyone find what's the problem with my code, or this logic is totally wrong? Is there any better way to do this validation?
Your input is in the format 2/09/2012 not 2-09-2012, so your dateformat should be like below:
DateFormat df1 = new SimpleDateFormat("dd/MM/yyyy");
DateFormat df2 = new SimpleDateFormat("d/MM/yyyy");
DateFormat df3 = new SimpleDateFormat("dd/M/yyyy");
I think your code is fine (but not very scalable - try to do it in a for-loop in case you add more formats later).
The problem is that your format strings are wrong. Instead of dd-MM-yyyy you should have dd/MM/yyyy. The same goes for the rest of the formats:
DateFormat df1 = new SimpleDateFormat("dd/MM/yyyy");
DateFormat df2 = new SimpleDateFormat("d/MM/yyyy");
DateFormat df3 = new SimpleDateFormat("dd/M/yyyy");
The validation fails because / isn't -.
Add this additional check after parsing each string:
Tokenize the value of the date String
Then use the extracted day, month and year values to create a new Date object using GregorianCalendar
Compare this to the date object you created from parsing the date strings
If they match then you know that the input string contained a valid date format
Good Day.
I've got another problem related to Jtable.
I want to change the row color of a table if the date inside column (expiry) exceeds or is equal to the current date.
I tried this code but i get an error: java.lang.NumberFormatException: For input string: "2012-03-15"
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
String expDateString = sdf.format(cal.getTime());
System.out.println(expDateString);
Double date = Double.parseDouble(expDateString);
Double val = Double.parseDouble(tableSummary.getModel().getValueAt(row, 6).toString());
for(int i=0; i<=tableSummary.getRowCount()-1; i++){
if(val >= date){
renderer.setBackground(red);
}
}
Thanks!
here's a new code:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
String expDateString = sdf.format(cal.getTime());
Date today = new Date(expDateString);
System.out.println("ang churva is " + today);
Date given = new Date(tableSummary.getModel().getValueAt(row, 6).toString());
for(int i=0; i<=tableSummary.getRowCount()-1; i++){
if(today.compareTo(given)>=0){
renderer.setBackground(red);
}
}
but i get this exception: java.lang.IllegalArgumentException at Date today = new Date(expDateString);
Use the code
DATEDIFF('d',NOW(),exdate)
in your resultset query. It will return the difference. Alter it possibly to match your needs.
You can't cast a date string in a double
Double date = Double.parseDouble(expDateString); //does not work!
Simple example of how you can compare you dates. Note that if the objects in your JTable already are Dates, you don't need all the parsing, which would make your life easier.
The output of the code below is:
expiryDate=2012-03-15
tableDateOK=2012-03-12
tableDateExpired=2012-03-18
tableDateOK>expiryDate = false
tableDateExpired>expiryDate = true
Code:
public static void main(String args[]) throws ParseException {
String expiryDate = "2012-03-15";
String tableDateOk = "2012-03-12";
String tableDateExpired = "2012-03-18";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("expiryDate="+expiryDate);
System.out.println("tableDateOK="+tableDateOk);
System.out.println("tableDateExpired="+tableDateExpired);
System.out.println("tableDateOK>expiryDate = " + sdf.parse(tableDateOk).after(sdf.parse(expiryDate)));
System.out.println("tableDateExpired>expiryDate = " + sdf.parse(tableDateExpired).after(sdf.parse(expiryDate)));
}
line Double date = Double.parseDouble(expDateString);
this cannot work because string "2012-03-15" is simply not a valid double value.
I do not understand why you are trying to compare two double values:
if you have Date in table, use Date.after() and Date.before() to find out, whether your date is before or after now.
if you have String in table, use the SimpleDateFormat.parse() to get Date from it and do point 1
public String compareDate( Request request ) throws ParseException {
Date debitDate= request.getPaymentTxn().getCrValDt();
Date now = new Date();
String response="";
SimpleDateFormat sdfDate = new SimpleDateFormat("dd/MM/yyyy");
String strCurrDate = sdfDate.format(now);
String strDebitDate = sdfDate.format(debitDate);
System.out.println("Current Date: " + strCurrDate);
Date currentDate = new SimpleDateFormat("dd/MM/yyyy").parse(strCurrDate);
Date txnDate = new SimpleDateFormat("dd/MM/yyyy").parse(strDebitDate);
System.out.println("C -> "+currentDate);
System.out.println("C -> "+txnDate);
if (txnDate!=null){
if (currentDate.equals(txnDate))
{
System.out.println("Valid Txn");
response="valid";
}
if (currentDate.after(txnDate))
{
System.out.println("--> Not Valid TXN Past");
response="notValid";
}
if (currentDate.before(txnDate)){
System.out.println("Future Valid TXn");
response="future";
}
}
return response;
}
PLease Chk it out its working fine