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.
Related
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")));
}
I have this class:
public class Period {
private Date startDate = new Date();
private String startTime = "10:00";
private Date endDate = new Date();
private String endTime = "23:59";
}
Now i need two string outputs "2014-01-08T10:00:00" and "2014-01-08T23:59:00" !
How i can create these two outputs by using the class Period? It is necessary to work with the class Period, which includes startTime and EndTime as Strings!
I only have this small code for using the class SimpleDateFormat:
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Thanks for helping me !
What #tobias_k is saying is this. This is your format
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'");
When you call .format to format the Date, it returns a String
String dateString = format.format(period.getStartDate());
Then concatenate it
dateString = dateString + period.getStartTime();
Try this: Using GregorianCalendar
Date startDate = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
GregorianCalendar cal = new GregorianCalendar();
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), 10, 00, 00);
System.out.println(dateFormat.format(cal.getTime()));
Update1
To get the string value in variable:
String str = dateFormat.format(cal.getTime());
System.out.println(str);
I have a date object parsed from JSON in the follwing format:
String date = getJsonDateFromServer();
Date result = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(date);
But I am only interested in the time part (no months, year, day etc).
So my question is: How do I replace the date part (Months, Year, Day) correctly without loosing the time zone information?
What I finally want to do is the following:
(1) Parsing two dates (without date information). Start and Endtime
(2) Check for another date 'dw' (with date information) if 'dw' is in between the time defined in start and end from (1)
For example:
Date start = parse(18:00 +02:00);
Date end = parse(20:00 +02:00);
Date dw=new Date();
boolean inBetween = dw>= start && dw <= end;
How to do this?
/UPDATE:
I implemented it for test purposes this way:
public boolean isActiveOn(Date date) {
Calendar toCheck = Calendar.getInstance();
toCheck.setTime(date);
Calendar cal_start = Calendar.getInstance();
cal_start.setTime(this.time_begin);
cal_start.set(toCheck.get(Calendar.YEAR), toCheck.get(Calendar.MONTH),
toCheck.get(Calendar.DAY_OF_MONTH));
Calendar cal_end = Calendar.getInstance();
cal_end.setTime(this.time_end);
cal_end.set(toCheck.get(Calendar.YEAR), toCheck.get(Calendar.MONTH),
toCheck.get(Calendar.DAY_OF_MONTH));
boolean after_start = toCheck.after(cal_start);
boolean before_end = toCheck.before(cal_end);
return after_start && before_end;
}
But isn't that causing problems when different timezones are set in this.time_begin, this.time_end and date ?
All you have to do is:
String date = getJsonDateFromServer();
Date result = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(date);
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ssZ")
Date timeOnly = dateFormat.parse(dateFormat.format(result));
Now you have a date with time and timezone only.
And here is the compare code:
Date start = dateFormat.format(result);
Date end = dateFormat.format(resultNr2);
Date dw = dateFormat.format(resultNr3);
boolean inBetween = dw.after(start) && dw.before(end);
private DatePickerDialog createDialogWithoutDateField(){
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
(AdditionalRepaymentDetailClass.this, datePickerListener_edtToDate,mYear,mMonth, mDay);
DatePickerDialog edtToDate= new DatePickerDialog(DirectDebitClass.this, datePickerListener_edtToDate, year, month,day);
try{
Field[] datePickerDialogFields = edtToDate.getClass().getDeclaredFields();
for (Field datePickerDialogField : datePickerDialogFields) {
if (datePickerDialogField.getName().equals("mDatePicker")|| datePickerDialogField.getName().equals("mDaySpinner")) {
datePickerDialogField.setAccessible(true);
DatePicker datePicker = (DatePicker) datePickerDialogField.get(edtToDate);
Field datePickerFields[] = datePickerDialogField.getType().getDeclaredFields();
for (Field datePickerField : datePickerFields) {
if ("mDayPicker".equals(datePickerField.getName())|| "mDaySpinner".equals(datePickerField.getName())) {
datePickerField.setAccessible(true);
Object dayPicker = new Object();
dayPicker = datePickerField.get(datePicker);
((View) dayPicker).setVisibility(View.GONE);
}
}
}
}
}catch(Exception ex){
}
return edtToDate;
}
U can try this
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
I got a list of dates as String
for example date1->'11-11-2010' and date2->'12-01-2011'
I want to print all the dates between these two dates..
I tried to work with cal.add() but am not able to set my date1 to my cal.. if i do so i get null p
below code should do the trick for you.
String date1 = "11-11-2010";
String date2 = "12-01-2011";
SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(format.parse(date1));
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(format.parse(date2));
Date currentDate = calendar1.getTime();
while(!currentDate.equals(cal2.getTime())){
calendar1.add(Calendar.DAY_OF_MONTH, 1);
currentDate = cal1.getTime();
System.out.println(currentDate);
}